1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
// SPDX-License-Identifier: Apache-2.0 OR MIT
/*!
<!-- tidy:crate-doc:start -->
Library to serialize and deserialize [Syn] syntax trees.
## Usage
Add this to your `Cargo.toml`:
```toml
[dependencies]
syn-serde = "0.3"
```
*Compiler support: requires rustc 1.56+*
## 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
<!-- tidy:crate-doc:end -->
*/
#![doc(test(
no_crate_inject,
attr(
deny(warnings, rust_2018_idioms, single_use_lifetimes),
allow(dead_code, unused_variables)
)
))]
#![forbid(unsafe_code)]
#![warn(
rust_2018_idioms,
single_use_lifetimes,
unreachable_pub,
clippy::pedantic,
// Lints that may help when writing public library.
// missing_debug_implementations,
// missing_docs,
clippy::alloc_instead_of_core,
// clippy::exhaustive_enums, // TODO
// clippy::exhaustive_structs, // TODO
clippy::impl_trait_in_params,
// clippy::missing_inline_in_public_items,
// clippy::std_instead_of_alloc,
clippy::std_instead_of_core,
)]
#![allow(
clippy::enum_glob_use,
clippy::missing_errors_doc,
clippy::module_name_repetitions,
clippy::needless_doctest_main,
clippy::struct_excessive_bools,
clippy::used_underscore_binding,
clippy::wildcard_imports
)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#[macro_use]
mod macros;
#[path = "gen/ast_struct.rs"]
mod ast_struct;
#[path = "gen/ast_enum.rs"]
mod ast_enum;
#[path = "gen/convert.rs"]
mod convert;
mod attr {
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use crate::{
ast_enum::{AttrStyle, Meta},
ast_struct::{Attribute, MetaList, MetaNameValue},
};
}
#[doc(hidden)]
pub use crate::attr::{AttrStyle, Attribute, Meta, MetaList, MetaNameValue};
mod data;
pub(crate) use crate::data::assert_struct_semi;
#[doc(hidden)]
pub use crate::data::{Field, Fields, FieldsNamed, FieldsUnnamed, Variant};
mod expr;
#[doc(hidden)]
pub use crate::expr::{
Arm, Expr, ExprArray, ExprAssign, ExprAsync, ExprAwait, ExprBinary, ExprBlock, ExprBreak,
ExprCall, ExprCast, ExprClosure, ExprConst, ExprContinue, ExprField, ExprForLoop, ExprGroup,
ExprIf, ExprIndex, ExprInfer, ExprLet, ExprLit, ExprLoop, ExprMacro, ExprMatch, ExprMethodCall,
ExprParen, ExprPath, ExprRange, ExprReference, ExprRepeat, ExprReturn, ExprStruct, ExprTry,
ExprTryBlock, ExprTuple, ExprUnary, ExprUnsafe, ExprWhile, ExprYield, FieldValue, Index, Label,
Member, RangeLimits,
};
mod file {
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use crate::ast_struct::File;
}
#[doc(hidden)]
pub use crate::file::File;
mod generics;
#[doc(hidden)]
pub use crate::generics::{
BoundLifetimes, ConstParam, GenericParam, Generics, LifetimeParam, PredicateLifetime,
PredicateType, TraitBound, TraitBoundModifier, TypeParam, TypeParamBound, WhereClause,
WherePredicate,
};
mod item;
#[doc(hidden)]
pub use crate::item::{
FnArg, ForeignItem, ForeignItemFn, ForeignItemMacro, ForeignItemStatic, ForeignItemType,
ImplItem, ImplItemConst, ImplItemFn, ImplItemMacro, ImplItemType, ImplRestriction, Item,
ItemConst, ItemEnum, ItemExternCrate, ItemFn, ItemForeignMod, ItemImpl, ItemMacro, ItemMod,
ItemStatic, ItemStruct, ItemTrait, ItemTraitAlias, ItemType, ItemUnion, ItemUse, Receiver,
Signature, StaticMutability, TraitItem, TraitItemConst, TraitItemFn, TraitItemMacro,
TraitItemType, UseGroup, UseName, UsePath, UseRename, UseTree, Variadic,
};
mod lifetime {
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use crate::ast_struct::Lifetime;
}
#[doc(hidden)]
pub use crate::lifetime::Lifetime;
mod lit;
#[doc(hidden)]
pub use crate::lit::{
Lit, LitBool, LitByte, LitByteStr, LitChar, LitFloat, LitInt, LitStr, StrStyle,
};
mod mac {
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use crate::{ast_enum::MacroDelimiter, ast_struct::Macro};
}
#[doc(hidden)]
pub use crate::mac::{Macro, MacroDelimiter};
mod op {
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use crate::ast_enum::{BinOp, UnOp};
}
#[doc(hidden)]
pub use crate::op::{BinOp, UnOp};
mod pat;
#[doc(hidden)]
pub use crate::expr::{
ExprConst as PatConst, ExprLit as PatLit, ExprMacro as PatMacro, ExprPath as PatPath,
ExprRange as PatRange,
};
#[doc(hidden)]
pub use crate::pat::{
FieldPat, Pat, PatIdent, PatOr, PatParen, PatReference, PatRest, PatSlice, PatStruct, PatTuple,
PatTupleStruct, PatType, PatWild,
};
mod path;
#[doc(hidden)]
pub use crate::path::{
AngleBracketedGenericArguments, AssocConst, AssocType, Constraint, GenericArgument,
ParenthesizedGenericArguments, Path, PathArguments, PathSegment, QSelf,
};
mod restriction;
#[doc(hidden)]
pub use crate::restriction::{FieldMutability, VisRestricted, Visibility};
mod stmt;
#[doc(hidden)]
pub use crate::stmt::{Block, Local, LocalInit, Stmt, StmtMacro};
mod ty;
#[doc(hidden)]
pub use crate::ty::{
Abi, BareFnArg, BareVariadic, ReturnType, Type, TypeArray, TypeBareFn, TypeGroup,
TypeImplTrait, TypeMacro, TypeParen, TypePath, TypePtr, TypeReference, TypeSlice,
TypeTraitObject, TypeTuple,
};
mod token_stream;
#[doc(hidden)]
pub use crate::token_stream::{
Delimiter, Group, Ident, Literal, Punct, Spacing, TokenStream, TokenTree,
};
#[cfg(feature = "json")]
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
pub mod json;
mod sealed {
pub trait Sealed {}
}
// =============================================================================
// Syn trait
/// A trait for the data structures of [Syn] and [proc-macro2].
///
/// [Syn]: https://github.com/dtolnay/syn
/// [proc-macro2]: https://github.com/alexcrichton/proc-macro2
#[allow(single_use_lifetimes)] // https://github.com/rust-lang/rust/issues/55058
pub trait Syn: Sized + sealed::Sealed {
type Adapter: Serialize + for<'de> Deserialize<'de>;
/// Converts a `Syn` type into an adapter.
///
/// # Examples
///
/// ```rust
/// # #[cfg(feature = "json")]
/// # fn dox() {
/// use syn_serde::Syn;
///
/// let syn_file: syn::File = syn::parse_quote! {
/// fn main() {
/// println!("Hello, world!");
/// }
/// };
///
/// let serializable_file = syn_file.to_adapter();
/// println!("{}", serde_json::to_string_pretty(&serializable_file).unwrap());
/// # }
/// ```
fn to_adapter(&self) -> Self::Adapter;
/// Converts an adapter into a `Syn` type.
///
/// # Examples
///
/// ```rust
/// # #[cfg(feature = "json")]
/// # fn dox() -> Result<(), Box<dyn std::error::Error>> {
/// use syn_serde::Syn;
///
/// // `struct Unit;`
/// let json = r#"{
/// "struct": {
/// "ident": "Unit",
/// "fields": "unit"
/// }
/// }"#;
///
/// let serializable_file: <syn::File as Syn>::Adapter = serde_json::from_str(json)?;
/// let syn_file = syn::File::from_adapter(&serializable_file);
/// # Ok(())
/// # }
/// ```
fn from_adapter(adapter: &Self::Adapter) -> Self;
}
// =============================================================================
use core::ops;
use proc_macro2::Span;
use serde::{de::Deserialize, ser::Serialize};
use serde_derive::{Deserialize, Serialize};
type Punctuated<T> = Vec<T>;
fn default<T>() -> T
where
T: Default,
{
T::default()
}
fn default_or_none<T>(x: bool) -> Option<T>
where
T: Default,
{
if x {
Some(T::default())
} else {
None
}
}
fn not<T>(x: T) -> T::Output
where
T: ops::Not,
{
!x
}
// https://github.com/rust-lang/rust/issues/51443
trait RefInto<U>: Sized {
fn ref_into<'a>(&'a self) -> U
where
&'a Self: Into<U>,
{
self.into()
}
}
impl<T, U> RefInto<U> for T {}
trait MapInto<U, M> {
type T;
fn ref_map<'a, F>(&'a self, f: F) -> M
where
Self::T: 'a,
F: FnMut(&'a Self::T) -> U;
fn map_into<'a>(&'a self) -> M
where
Self::T: 'a,
&'a Self::T: Into<U>,
{
self.ref_map(Into::into)
}
}
impl<T, U> MapInto<U, Vec<U>> for Vec<T> {
type T = T;
fn ref_map<'a, F>(&'a self, f: F) -> Vec<U>
where
F: FnMut(&'a Self::T) -> U,
{
self.iter().map(f).collect()
}
}
impl<T, U, P> MapInto<U, syn::punctuated::Punctuated<U, P>> for Vec<T>
where
P: Default,
{
type T = T;
fn ref_map<'a, F>(&'a self, f: F) -> syn::punctuated::Punctuated<U, P>
where
F: FnMut(&'a Self::T) -> U,
{
self.iter().map(f).collect()
}
}
impl<T, U, P> MapInto<U, Vec<U>> for syn::punctuated::Punctuated<T, P>
where
P: Default,
{
type T = T;
fn ref_map<'a, F>(&'a self, f: F) -> Vec<U>
where
F: FnMut(&'a Self::T) -> U,
{
self.iter().map(f).collect()
}
}
impl<T, U> MapInto<U, Option<U>> for Option<T> {
type T = T;
fn ref_map<'a, F>(&'a self, f: F) -> Option<U>
where
F: FnMut(&'a Self::T) -> U,
{
self.as_ref().map(f)
}
}
impl<T, U> MapInto<U, Box<U>> for Box<T> {
type T = T;
fn ref_map<'a, F>(&'a self, mut f: F) -> Box<U>
where
F: FnMut(&'a Self::T) -> U,
{
Box::new(f(self))
}
}