https://github.com/taiki-e/syn-serde
Library to serialize and deserialize Syn syntax trees.
https://github.com/taiki-e/syn-serde
proc-macro rust serde
Last synced: 11 days ago
JSON representation
Library to serialize and deserialize Syn syntax trees.
- Host: GitHub
- URL: https://github.com/taiki-e/syn-serde
- Owner: taiki-e
- License: apache-2.0
- Created: 2019-03-09T15:01:29.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2024-10-25T13:56:49.000Z (12 months ago)
- Last Synced: 2024-10-25T17:58:14.402Z (12 months ago)
- Topics: proc-macro, rust, serde
- Language: Rust
- Homepage: https://docs.rs/syn-serde
- Size: 4.83 MB
- Stars: 24
- Watchers: 4
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# syn-serde
[](https://crates.io/crates/syn-serde)
[](https://docs.rs/syn-serde)
[](#license)
[](https://www.rust-lang.org)
[](https://github.com/taiki-e/syn-serde/actions)Library to serialize and deserialize [Syn] syntax trees.
## Usage
Add this to your `Cargo.toml`:
```toml
[dependencies]
syn-serde = "0.3"
```## Examples
```toml
[dependencies]
syn-serde = { version = "0.3", features = ["json"] }
syn = { version = "2", features = ["full"] }
``````rust
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:
```json
{
"items": [
{
"fn": {
"ident": "main",
"inputs": [],
"output": null,
"stmts": [
{
"semi": {
"macro": {
"path": {
"segments": [
{
"ident": "println"
}
]
},
"delimiter": "paren",
"tokens": [
{
"lit": "\"Hello, world!\""
}
]
}
}
}
]
}
}
]
}
```### Rust source file -> JSON representation of the syntax tree
The [`rust2json`] example parse a Rust source file into a `syn_serde::File`
and print out a JSON representation of the syntax tree.### JSON file -> Rust syntax tree
The [`json2rust`] example parse a JSON file into a `syn_serde::File` and
print out a Rust syntax tree.## Optional features
- **`json`** — Provides functions for JSON <-> Rust serializing and
deserializing.## Relationship to Syn
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.[Syn]: https://github.com/dtolnay/syn
[proc-macro2]: https://github.com/alexcrichton/proc-macro2
[`rust2json`]: https://github.com/taiki-e/syn-serde/tree/HEAD/examples/rust2json
[`json2rust`]: https://github.com/taiki-e/syn-serde/tree/HEAD/examples/json2rust## License
Licensed under either of [Apache License, Version 2.0](LICENSE-APACHE) or
[MIT license](LICENSE-MIT) 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.