How do I add examples to macro documentation in rust? - macros

When writing a macro, I would like to document it proprerly, and this includes examples.
But when I try to do that in the same way as a regular function I get:
[E0468]: an `extern crate` loading macros must be at the crate root
I run cargo test on nightly to test the following:
// src/lib.rs in crate with name advent9
/// this macro essentialy appends .as_bytes()
/// `b!("bla")` -> `"bla".as_bytes()`
///
/// # Examples
/// ```
/// #[macro_use]
/// extern crate advent9;
///
/// let bytes : &[u8] = b!("this is a bytestring");
///
/// println!("{:?}", bytes);
/// // prints:
/// // [116, 104, 105, 115, 32, 105, 115, 32, 97, 32, 98, 121, 116, 101, 115, 116, 114, 105, 110, 103]
/// ```
// I don't need this exported, but perhaps the example does?
#[macro_export]
macro_rules! b {
($string:expr) => {
$string.as_bytes()
}
My understanding of the doctests is that each gets wrapped in their own main function. Like this:
fn main() {
#[macro_use]
extern crate advent9;
let bytes : &[u8] = b!("this is a bytestring");
println!("{:?}", bytes);
// prints:
// [116, 104, 105, 115, 32, 105, 115, 32, 97, 32, 98, 121, 116, 101, 115, 116, 114, 105, 110, 103]
}
If this is correct, it would explain the error.
Is there any way to actually add examples to macros?

It is possible, though a bit convoluted; you need to do it in the following way:
/// # Example
/// ```
/// # #[macro_use] extern crate crate_name;
/// # fn main() {
/// use crate_name::module::object;
///
/// <example code>
/// # }
/// ```
#[macro_export]
macro_rules! some_macro {
<macro code>
}

Related

How to get bounding box with text using Tesseract4Android?

I am using 'cz.adaptech.tesseract4android:tesseract4android:4.3.0' in my Android project.
Is it possible to get bounding box with text data like in example below?
(32, 24, 60, 17) Maitre
(100, 24, 82, 19) corbeau,
(191, 28, 29, 13) sur
(227, 28, 22, 12) un
(257, 24, 50, 17) arbre
(315, 24, 70, 21) perché,
(79, 49, 58, 17) Tenait
Official sample shows how to get plain text only, not boxes with text inside:
TessBaseAPI tess = getTessBaseAPI(path, context);
String text = tess.getUTF8Text();
To get bounding box with text use next code:
TessBaseAPI tess = new TessBaseAPI();
// Given path must contain subdirectory `tessdata` where are `*.traineddata` language files
String dataPath = context.getExternalFilesDir(null).getPath() + "/OCRme/";
// Initialize API for specified language (can be called multiple times during Tesseract lifetime)
if (!tess.init(dataPath, "eng", TessBaseAPI.OEM_TESSERACT_LSTM_COMBINED)) {
throw new IOException("Error initializing Tesseract (wrong data path or language)");
}
// Specify image and then recognize it and get result (can be called multiple times during Tesseract lifetime)
tess.setImage(bitmap);
tess.setPageSegMode(TessBaseAPI.PageSegMode.PSM_AUTO_OSD);
tess.getUTF8Text();
ResultIterator resultIterator = tess.getResultIterator();
List < Rect > boxes = new ArrayList < > ();
List < String > texts = new ArrayList < > ();
while (resultIterator.next(TessBaseAPI.PageIteratorLevel.RIL_WORD)) {
Rect rect = resultIterator.getBoundingRect(TessBaseAPI.PageIteratorLevel.RIL_WORD);
String text = resultIterator.getUTF8Text(TessBaseAPI.PageIteratorLevel.RIL_WORD);
boxes.add(rect);
texts.add(text);
}

Is there any way to download file converted by webcontent_converter package?

I have some pdf content which i retrieved from the API and i converted it using webcontent_converter package. But when i try to download it, it is throwing error or not downloading.
here is the code:
var dir = await getApplicationDocumentsDirectory();
var savedPath = "${dir.path}/$title.pdf";
var result = await WebcontentConverter.contentToPDF(
content: content,
savedPath: savedPath,
format: PaperFormat.a4,
margins:
PdfMargins.px(top: 55, bottom: 55, right: 55, left: 55),
);
I can convert the above result into file by :
File file = File(result!)
but how can i download the file?

Randomize swatch for a color passed to a function in Flutter

I have a function that gets Color as an argument. I want to generate a list of random swatches for that color within that function. For example, if the color passed is Colors.amber I want a list like:
[ Colors.amber[100], Colors.amber[800], Colors.amber[500], Colors.amber[900], Colors.amber[300]... ]
Is it possible? Any advice or help would be appreciated. Thank you in advance.
You can randomize only the list of "plus colors":
Flutter Colors
List<int> types = [50, 100, 200, 300, 400, 600, 700, 800, 900]
And randomize a number between 0..8
int get getRandomNumber => 0 + Random().nextInt(8 - 0);
Use to get the random color
Color? selectedColor = Colors.amber[types[getRandomNumber]];
Update:
Using this method extension, we can create a "workaround"
extension HexColor on Color {
/// String is in the format "aabbcc" or "ffaabbcc" with an optional leading "#".
Color fromHex(String hexString) {
final buffer = StringBuffer();
if (hexString.length == 6 || hexString.length == 7) buffer.write('ff');
buffer.write(hexString.replaceFirst('#', ''));
return Color(int.parse(buffer.toString(), radix: 16));
}
/// Prefixes a hash sign if [leadingHashSign] is set to `true` (default is `true`).
String toHex({bool leadingHashSign = true}) => '${leadingHashSign ? '#' : ''}'
'${alpha.toRadixString(16).padLeft(2, '0')}'
'${red.toRadixString(16).padLeft(2, '0')}'
'${green.toRadixString(16).padLeft(2, '0')}'
'${blue.toRadixString(16).padLeft(2, '0')}';
}
And using this function
Color getRandomColor(String selectedColor) {
final List<int> types = [50, 100, 200, 300, 400, 600, 700, 800, 900];
int getRandomNumber = 0 + Random().nextInt(8 - 0);
return Color.fromHex(selectedColor)[types[getRandomNumber]];
}
//true will return "#ffffff", false will return "ffffff"
Color _color = getRandomColor(Color.red.toHex(true));
Found a simple solution
Let color be the passed parameter. Now find the index of that color in the Colors.primaries list.
for(int i=0;i<Colors.primaries.length;i++){
if(color==Colors.primaries[i]){
colorIndex=i;
}
}
Use this index to generate the random swatch of the preferred color.
Colors.primaries[colorIndex][Random.nextInt(9) * 100]

Customizing the Eclipse code formatter on many arguments

When customizing the autoformatter from Eclipse, you have several options for identation policy for several cases. When it comes to function calls with many arguments, the most of you probably get something like this result:
/**
* Arguments
*/
class Example {
void foo() {
Other.bar(
100,
nested(
200,
300,
400,
500,
600,
700,
800,
900));
}
}
Would anybody else here find something like this would look better and more clearly?
/**
* Arguments
*/
class Example {
void foo() {
Other.bar(
100,
nested(
200,
300,
400,
500,
600,
700,
800,
900
)
);
}
}
Are there any possibilities to achieve such results? Thanks!

Overriding the always-top property of the navigation bar in Titanium

In Titanium for iPhone is it possible to display something above the navigation bar – or just disabling the always-top property of the navigation bar?
This is how it looks right now:
This is part of the actual Photoshop-mock-up:
The code-snippet invoking this is:
var win1 = Titanium.UI.createWindow({
title: 'Home',
navBarHidden: false,
barImage: 'topbar.png',
backgroundImage: 'bga.png'
});
c = Titanium.UI.createImageView({
image: 'logobar.png',
top: -13,
right: 7,
width: 74,
height: 108,
exitOnClose: !0
})
try {
win1.add(c);
c.animate({zIndex:3});
win1.addEventListener('focus', function () {
Titanium.App.Analytics.trackPageview('/win1')
});
}catch(e){
alert(e);
}
The try-catch was implemented as I didn't trust the existence of .animate, however it did exist but did not work.
Answer(, or maybe not what it should be like)
Titanium itself does not support the feature of manipulating the zIndex or rather the onTop-properties. However, I've found a workaround allowing the overlay to be shown.
This workaround works by the way Titanium handles windows. First, we define the main window (e.g. win1) and fill it. Then we create an assistant window (e.g. win1a) and assign the ImageView to it. Then we position the new window on top of the other window and voilà.
var win1 = Titanium.UI.createWindow({
title: "*******",
navBarHidden: false,
barImage: 'topbar.png',
backgroundColor: "gray",
});
var win1l = Titanium.UI.createWindow({
title: "",
navBarHidden: true,
height: 84,
width: 64,
right: 0,
top: 0
});
// Inject ImageView into top-most window
win1l.add(Titanium.UI.createImageView({
image: "logobar.png",
top: 2,
right: 5,
width: 60.3, //74, // 74/108 = 0.6851851852
height: 88, //108, // ((108-20)*(74/108)) = 60.29629 ~ 60.3
exitOnClose: !0
}));
win1l.open();
I hope this might have been helpful for you.
Thanks, -Kenan.