An open API service indexing awesome lists of open source software.

https://github.com/dekirisu/strung

Easy access of struct fields in strings using different/custom pre/postfix: "Hello, {field}" in rust
https://github.com/dekirisu/strung

field rust string string-formatter string-manipulation struct

Last synced: 5 months ago
JSON representation

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

Awesome Lists containing this project

README

          

















## Easy access to struct fields in strings
🐠 add strung to the dependencies in the `Cargo.toml`:
```toml
[dependencies]
strung = "0.1"
```
🦀 use/import everything of the prelude in rust:
```rust
use strung::prelude::*;
```
🦊 works for **unnamed** fields:
```rust
#[derive(Strung)]
struct Struct (&'static str, u32);
fn main(){
let s: String = Struct("Bob", 10).strung("{0} is {1}th");
}
```
🦊 works for **named** fields:
```rust
#[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](https://doc.rust-lang.org/std/fmt/trait.Display.html), it has to be ignored!
```rust
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,
```rust
#[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:
```rust
#[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(..)`:
```rust
#[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:
```rust
#[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,..)`:
```rust
#[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::

(..)`:

```rust
#[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](https://docs.rs/strung)

🦎 Changelog

[🐱 GitHub](https://github.com/dekirisu/strung)

[👾 Discord Server](https://discord.gg/kevWvBuPFg)

---
### 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.