1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
//! String formatter/builder with easy access of struct fields, which implement the [std::fmt::Display] trait.
//! If they do not, they can be marked to be ignored.
//! # Usage
//! Here's most you have to know!
//! ```
//! use strung::prelude::*; // import everything from prelude
//!
//! fn main(){
//!
//! // create structs - defined below cause instant action!
//! let NAMED = Test {num: 1, name: "st"};
//! let TUPLE = TestTup (1, "st");
//! let CUSTOM = TestCustom {num: 1, nop: NoDsply};
//!
//! // most general - you'll probably mostly use this! - using {field_name}
//! let text = NAMED.strung("{num}{name}");
//! assert_eq!(&text,"1st");
//!
//! // also works with String, just reference it
//! let s: String = "{num}{name}".into();
//! let text = NAMED.strung(&s);
//! assert_eq!(&text,"1st");
//!
//! // it will always replace every occurrence
//! let text = NAMED.strung("{num}{num}th < {num}{name}");
//! assert_eq!(&text,"11th < 1st");
//!
//! // for tuple structs, use the fields index number, instead of the name
//! let text = TUPLE.strung("{0}{1}");
//! assert_eq!(&text,"1st");
//!
//! // the [strung] function will change if you set custom pre/postfix - see TestCustom below
//! let text = CUSTOM.strung("%num%st");
//! assert_eq!(&text,"1st");
//!
//! // there are different presets, so you can still use {field_name} using [strung_curly]
//! let text = CUSTOM.strung_curly("{num}st");
//! assert_eq!(&text,"1st");
//!
//! // note: {nop} is not available, cause it's ignored - see TestCustom below
//! let text = CUSTOM.strung_curly("{num}st {nop}");
//! assert_eq!(&text,"1st {nop}");
//!
//! // [strung_dollar] for $field_name
//! let text = NAMED.strung_dollar("$num$name");
//! assert_eq!(&text,"1st");
//!
//! // [strung_dollry] for ${field_name}
//! let text = NAMED.strung_dollry("${num}${name}");
//! assert_eq!(&text,"1st");
//!
//! // [strung_hashtag] for #field_name
//! let text = NAMED.strung_hashtag("#num#name");
//! assert_eq!(&text,"1st");
//!
//! // [strung_angle] for <field_name>
//! let text = NAMED.strung_angle("<num><name>");
//! assert_eq!(&text,"1st");
//!
//! // most flexible - inline setting via [strung_dynamic] - a bit less efficient
//! let text = NAMED.strung_dynamic("<",">","<num><name>");
//! assert_eq!(&text,"1st");
//!
//! // also flexible - global static variables, you can easily change ...
//! strung::config::static_global("+","+");
//! let text = NAMED.strung_static("+num++name+");
//! assert_eq!(&text,"1st");
//!
//! // ... whenever you want, but usually you'll just need it once at the start of main()
//! strung::config::static_global("[","]");
//! let text = NAMED.strung_static("[num][name]");
//! assert_eq!(&text,"1st");
//!
//! // [strung_hashtag] and [strung_dollar] also enable cascading
//! let CASCADE = TestCascade {tup: TestTup(2,"nd")};
//! let text = CASCADE.strung_dollar("$tup.0$tup.1");
//! assert_eq!(&text,"2nd");
//!
//! }
//!
//! // named struct
//! #[derive(Strung)] // easy derive
//! struct Test {
//! num: u32,
//! name: &'static str,
//! }
//!
//! // tuple struct
//! #[derive(Strung)] // easy derive
//! struct TestTup(u32,&'static str);
//!
//! // custom pre/postfix per struct + ignore
//! #[derive(Strung)] // easy derive
//! #[strung("%","%")] // custom pre/postfix!
//! struct TestCustom {
//! num: u32,
//! // ignore: makes this field unavailable
//! // this would fail w/o the ignore, cause no [Display]!
//! // other usage: gain a lil more performance
//! #[igno] // or #[strung(igno)] 2b more specific
//! nop: NoDsply
//! }
//!
//! // custom pre/postfix per struct + ignore
//! #[derive(Strung)] // easy derive
//! struct TestCascade {
//! // cascade: makes the fields of another Strung available
//! // automatically ignores the struct itself, not its fields
//! #[cscd] //, #[cascade], #[strung(cascade)] or #[strung(cscd)]
//! tup: TestTup
//! }
//!
//! // struct with no Display trait
//! 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 <strong>maybe</strong> change in future, so these variables dont have to always be imported.
//!
//! But: here's how it can be used for now:
//! ```
//! // Import everything from prelude
//! use strung::prelude::*;
//! // Shadow static pre/postfix for this file.
//! strung::config::static_on_file!("[","]");
//!
//! #[derive(Strung)]
//! struct Test {
//! text: &'static str,
//! num: u32
//! }
//! fn main(){
//! // Create struct as usual
//! let test = Test {
//! text: "5k",
//! num: 5000
//! };
//! // Use whatever you've set above
//! 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 `#[igno]` attribute, or if it interferes with other
//! macros, you can be more specific: `#[strung(ignore)]` or `#[strung(igno)]`:
//! ```
//! // Import everything from prelude
//! use strung::prelude::*;
//! // A struct, not impl Display
//! struct CustomField (u32);
//!
//! #[derive(Strung)]
//! struct Test {
//! num: u32,
//! // Would fail without the attribute:
//! #[igno] nope: CustomField
//! }
//!
//! #[derive(Strung)]
//! struct TestTup (
//! u32,
//! // Would fail without the attribute:
//! #[strung(ignore)] CustomField,
//! &'static str
//! );
//!
//! fn main(){
//! /* ------------------------------ Named Fields ------------------------------ */
//! // Create struct as usual
//! let test = Test {
//! num: 1,
//! nope: CustomField(0),
//! };
//! // {nope} not available!
//! let text = test.strung("Number {num} {nope}");
//! assert_eq!(&text,"Number 1 {nope}");
//!
//! /* ------------------------- Unnamed Fields (Tuple) ------------------------- */
//! // Create struct as usual
//! let test = TestTup(1,CustomField(0),":)");
//! // {1} not available!
//! 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 `#[cascade]`, `#[cscd]` attribute or `#[strung(cascade)]`, `#[strung(cscd)]` in more specific use cases:
//! ```
//! use strung::prelude::*;
//!
//! // #[strung(ignore)] just because none of them are implementing Display!
//! #[derive(Strung)] struct A {#[cscd]field:B}
//! #[derive(Strung)] struct B (u32,#[cascade]C);
//! #[derive(Strung)] struct C {#[strung(cascade)]field:D,num:u32}
//! #[derive(Strung)] struct D (#[strung(cscd)]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");
//! }
//! ```
//!
/// Designed to be used with the strung-derive crate!
pub trait Strung {
/// Default text replacement via `{field_name}`, changable via `#[strung("pre","post")]`
/// This is the most efficient cause pre- anf postfixes are merged with the field-names on compilation time!
fn strung(&self, _text: &str) -> String {todo!()}
/// Replacement with custom static postfixes and prefixes
/// - STRUNG_PRE for the prefix - Default: "$"
/// - STRUNG_POST for the postfix - Default: ""
/// Therefore by default replacement via `$field_name`.
///
/// Easy changable:
/// - File scope (shadowing): [config::static_on_file]
/// - Global scope (mut): [config::static_global]
fn strung_static(&self, _text: &str) -> String {todo!()}
/// Replacement with custom inline Postfixes and Prefixes
fn strung_dynamic(&self, _pre: &str, _post:&str, _text: &str) -> String {todo!()}
/// Replacement with custom generic constant char Postfixes and Prefixes
fn strung_generic <const A:char,const Z:char>(&self, _text: &str) -> String {todo!()}
/// Same as [Strung::strung] but not changable and always addressable by `{field_name}`
fn strung_curly(&self, _text: &str) -> String {todo!()}
/// Same as [Strung::strung] but not changable and always addressable by `$field_name`
fn strung_dollar(&self, _text: &str) -> String {todo!()}
/// Same as [Strung::strung] but not changable and always addressable by `${field_name}`
fn strung_dollry(&self, _text: &str) -> String {todo!()}
/// Same as [Strung::strung] but not changable and always addressable by `#field_name`
fn strung_hashtag(&self, _text: &str) -> String {todo!()}
/// Same as [Strung::strung] but not changable and always addressable by `<field_name>`
fn strung_angle(&self, _text: &str) -> String {todo!()}
}
/// Just an empty unit struct with Strung trait!
pub struct StrungUnit;
impl Strung for StrungUnit {
fn strung_dynamic(&self, _pre: &str, _post:&str, text: &str) -> String {text.into()}
fn strung(&self, text: &str) -> String {text.into()}
fn strung_static(&self, text: &str) -> String {text.into()}
fn strung_curly(&self, text: &str) -> String {text.into()}
fn strung_dollar(&self, text: &str) -> String {text.into()}
fn strung_dollry(&self, text: &str) -> String {text.into()}
fn strung_hashtag(&self, text: &str) -> String {text.into()}
fn strung_angle(&self, text: &str) -> String {text.into()}
fn strung_generic <const STRUNG_PRE:char,const STRUNG_POST:char>(&self, text: &str) -> String {text.into()}
}
pub mod config {
//! Configurations, currently just static pre- and postfix! 👻
/// Prefix used by [super::Strung::strung_static]
pub static mut STRUNG_PRE: &'static str = "$";
/// Postfix used by [super::Strung::strung_static]
pub static mut STRUNG_POST: &'static str = "";
/// Quickly shadow [STRUNG_PRE] and [STRUNG_POST] for the current file.
///
/// Use it after importing the prelude:
/// ```
/// use strung::prelude::*;
/// strung::config::static_on_file!("{","}");
/// // ...
/// ```
#[macro_export]
macro_rules! static_on_file {($pre: expr, $post: expr) => {
const STRUNG_PRE: &'static str = $pre;
const STRUNG_POST: &'static str = $post;
};}
pub use static_on_file;
/// Quickly change [STRUNG_PRE] and [STRUNG_POST] globally.
///
/// Use anywhere within the runtime:
/// ```
/// use strung::prelude::*;
/// fn main(){
/// strung::set_static("{","}");
/// // ...
/// }
/// // ...
/// ```
///
pub fn set_static(pre: &'static str, post: &'static str){
unsafe {
STRUNG_PRE = pre;
STRUNG_POST = post;
}
}
/// alias for [set_static]
pub fn static_global(pre: &'static str, post: &'static str){
set_static(pre,post)
}
}
pub use config::set_static;
pub mod prelude {
//! All needed goods!
pub use strung_derive::*;
pub use super::{Strung,StrungUnit};
pub use super::config::{STRUNG_PRE,STRUNG_POST};
}