stri
stri is a set of procedural macros for string interpolation.
Usage Example
use stri::{si, sql};
fn main(){
let name = "Ahmed";
let age = 63;
let height = 180.5;
assert_eq!(
si!("my name is {name}, i am {age} years old and my height is {height}"),
"my name is Ahmed, i am 63 years old and my height is 180.5",
);
let note = r#"My friend's name is Ali"#;
assert_eq!(
sql!("INSERT INTO users (name, age, height, note) VALUES ({name}, {age}, {height}, {note})"),
r#"INSERT INTO users (name, age, height, note) VALUES ('Ahmed', 63, 180.5, 'My friend''s name is Ali')"#,
);
let note = r#"[' " > < &]"#;
assert_eq!(
sql!("INSERT INTO users (name, age, height, note) VALUES ({name}, {age}, {height}, {~html note})"),
r#"INSERT INTO users (name, age, height, note) VALUES ('Ahmed', 63, 180.5, '[' " > < &]')"#,
);
use chrono::NaiveDateTime;
let dt = NaiveDateTime::parse_from_str("2015-09-05 23:56:04", "%Y-%m-%d %H:%M:%S").unwrap();
assert_eq!(
si!("date and time is: {dt}"),
"date and time is: 2015-09-05 23:56:04"
);
assert_eq!(
sql!("SELECT {dt}::TIMESTAMP AS date_time"),
"SELECT '2015-09-05 23:56:04'::TIMESTAMP AS date_time"
);
}
file! macro
For longer string or sql content, you can externalize it into a separate file and use the file! macro to include it. The file! macro intelligently applies the logic of the si! or sql! macros internally, determined by the extension of the specified file.
File: src/example.txt
my name is {name}, i am {age} years old and my height is {height}
File: src/example.sql
INSERT INTO users (name, age, height, note) VALUES ({name}, {age}, {height}, {note})
use stri::file;
fn main(){
let name = "Ahmed";
let age = 63;
let height = 180.5;
let note = r#"My friend's name is Ali"#;
assert_eq!(
file!("src/example.txt"),
"my name is Ahmed, i am 63 years old and my height is 180.5",
);
assert_eq!(
file!("src/example.sql"),
r#"INSERT INTO users (name, age, height, note) VALUES ('Ahmed', 63, 180.5, 'My friend''s name is Ali')"#,
);
}
Contribution
If you encounter any issues or want to suggest a feature, please open an issue in github.
License Information
"stri" is licensed under Ethical Use License (EUL v1.0). see LICENSE for full license details.