
String formatter/builder with easy access of struct fields, which implement the Display trait.
If they do not, they can be marked to be ignored.
Usage
Here's most you have to know!
use strung::prelude::*;
fn main(){
let NAMED = Test {num: 1, name: "st"};
let TUPLE = TestTup (1, "st");
let CUSTOM = TestCustom {num: 1, nop: NoDsply};
let text = NAMED.strung("{num}{name}");
assert_eq!(&text,"1st");
let s: String = "{num}{name}".into();
let text = NAMED.strung(&s);
assert_eq!(&text,"1st");
let text = NAMED.strung("{num}{num}th < {num}{name}");
assert_eq!(&text,"11th < 1st");
let text = TUPLE.strung("{0}{1}");
assert_eq!(&text,"1st");
let text = CUSTOM.strung("%num%st");
assert_eq!(&text,"1st");
let text = CUSTOM.strung_curly("{num}st");
assert_eq!(&text,"1st");
let text = CUSTOM.strung_curly("{num}st {nop}");
assert_eq!(&text,"1st {nop}");
let text = NAMED.strung_dollar("$num$name");
assert_eq!(&text,"1st");
let text = NAMED.strung_dollry("${num}${name}");
assert_eq!(&text,"1st");
let text = NAMED.strung_hashtag("#num#name");
assert_eq!(&text,"1st");
let text = NAMED.strung_angle("<num><name>");
assert_eq!(&text,"1st");
let text = NAMED.strung_dynamic("<",">","<num><name>");
assert_eq!(&text,"1st");
strung::config::static_global("+","+");
let text = NAMED.strung_static("+num++name+");
assert_eq!(&text,"1st");
strung::config::static_global("[","]");
let text = NAMED.strung_static("[num][name]");
assert_eq!(&text,"1st");
let CASCADE = TestCascade {tup: TestTup(2,"nd")};
let text = CASCADE.strung_dollar("$tup.0$tup.1");
assert_eq!(&text,"2nd");
}
#[derive(Strung)] struct Test {
num: u32,
name: &'static str,
}
#[derive(Strung)] struct TestTup(u32,&'static str);
#[derive(Strung)] #[strung("%","%")] struct TestCustom {
num: u32,
#[strung(ignore)] nop: NoDsply
}
#[derive(Strung)] struct TestCascade {
#[strung(cascade,ignore)]
tup: TestTup
}
struct NoDsply;
About Statics
Prelude imports two static variables [config::STRUNG_PRE] and [config::STRUNG_POST], which can be used
to set the prefix and postfix as a configuration. [Strung::strung_static] uses anything called STRUNG_PRE or STRUNG_POST
on the file.
[config::static_global] changes these variables, as you saw in the walkthrough, there's another method of
changing it per file. It's not included in the walkthrough cause it shadows these variables, it's a macro called [config::static_on_file].
📝 Note: This will maybe change in future, so these variables dont have to always be imported.
But: here's how it can be used for now:
use strung::prelude::*; strung::config::static_on_file!("[","]"); #[derive(Strung)] struct Test {
text: &'static str,
num: u32
}
fn main(){
let test = Test { text: "5k",
num: 5000
};
let text = test.strung_static("[text]=[num]"); assert_eq!(&text,"5k=5000");
}
Ignore fields
Sometimes you wanna ignore certain fields - e.g. in these scenarios:
- Get even moar efficiency 📈
- A field-type doesn't implement [std::fmt::Display]
This can be done with the #[strung(ignore)] attribute:
use strung::prelude::*;
struct CustomField (u32);
#[derive(Strung)]
struct Test {
num: u32,
#[strung(ignore)] nope: CustomField }
#[derive(Strung)]
struct TestTup (
u32,
#[strung(ignore)] CustomField, &'static str
);
fn main(){
let test = Test { num: 1,
nope: CustomField(0), };
let text = test.strung("Number {num} {nope}"); assert_eq!(&text,"Number 1 {nope}");
let test = TestTup(1,CustomField(0),":)"); let text = test.strung("Number {0} {1} {2}"); assert_eq!(&text,"Number 1 {1} :)");
}
❗ Experimental: Cascading ❗
There's also the possibility of cascading. e.g.: $field.0.num, it's experimentally implemented for [Strung::strung_dollar] and [Strung::strung_hashtag] at the moment,
cause it was the easiest to do. 🦀
For this to work, the field-type has to derive [Strung] via derive macro and mark it with the #[strung(cascade)] attribute:
use strung::prelude::*;
#[derive(Strung)] struct A {#[strung(cascade,ignore)]field:B}
#[derive(Strung)] struct B (u32,#[strung(cascade,ignore)]C);
#[derive(Strung)] struct C {#[strung(cascade,ignore)]field:D,num:u32}
#[derive(Strung)] struct D (#[strung(cascade,ignore)]E);
#[derive(Strung)] struct E {num:u32}
fn main(){
let test = A{
field: B(500,C{
num: 123,
field: D(E{
num: 623
})
})
};
let text = test.strung_dollar(
"Hello, $field.0 + $field.1.num = $field.1.field.0.num"
);
assert_eq!(&text,"Hello, 500 + 123 = 623");
let text = test.strung_hashtag(
"Hello, #field.0 + #field.1.num = #field.1.field.0.num"
);
assert_eq!(&text,"Hello, 500 + 123 = 623");
}
License