#xml #syn #serialization #serde #syntax-tree #xml-format #rust

app rust2xml

Library to serialize Syn syntax trees into XML format

1 unstable release

0.0.1 Jan 6, 2021

#34 in #xml-format

Apache-2.0 OR MIT

17KB
276 lines

Parse a Rust source file into a syn_serde::File and print out an XML representation of the abstract syntax tree using quick-xml with a patch.

git clone https://github.com/tafia/quick-xml
cp var.rs quick-xml/src/se/var.rs
cargo install --path .
rust2xml src/main.rs > main.xml

The output file main.xml should contains the followling information:

<File><attrs><inner/><path><segments ident="warn"/></path><group><Group><parenthesis/><ident>rust_2018_idioms</ident><punct><Punct op=","><alone/></Punct></punct><ident>single_use_lifetimes</ident></Group></group></attrs><use><ItemUse><path><UsePath ident="std"><group><ident>env</ident><ident>fs</ident><path><UsePath ident="path"><ident>PathBuf</ident></UsePath></path></group></UsePath></path></ItemUse></use><mod><ItemMod ident="xml" semi="true"/></mod><type><ItemType ident="Result"><generics><type><TypeParam ident="T"/></type></generics><path><segments><PathSegment ident="std"/><PathSegment ident="result"/><PathSegment ident="Result"><angle_bracketed><AngleBracketedGenericArguments><type><path><segments><PathSegment ident="T"/></segments></path></type><type><path><segments><PathSegment ident="Box"><angle_bracketed><AngleBracketedGenericArguments><type><trait_object><TypeTraitObject dyn="true"><trait><TraitBound><path><segments ident="std"/><segments ident="error"/><segments ident="Error"/></path></TraitBound></trait></TypeTraitObject></trait_object></type></AngleBracketedGenericArguments></angle_bracketed></PathSegment></segments></path></type></AngleBracketedGenericArguments></angle_bracketed></PathSegment></segments></path></ItemType></type><fn><ident>main</ident><inputs></inputs><output></output><stmts><expr><if><ExprIf><let><ExprLet><tuple_struct><PatTupleStruct><path><segments ident="Err"/></path><pat><ident><PatIdent ident="e"/></ident></pat></PatTupleStruct></tuple_struct><call><ExprCall><path><segments><PathSegment ident="try_main"/></segments></path></ExprCall></call></ExprLet></let><semi><macro><path><Path><segments ident="eprintln"/></Path></path><delimiter><paren/></delimiter><tokens><lit>&quot;{}&quot;</lit><punct><Punct op=","><alone/></Punct></punct><ident>e</ident></tokens></macro></semi><semi><call><ExprCall><path><segments><PathSegment ident="std"/><PathSegment ident="process"/><PathSegment ident="exit"/></segments></path><lit>1</lit></ExprCall></call></semi></ExprIf></if></expr></stmts></fn><fn><ident>try_main</ident><inputs></inputs><output><path><segments><PathSegment ident="Result"><angle_bracketed><AngleBracketedGenericArguments><type><tuple><TypeTuple/></tuple></type></AngleBracketedGenericArguments></angle_bracketed></PathSegment></segments></path></output><stmts><let><Local><ident><PatIdent mut="true" ident="args"/></ident><call><ExprCall><path><segments><PathSegment ident="env"/><PathSegment ident="args_os"/></segments></path></ExprCall></call></Local></let><let><Local><_><PatWild/></_><method_call><ExprMethodCall method="next"><path><segments><PathSegment ident="args"/></segments></path></ExprMethodCall></method_call></Local></let><let><Local><ident><PatIdent ident="filepath"/></ident><match><ExprMatch><tuple><ExprTuple><method_call><ExprMethodCall method="next"><path><segments><PathSegment ident="args"/></segments></path></ExprMethodCall></method_call><method_call><ExprMethodCall method="next"><path><segments><PathSegment ident="args"/></segments></path></ExprMethodCall></method_call></ExprTuple></tuple><arms><tuple><PatTuple><tuple_struct><PatTupleStruct><path><segments ident="Some"/></path><pat><ident><PatIdent ident="arg"/></ident></pat></PatTupleStruct></tuple_struct><ident><PatIdent ident="None"/></ident></PatTuple></tuple><call><ExprCall><path><segments><PathSegment ident="PathBuf"/><PathSegment ident="from"/></segments></path><path><segments><PathSegment ident="arg"/></segments></path></ExprCall></call></arms><arms><_><PatWild/></_><block><ExprBlock><semi><macro><path><Path><segments ident="println"/></Path></path><delimiter><paren/></delimiter><tokens><lit>&quot;Usage: rust2xml path/to/filename.rs&quot;</lit></tokens></macro></semi><semi><return><ExprReturn><call><ExprCall><path><segments><PathSegment ident="Ok"/></segments></path><tuple><ExprTuple/></tuple></ExprCall></call></ExprReturn></return></semi></ExprBlock></block></arms></ExprMatch></match></Local></let><let><Local><ident><PatIdent ident="code"/></ident><try><ExprTry><call><ExprCall><path><segments><PathSegment ident="fs"/><PathSegment ident="read_to_string"/></segments></path><reference><ExprReference><path><segments><PathSegment ident="filepath"/></segments></path></ExprReference></reference></ExprCall></call></ExprTry></try></Local></let><let><Local><ident><PatIdent ident="syntax"/></ident><try><ExprTry><call><ExprCall><path><segments><PathSegment ident="syn"/><PathSegment ident="parse_file"/></segments></path><reference><ExprReference><path><segments><PathSegment ident="code"/></segments></path></ExprReference></reference></ExprCall></call></ExprTry></try></Local></let><semi><macro><path><Path><segments ident="println"/></Path></path><delimiter><paren/></delimiter><tokens><lit>&quot;{}&quot;</lit><punct><Punct op=","><alone/></Punct></punct><ident>xml</ident><punct><Punct op=":"><joint/></Punct></punct><punct><Punct op=":"><alone/></Punct></punct><ident>to_string</ident><group><Group><parenthesis/><punct><Punct op="&amp;"><alone/></Punct></punct><ident>syntax</ident></Group></group></tokens></macro></semi><expr><call><ExprCall><path><segments><PathSegment ident="Ok"/></segments></path><tuple><ExprTuple/></tuple></ExprCall></call></expr></stmts></fn></File>

Dependencies

~3MB
~66K SLoC