| Crates.io | codize |
| lib.rs | codize |
| version | 0.3.3 |
| created_at | 2023-04-19 06:12:29.319233+00 |
| updated_at | 2024-05-23 23:38:05.130693+00 |
| description | Simple, language-agnostic library that pretty-prints code for your code-generation tool. |
| homepage | |
| repository | https://github.com/Pistonite/codize |
| max_upload_size | |
| id | 843213 |
| size | 35,015 |
Simple, language-agnostic library that pretty-prints code for your code-generation tool.
First, create a [Code] enum with the code structure with one of the ways listed below.
Then, the [Format] struct can be used to format the output. Or you can simply use to_string
for a quick formatting with the default parameters
Code] ExamplesThe [Code] enum stores all of the code structures. You can create it in one of the following
ways:
String] or &str with into()cblock!] macroclist!] macrointo(),
or with the [cconcat!] macro which allows for mixing different types of code segmentsUsually, the macros will automatically convert the input to [Code] by calling Code::from.
use codize::{cblock, clist, cconcat};
let code = cconcat![
"",
"/// This block is auto-generated",
"",
cblock!("fn main() {", [
cblock!("println!(", [
clist!("," => [r#""{}, {}!""#, r#""Hello""#, r#""world!""#])
], ");")
], "}"),
];
let expected = r#"
/// This block is auto-generated
fn main() {
println!(
"{}, {}!",
"Hello",
"world!",
);
}"#;
assert_eq!(expected, code.to_string());
Format] ExamplesYou can use the [FormatCode] trait along with the [Format] struct to change global
formatting options
use codize::{cblock, Format, FormatCode};
let code = cblock!("fn main() {", ["println!(\"Hello, world!\");"], "}");
let indent_2 =
r#"fn main() {
println!("Hello, world!");
}"#;
assert_eq!(indent_2, code.format_with(&Format::indent(2)));
let indent_tab =
"fn main() {
\tprintln!(\"Hello, world!\");
}";
assert_eq!(indent_tab, code.format_with(&Format::indent_tab()));