David Tolnay | ad2836d | 2017-04-20 10:11:43 -0700 | [diff] [blame] | 1 | #![doc(html_root_url = "https://dtolnay.github.io/syn")] |
| 2 | |
David Tolnay | 02a8d47 | 2017-02-19 12:59:44 -0800 | [diff] [blame] | 3 | #![cfg_attr(feature = "cargo-clippy", allow(large_enum_variant))] |
David Tolnay | aed77b0 | 2016-09-23 20:50:31 -0700 | [diff] [blame] | 4 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 5 | extern crate proc_macro; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 6 | extern crate proc_macro2; |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 7 | extern crate unicode_xid; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 8 | |
Nika Layzell | 7f5fc68 | 2017-10-25 00:47:10 -0400 | [diff] [blame] | 9 | #[cfg(any(feature = "printing", feature = "parsing"))] |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 10 | extern crate quote; |
| 11 | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 12 | #[macro_use] |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 13 | extern crate synom; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 14 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 15 | #[macro_use] |
| 16 | mod macros; |
| 17 | |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 18 | mod attr; |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 19 | pub use attr::{Attribute, AttrStyle, MetaItem, NestedMetaItem, MetaItemList, |
| 20 | MetaNameValue}; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 21 | |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 22 | mod data; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 23 | pub use data::{Field, Variant, VariantData, Visibility, VisRestricted, VisCrate, |
| 24 | VisPublic, VisInherited}; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 25 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 26 | mod expr; |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 27 | pub use expr::{Expr, ExprKind, ExprBox, ExprInPlace, ExprArray, ExprCall, |
| 28 | ExprMethodCall, ExprTup, ExprBinary, ExprUnary, ExprCast, |
| 29 | ExprType, ExprIf, ExprIfLet, ExprWhile, ExprWhileLet, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 30 | ExprForLoop, ExprLoop, ExprMatch, ExprClosure, ExprBlock, |
| 31 | ExprAssign, ExprAssignOp, ExprField, ExprTupField, ExprIndex, |
| 32 | ExprRange, ExprPath, ExprAddrOf, ExprBreak, ExprContinue, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 33 | ExprRet, ExprStruct, ExprRepeat, ExprParen, ExprTry, ExprCatch, |
Nika Layzell | 42bf7e3 | 2017-10-24 23:10:16 -0400 | [diff] [blame] | 34 | ExprGroup, ExprYield}; |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 35 | |
| 36 | #[cfg(feature = "full")] |
| 37 | pub use expr::{Arm, BindingMode, Block, CaptureBy, FieldPat, FieldValue, Local, |
| 38 | MacStmtStyle, Pat, RangeLimits, Stmt, PatIdent, PatWild, |
| 39 | PatStruct, PatTuple, PatTupleStruct, PatPath, PatBox, PatRef, |
| 40 | PatLit, PatRange, PatSlice, InPlaceKind}; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 41 | |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 42 | mod generics; |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame^] | 43 | pub use generics::{Generics, LifetimeDef, TraitBoundModifier, TypeParam, TypeParamBound, |
David Tolnay | fa23f57 | 2017-01-23 00:19:11 -0800 | [diff] [blame] | 44 | WhereBoundPredicate, WhereClause, WhereEqPredicate, WherePredicate, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 45 | WhereRegionPredicate, BoundLifetimes}; |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 46 | #[cfg(feature = "printing")] |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame^] | 47 | pub use generics::{ImplGenerics, Turbofish, TypeGenerics}; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 48 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 49 | mod ident; |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 50 | pub use ident::Ident; |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 51 | |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 52 | #[cfg(feature = "full")] |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 53 | mod item; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 54 | #[cfg(feature = "full")] |
David Tolnay | 8894f60 | 2017-11-11 12:11:04 -0800 | [diff] [blame] | 55 | pub use item::{Constness, Defaultness, FnArg, FnDecl, ForeignItem, ItemForeignMod, |
David Tolnay | 857628c | 2017-11-11 12:25:31 -0800 | [diff] [blame] | 56 | ImplItem, ImplPolarity, Item, MethodSig, PathListItem, |
David Tolnay | da705bd | 2017-11-10 21:58:05 -0800 | [diff] [blame] | 57 | TraitItem, ViewPath, ItemExternCrate, ItemUse, |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame^] | 58 | ItemStatic, ItemConst, ItemFn, ItemMacro, ItemMod, ItemType, ItemEnum, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 59 | ItemStruct, ItemUnion, ItemTrait, ItemDefaultImpl, ItemImpl, |
| 60 | PathSimple, PathGlob, PathList, ForeignItemFn, ForeignItemStatic, |
David Tolnay | decf28d | 2017-11-11 11:56:45 -0800 | [diff] [blame] | 61 | TraitItemConst, TraitItemMacro, TraitItemMethod, TraitItemType, |
David Tolnay | 857628c | 2017-11-11 12:25:31 -0800 | [diff] [blame] | 62 | ImplItemConst, ImplItemMacro, ImplItemMethod, ImplItemType, ArgSelfRef, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 63 | ArgSelf, ArgCaptured}; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 64 | |
David Tolnay | 631cb8c | 2016-11-10 17:16:41 -0800 | [diff] [blame] | 65 | #[cfg(feature = "full")] |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 66 | mod file; |
David Tolnay | 631cb8c | 2016-11-10 17:16:41 -0800 | [diff] [blame] | 67 | #[cfg(feature = "full")] |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 68 | pub use file::File; |
David Tolnay | 631cb8c | 2016-11-10 17:16:41 -0800 | [diff] [blame] | 69 | |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 70 | mod lifetime; |
| 71 | pub use lifetime::Lifetime; |
| 72 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 73 | mod lit; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 74 | pub use lit::{Lit, LitKind}; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 75 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 76 | mod mac; |
David Tolnay | decf28d | 2017-11-11 11:56:45 -0800 | [diff] [blame] | 77 | pub use mac::{Macro, TokenTree}; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 78 | |
David Tolnay | 0e83740 | 2016-12-22 17:25:55 -0500 | [diff] [blame] | 79 | mod derive; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 80 | pub use derive::{Body, DeriveInput, BodyEnum, BodyStruct}; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 81 | |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 82 | mod op; |
| 83 | pub use op::{BinOp, UnOp}; |
| 84 | |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 85 | mod ty; |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame^] | 86 | pub use ty::{Abi, AbiKind, AngleBracketedParameterData, BareFnArg, BareFnArgName, BareFnType, |
| 87 | ReturnType, MutType, Mutability, ParenthesizedParameterData, Path, |
| 88 | PathParameters, PathSegment, PolyTraitRef, QSelf, Type, TypeBinding, Unsafety, |
| 89 | TypeSlice, TypeArray, TypePtr, TypeRptr, TypeBareFn, TypeNever, TypeTup, TypePath, |
| 90 | TypeTraitObject, TypeImplTrait, TypeParen, TypeInfer, TypeGroup}; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 91 | #[cfg(feature = "printing")] |
| 92 | pub use ty::PathTokens; |
| 93 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 94 | pub use synom::span::Span; |
| 95 | pub use synom::tokens; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 96 | pub use synom::delimited; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 97 | |
Nika Layzell | a6f46c4 | 2017-10-26 15:26:16 -0400 | [diff] [blame] | 98 | mod gen { |
| 99 | #[cfg(feature = "visit")] |
| 100 | pub mod visit; |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 101 | |
Nika Layzell | a6f46c4 | 2017-10-26 15:26:16 -0400 | [diff] [blame] | 102 | #[cfg(feature = "visit_mut")] |
| 103 | pub mod visit_mut; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 104 | |
Nika Layzell | a6f46c4 | 2017-10-26 15:26:16 -0400 | [diff] [blame] | 105 | #[cfg(feature = "fold")] |
| 106 | pub mod fold; |
| 107 | } |
| 108 | pub use gen::*; |
gnzlbg | 9ae88d8 | 2017-01-26 20:45:17 +0100 | [diff] [blame] | 109 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 110 | //////////////////////////////////////////////////////////////////////////////// |
| 111 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 112 | #[cfg(feature = "parsing")] |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 113 | pub use synom::ParseError; |
Ted Driggs | 054abbb | 2017-05-01 12:20:52 -0700 | [diff] [blame] | 114 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 115 | #[cfg(feature = "parsing")] |
| 116 | use synom::{Synom, SynomBuffer}; |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 117 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 118 | /// Parse tokens of source code into the chosen syn data type. |
| 119 | /// |
| 120 | /// This is preferred over parsing a string because tokens are able to preserve |
| 121 | /// information about where in the user's code they were originally written (the |
| 122 | /// "span" of the token), possibly allowing the compiler to produce better error |
| 123 | /// messages. |
| 124 | /// |
| 125 | /// # Examples |
| 126 | /// |
| 127 | /// ```rust,ignore |
| 128 | /// extern crate proc_macro; |
| 129 | /// use proc_macro::TokenStream; |
| 130 | /// |
| 131 | /// extern crate syn; |
| 132 | /// |
| 133 | /// #[macro_use] |
| 134 | /// extern crate quote; |
| 135 | /// |
| 136 | /// use syn::DeriveInput; |
| 137 | /// |
| 138 | /// #[proc_macro_derive(MyMacro)] |
| 139 | /// pub fn my_macro(input: TokenStream) -> TokenStream { |
| 140 | /// // Parse the tokens into a syntax tree |
| 141 | /// let ast: DeriveInput = syn::parse(input).unwrap(); |
| 142 | /// |
| 143 | /// // Build the output, possibly using quasi-quotation |
| 144 | /// let expanded = quote! { |
| 145 | /// /* ... */ |
| 146 | /// }; |
| 147 | /// |
| 148 | /// // Parse back to a token stream and return it |
| 149 | /// expanded.parse().unwrap() |
| 150 | /// } |
| 151 | /// ``` |
| 152 | #[cfg(feature = "parsing")] |
| 153 | pub fn parse<T>(tokens: proc_macro::TokenStream) -> Result<T, ParseError> |
| 154 | where T: Synom, |
| 155 | { |
| 156 | _parse(tokens.into()) |
| 157 | } |
| 158 | |
| 159 | #[cfg(feature = "parsing")] |
| 160 | fn _parse<T>(tokens: proc_macro2::TokenStream) -> Result<T, ParseError> |
| 161 | where T: Synom, |
| 162 | { |
| 163 | let buf = SynomBuffer::new(tokens); |
| 164 | let result = T::parse(buf.begin()); |
| 165 | let err = match result { |
| 166 | Ok((rest, t)) => { |
| 167 | if rest.eof() { |
| 168 | return Ok(t); |
| 169 | } else if rest == buf.begin() { |
| 170 | // parsed nothing |
| 171 | ParseError::new("failed to parse anything") |
| 172 | } else { |
| 173 | ParseError::new("failed to parse all tokens") |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 174 | } |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 175 | } |
| 176 | Err(err) => err, |
| 177 | }; |
| 178 | match T::description() { |
Alex Crichton | c1b76f5 | 2017-07-06 15:04:24 -0700 | [diff] [blame] | 179 | Some(s) => Err(ParseError::new(format!("failed to parse {}: {}", s, err))), |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 180 | None => Err(err), |
| 181 | } |
| 182 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 183 | |
Nika Layzell | cda7ebd | 2017-10-24 23:10:44 -0400 | [diff] [blame] | 184 | /// Parse a `quote::Tokens` of Rust code into the chosen syn data type. |
| 185 | /// |
| 186 | /// # Examples |
| 187 | /// |
| 188 | /// ```rust |
| 189 | /// extern crate syn; |
| 190 | /// # |
| 191 | /// # #[macro_use] |
Nika Layzell | cda7ebd | 2017-10-24 23:10:44 -0400 | [diff] [blame] | 192 | /// # extern crate quote; |
David Tolnay | 9174b97 | 2017-11-09 22:27:50 -0800 | [diff] [blame] | 193 | /// # |
| 194 | /// # type Result<T> = std::result::Result<T, Box<std::error::Error>>; |
Nika Layzell | cda7ebd | 2017-10-24 23:10:44 -0400 | [diff] [blame] | 195 | /// |
| 196 | /// use syn::Expr; |
Nika Layzell | cda7ebd | 2017-10-24 23:10:44 -0400 | [diff] [blame] | 197 | /// |
| 198 | /// fn run() -> Result<()> { |
| 199 | /// let code = quote!(assert_eq!(u8::max_value(), 255)); |
| 200 | /// let expr = syn::parse_tokens::<Expr>(code)?; |
| 201 | /// println!("{:#?}", expr); |
| 202 | /// Ok(()) |
| 203 | /// } |
| 204 | /// # |
| 205 | /// # fn main() { run().unwrap() } |
| 206 | /// ``` |
| 207 | #[cfg(feature = "parsing")] |
| 208 | pub fn parse_tokens<T: Synom>(tokens: quote::Tokens) -> Result<T, ParseError> { |
| 209 | _parse(tokens.into()) |
| 210 | } |
| 211 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 212 | /// Parse a string of Rust code into the chosen syn data type. |
| 213 | /// |
| 214 | /// # Examples |
| 215 | /// |
| 216 | /// ```rust |
| 217 | /// extern crate syn; |
| 218 | /// # |
David Tolnay | 9174b97 | 2017-11-09 22:27:50 -0800 | [diff] [blame] | 219 | /// # |
| 220 | /// # type Result<T> = std::result::Result<T, Box<std::error::Error>>; |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 221 | /// |
| 222 | /// use syn::Expr; |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 223 | /// |
| 224 | /// fn run() -> Result<()> { |
| 225 | /// let code = "assert_eq!(u8::max_value(), 255)"; |
| 226 | /// let expr = syn::parse_str::<Expr>(code)?; |
| 227 | /// println!("{:#?}", expr); |
| 228 | /// Ok(()) |
| 229 | /// } |
| 230 | /// # |
| 231 | /// # fn main() { run().unwrap() } |
| 232 | /// ``` |
| 233 | #[cfg(feature = "parsing")] |
| 234 | pub fn parse_str<T: Synom>(s: &str) -> Result<T, ParseError> { |
| 235 | _parse(s.parse()?) |
| 236 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 237 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 238 | // FIXME the name parse_file makes it sound like you might pass in a path to a |
| 239 | // file, rather than the content. |
| 240 | /// Parse the content of a file of Rust code. |
| 241 | /// |
| 242 | /// This is different from `syn::parse_str::<File>(content)` in two ways: |
| 243 | /// |
| 244 | /// - It discards a leading byte order mark `\u{FEFF}` if the file has one. |
| 245 | /// - It preserves the shebang line of the file, such as `#!/usr/bin/env rustx`. |
| 246 | /// |
| 247 | /// If present, either of these would be an error using `from_str`. |
| 248 | /// |
| 249 | /// # Examples |
| 250 | /// |
| 251 | /// ```rust,no_run |
| 252 | /// extern crate syn; |
| 253 | /// # |
David Tolnay | 9174b97 | 2017-11-09 22:27:50 -0800 | [diff] [blame] | 254 | /// # |
| 255 | /// # type Result<T> = std::result::Result<T, Box<std::error::Error>>; |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 256 | /// |
| 257 | /// use std::fs::File; |
| 258 | /// use std::io::Read; |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 259 | /// |
| 260 | /// fn run() -> Result<()> { |
| 261 | /// let mut file = File::open("path/to/code.rs")?; |
| 262 | /// let mut content = String::new(); |
| 263 | /// file.read_to_string(&mut content)?; |
| 264 | /// |
| 265 | /// let ast = syn::parse_file(&content)?; |
| 266 | /// if let Some(shebang) = ast.shebang { |
| 267 | /// println!("{}", shebang); |
| 268 | /// } |
| 269 | /// println!("{} items", ast.items.len()); |
| 270 | /// |
| 271 | /// Ok(()) |
| 272 | /// } |
| 273 | /// # |
| 274 | /// # fn main() { run().unwrap() } |
| 275 | /// ``` |
| 276 | #[cfg(all(feature = "parsing", feature = "full"))] |
| 277 | pub fn parse_file(mut content: &str) -> Result<File, ParseError> { |
| 278 | // Strip the BOM if it is present |
| 279 | const BOM: &'static str = "\u{feff}"; |
| 280 | if content.starts_with(BOM) { |
| 281 | content = &content[BOM.len()..]; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 282 | } |
Michael Layzell | 5e107ff | 2017-01-24 19:58:39 -0500 | [diff] [blame] | 283 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 284 | let mut shebang = None; |
| 285 | if content.starts_with("#!") && !content.starts_with("#![") { |
| 286 | if let Some(idx) = content.find('\n') { |
| 287 | shebang = Some(content[..idx].to_string()); |
| 288 | content = &content[idx..]; |
| 289 | } else { |
| 290 | shebang = Some(content.to_string()); |
| 291 | content = ""; |
| 292 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 293 | } |
David Tolnay | 0a8972b | 2017-02-27 02:10:01 -0800 | [diff] [blame] | 294 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 295 | let mut file: File = parse_str(content)?; |
| 296 | file.shebang = shebang; |
| 297 | Ok(file) |
Michael Layzell | 5e107ff | 2017-01-24 19:58:39 -0500 | [diff] [blame] | 298 | } |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 299 | |
| 300 | #[cfg(feature = "printing")] |
| 301 | struct TokensOrDefault<'a, T: 'a>(&'a Option<T>); |
| 302 | |
| 303 | #[cfg(feature = "printing")] |
| 304 | impl<'a, T> quote::ToTokens for TokensOrDefault<'a, T> |
| 305 | where T: quote::ToTokens + Default, |
| 306 | { |
| 307 | fn to_tokens(&self, tokens: &mut quote::Tokens) { |
| 308 | match *self.0 { |
| 309 | Some(ref t) => t.to_tokens(tokens), |
| 310 | None => T::default().to_tokens(tokens), |
| 311 | } |
| 312 | } |
| 313 | } |