Crates.io | syn-serde |
lib.rs | syn-serde |
version | 0.3.1 |
source | src |
created_at | 2019-09-07 21:16:39.101446 |
updated_at | 2023-10-18 14:35:58.470599 |
description | Library to serialize and deserialize Syn syntax trees. |
homepage | |
repository | https://github.com/taiki-e/syn-serde |
max_upload_size | |
id | 163069 |
size | 258,506 |
Library to serialize and deserialize Syn syntax trees.
Add this to your Cargo.toml
:
[dependencies]
syn-serde = "0.3"
Compiler support: requires rustc 1.56+
[dependencies]
syn-serde = { version = "0.3", features = ["json"] }
syn = { version = "2", features = ["full"] }
use syn_serde::json;
let syn_file: syn::File = syn::parse_quote! {
fn main() {
println!("Hello, world!");
}
};
println!("{}", json::to_string_pretty(&syn_file));
This prints the following JSON:
{
"items": [
{
"fn": {
"ident": "main",
"inputs": [],
"output": null,
"stmts": [
{
"semi": {
"macro": {
"path": {
"segments": [
{
"ident": "println"
}
]
},
"delimiter": "paren",
"tokens": [
{
"lit": "\"Hello, world!\""
}
]
}
}
}
]
}
}
]
}
The rust2json
example parse a Rust source file into a syn_serde::File
and print out a JSON representation of the syntax tree.
The json2rust
example parse a JSON file into a syn_serde::File
and
print out a Rust syntax tree.
json
— Provides functions for JSON <-> Rust serializing and
deserializing.syn-serde is a fork of Syn, and syn-serde provides a set of data structures similar but not identical to Syn. All data structures provided by syn-serde can be converted to the data structures of Syn and proc-macro2.
The data structures of syn-serde 0.3 is compatible with the data structures of Syn 2.x.
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 the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.