#struct-fields #format-string #string #struct #replace #format #fmt

strung

Easy access of struct fields in strings using different/custom pre/postfix: "Hello, {field}"

5 releases

0.1.4 Nov 11, 2023
0.1.3 Sep 19, 2022
0.1.2 Sep 14, 2022
0.1.1 Sep 12, 2022
0.1.0 Sep 12, 2022

#46 in Value formatting

Download history 6/week @ 2024-02-22 3/week @ 2024-02-29 1/week @ 2024-03-07 1/week @ 2024-03-14 8/week @ 2024-03-21 95/week @ 2024-03-28 5/week @ 2024-04-04

108 downloads per month
Used in 2 crates (via querio)

MIT/Apache

22KB
68 lines

Easy access to struct fields in strings

🐠 add strung to the dependencies in the Cargo.toml:

[dependencies]
strung = "0.1"

🦀 use/import everything of the prelude in rust:

use strung::prelude::*;

🦊 works for unnamed fields:

#[derive(Strung)]
struct Struct (&'static str, u32);
fn main(){
    let s: String = Struct("Bob", 10).strung("{0} is {1}th"); 
}

🦊 works for named fields:

#[derive(Strung)]
struct Struct {
    name: &'static str,
    pos: u32,
}
fn main(){
    let s: String = Struct{name:"Bob", pos:10}.strung("{name} is {pos}th."); 
    // fields can also be addressed by their index
    let z: String = Struct{name:"Bob", pos:10}.strung("{0} is {1}th."); 
}

Ignore

🐳 use #[igno], #[ignore],#[strung(igno)] or #[strung(ignore)] to make a field unavailable
🦞 if a field type doesn't implement Display, it has to be ignored!

struct NoDisplay;
#[derive(Strung)]
struct Struct (&'static str, u32, #[igno] NoDisplay);
fn main(){
    let s: String = Struct("Bob", 10, NoDisplay)
        .strung("{0} is {1}th, he won {2}!"); 
}

Cascade

🐳 use #[cscd], #[cascade], #[strung(cscd)] or #[strung(cascade)] to cascade (recursion).
🐑 cascaded fields are ignored by default
🐔 use #[notice], #[ntce] or inside #[strung(..)] to make them available,

#[derive(Strung)]
struct Struct &'static str, u32);
#[derive(Strung)]
struct Cascade (u32, #[cscd] Struct);
fn main(){
    let s: String = Cascade(11,Struct("Bob", 10))
        .strung("{1.0} is {1.1}th for the {0}th time!"); 
    // => Bob is 10th for the 11th time!
}

Prefix and Postix Prefabs

🐈 5 different prefabs are provided:

#[derive(Strung)]
struct Struct (&'static str, u32);
fn main(){
    let t = Struct("Bob", 10);
    let s = t.strung_curly("{0} is {1}th."); 
    let s = t.strung_angle("<0> is <1>th."); 
    let s = t.strung_dollry("${0} is ${1}th."); 
    let s = t.strung_dollar("$0 is $1th."); 
    let s = t.strung_hashtag("#0 is #1th."); 
}

Custom Prefix and Postix

🦊 you can also customize pre-/postfixes in different ways
🦅 globally - using static variables and .strung_static(..):

#[derive(Strung)]
struct Struct (&'static str, u32);
fn main(){
    strung::set_static("<",">");
    let s: String = Struct("Bob", 10).strung_static("<0> is <1>th."); 
}

🐎 per struct - this overrides the default .strung(..) pre/postfix:

#[derive(Strung)]
#[strung("<",">")]
struct Struct (&'static str, u32);
fn main(){
    let s: String = Struct("Bob", 10).strung("<0> is <1>th."); 
}

🐍 per call - using parameters .strung_dynamic(pre,post,..):

#[derive(Strung)]
struct Struct (&'static str, u32);
fn main(){
    let s: String = Struct("Bob", 10).strung_dynamic("<",">","<0> is <1>th."); 
}

🦎 per call - using generic const chars .strung_generic::<pre,post>(..):

#[derive(Strung)]
struct Struct (&'static str, u32);
fn main(){
    let s: String = Struct("Bob", 10).strung_generic::<'<','>'>("<0> is <1>th."); 
}

Performance Comparison

🐕 dynamic/generic/global have equal runtime speed
🐇 default/prefabs/per-struct are faster!
🐁 Using a string of ~650 chracters and 6 field placeholders:

More Information

🦕 Documentation
🦎 Changelog
🐱 GitHub
👾 Discord Server


License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~330–780KB
~19K SLoC