#serde #serialization #syn

syn-serde

Library to serialize and deserialize Syn syntax trees

5 releases

0.2.3 Apr 6, 2021
0.2.2 Jan 5, 2021
0.2.1 Dec 29, 2020
0.2.0 Sep 16, 2019
0.0.0 Sep 7, 2019

#81 in Encoding

Download history 15/week @ 2020-12-24 23/week @ 2020-12-31 23/week @ 2021-01-07 23/week @ 2021-01-14 11/week @ 2021-01-21 10/week @ 2021-01-28 10/week @ 2021-02-04 40/week @ 2021-02-11 32/week @ 2021-02-18 17/week @ 2021-02-25 21/week @ 2021-03-04 47/week @ 2021-03-11 42/week @ 2021-03-18 43/week @ 2021-03-25 42/week @ 2021-04-01 38/week @ 2021-04-08

114 downloads per month
Used in 2 crates

Apache-2.0 OR MIT

225KB
6K SLoC

syn-serde

crates.io docs.rs license rustc build status

Library to serialize and deserialize Syn syntax trees.

Usage

Add this to your Cargo.toml:

[dependencies]
syn-serde = "0.2"

Compiler support: requires rustc 1.31+

Examples

[dependencies]
syn-serde = { version = "0.2", features = ["json"] }
syn = { version = "1", 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!\""
                  }
                ]
              }
            }
          }
        ]
      }
    }
  ]
}

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.2 is compatible with the data structures of Syn 1.0.

License

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.

Dependencies

~0.5–1.2MB
~28K SLoC