| Crates.io | syntaxfmt |
| lib.rs | syntaxfmt |
| version | 0.2.2 |
| created_at | 2025-10-19 22:37:23.455724+00 |
| updated_at | 2025-11-01 18:38:49.082066+00 |
| description | A derive macro-based library for flexible syntax tree formatting with pretty printing support. |
| homepage | https://github.com/jngls/syntaxfmt |
| repository | https://github.com/jngls/syntaxfmt |
| max_upload_size | |
| id | 1891056 |
| size | 62,365 |
A derive macro-based library for flexible syntax tree formatting with pretty printing support.
syntaxfmt provides a trait and builder based approach to formatting syntax trees with both compact and pretty-printed output modes. It's designed for compiler frontends, code generators, and any application that needs to format structured data as text with dynamic formatting.
#[derive(SyntaxFmt)]derive (enabled by default) - Enables the SyntaxFmt derive macroAdd syntaxfmt to your Cargo.toml:
[dependencies]
syntaxfmt = "0.2.2"
The simplest use case is to derive SyntaxFmt on your types and they'll format themselves by printing each field in order:
use syntaxfmt::{SyntaxFmt, syntax_fmt};
#[derive(SyntaxFmt)]
struct BinaryOp<'src> {
left: &'src str,
op: &'src str,
right: &'src str,
}
let expr = BinaryOp { left: "x", op: "+", right: "y" };
assert_eq!(format!("{}", syntax_fmt(&expr)), "x+y");
Use pre (prefix) and suf (suffix) attributes to add syntax around fields:
use syntaxfmt::{SyntaxFmt, syntax_fmt};
#[derive(SyntaxFmt)]
#[syntax(pre = "let ", suf = ";")]
struct LetStatement<'src> {
name: &'src str,
#[syntax(pre = " = ")]
value: &'src str,
}
let stmt = LetStatement { name: "x", value: "42" };
assert_eq!(format!("{}", syntax_fmt(&stmt)), "let x = 42;");
Enable pretty printing with the .pretty() method. Use modal attributes (arrays) to specify different formatting for normal vs pretty mode:
use syntaxfmt::{SyntaxFmt, syntax_fmt};
#[derive(SyntaxFmt)]
struct FunctionCall<'src> {
name: &'src str,
#[syntax(pre = ["(", "( "], suf = [")", " )"], delim = [", ", ", "])]
args: Vec<&'src str>,
}
let call = FunctionCall {
name: "max",
args: vec!["x", "y", "z"],
};
assert_eq!(format!("{}", syntax_fmt(&call)), "max(x, y, z)");
assert_eq!(format!("{}", syntax_fmt(&call).pretty()), "max( x, y, z )");
More advanced pretty printing is available via Indentation and Layout.
For complete documentation including many more examples, visit docs.rs/syntaxfmt.
Attributes can be applied at the type, field, or syntax_else level.
| Argument | Description | Valid Location |
|---|---|---|
pre |
Text before content | field/type/else |
suf |
Text after content | field/type/else |
delim |
Separator between collection elements | field/type/else |
cont |
Literal replacement for field value | field/type/else |
cont_with |
Custom formatter function/closure | field/type/else |
eval |
Conditional expression | field/type |
eval_with |
Conditional function/closure | field/type |
nl |
Newline positions (beg, pre, cont, suf) |
field/type/else |
ind |
Increase indent level for field content | field/type/else |
skip |
Omit field from formatting | field/type |
state |
Specify state type (type-level only) | type |
bound |
Add trait bound to state (type-level only) | type |
Contributions are welcome! Please feel free to submit a Pull Request.
This project is dual licensed under:
Unless explicitly stated otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.