Skip to content

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

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

dekirisu/strung

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

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

Topics

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Packages

No packages published

Languages