Giter VIP home page Giter VIP logo

syn-serde's Introduction

syn-serde

crates.io docs.rs license msrv github actions

Library to serialize and deserialize Syn syntax trees.

Usage

Add this to your Cargo.toml:

[dependencies]
syn-serde = "0.3"

Examples

[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!\""
                  }
                ]
              }
            }
          }
        ]
      }
    }
  ]
}

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.

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.

syn-serde's People

Contributors

alexcrichton avatar bors[bot] avatar carllerche avatar colin-kiegel avatar cramertj avatar dependabot[bot] avatar dtolnay avatar eijebong avatar gregkatz avatar hcpl avatar ignatenkobrain avatar killercup avatar kureuil avatar lukaslueg avatar mateon1 avatar matprec avatar mjbshaw avatar mystor avatar pepsighan avatar pilottesting avatar rantanen avatar sergiobenitez avatar sgrif avatar simonsapin avatar taiki-e avatar teddriggs avatar ubnt-intrepid avatar utkarshkukreti avatar white-oak avatar yijunyu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

syn-serde's Issues

Remove error from to_string / to_vec

The to_string and to_vec functions are currently defined to return Result<..., serde_json::Error>. serde_json guarantees that serializing to string will only fail if the type's Serialize impl decides to fail or if the type does not map to legal json, for example because of non-string keys. For syn-serde all of the data structures are designed to be compatible with json and their Serialize impls never decide to fail on their own, so it should be fine to unwrap the errors from serde_json::to_string and remove those errors from the API of syn-serde.

to_writer still needs to return Result because std::io is all fallible.

Location information on AST nodes

As mentioned in the issue on syn, for my usecase I need location information (ideally spans as offset + line + column) on each node to map it back to the source code.

Error while serialising AST

Hi, I'm able to generate AST for my code using syn crate. Now I'm trying to serialize the AST. while doing i'm getting an error for line println!("{}", json::to_string_pretty(&syntax));

error: the trait bound `syn::File: syn_serde::Syn` is not satisfied
label: the trait `syn_serde::Syn` is not implemented for `syn::File`

Here is my code

fn main() {
        let source = r#"
            fn main() {
                println!("hello");
            }
        "#;
        let syntax = syn::parse_file(source).unwrap();
        //println!("{:#?}\n", syntax); 
       // Convert AST to JSON
        println!("{}", json::to_string_pretty(&syntax));
}

@taiki-e How can I resolve this error?
Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.