mirror of
https://gitee.com/openharmony/third_party_rust_syn
synced 2024-11-27 01:41:05 +00:00
Emit fields as a json map of name to type
This commit is contained in:
parent
6da9cf0cab
commit
14d463e089
@ -9,6 +9,7 @@ publish = false # this is an internal crate which should never be published
|
||||
syn = { path = "..", features = ["full", "extra-traits"] }
|
||||
quote = "0.6"
|
||||
failure = "0.1"
|
||||
indexmap = { version = "1.0", features = ["serde-1"] }
|
||||
inflections = "1.1"
|
||||
proc-macro2 = "0.4"
|
||||
rustfmt-nightly = { git = "https://github.com/rust-lang-nursery/rustfmt" }
|
||||
|
@ -11,6 +11,7 @@
|
||||
//! 3. The path to `syn` is hardcoded.
|
||||
|
||||
use crate::types;
|
||||
use indexmap::IndexMap;
|
||||
use proc_macro2::TokenStream;
|
||||
|
||||
use std::fs::File;
|
||||
@ -516,21 +517,21 @@ mod codegen {
|
||||
types::Node::Struct(ref v) => {
|
||||
let mut fold_fields = TokenStream::new();
|
||||
|
||||
for field in v.fields() {
|
||||
let id = Ident::new(field.ident(), Span::call_site());
|
||||
for (field, ty) in v.fields() {
|
||||
let id = Ident::new(field, Span::call_site());
|
||||
let ref_toks = Owned(quote!(_i.#id));
|
||||
let visit_field = visit(field.ty(), v.features(), defs, Visit, &ref_toks)
|
||||
let visit_field = visit(ty, v.features(), defs, Visit, &ref_toks)
|
||||
.unwrap_or_else(|| noop_visit(Visit, &ref_toks));
|
||||
visit_impl.append_all(quote! {
|
||||
#visit_field;
|
||||
});
|
||||
let visit_mut_field =
|
||||
visit(field.ty(), v.features(), defs, VisitMut, &ref_toks)
|
||||
visit(ty, v.features(), defs, VisitMut, &ref_toks)
|
||||
.unwrap_or_else(|| noop_visit(VisitMut, &ref_toks));
|
||||
visit_mut_impl.append_all(quote! {
|
||||
#visit_mut_field;
|
||||
});
|
||||
let fold = visit(field.ty(), v.features(), defs, Fold, &ref_toks)
|
||||
let fold = visit(ty, v.features(), defs, Fold, &ref_toks)
|
||||
.unwrap_or_else(|| noop_visit(Fold, &ref_toks));
|
||||
|
||||
fold_fields.append_all(quote! {
|
||||
@ -645,7 +646,7 @@ pub fn generate(defs: &types::Definitions) {
|
||||
defs.insert(types::Node::Struct(types::Struct::new(
|
||||
tt.to_string(),
|
||||
types::Features::default(),
|
||||
vec![],
|
||||
IndexMap::new(),
|
||||
true)
|
||||
));
|
||||
}
|
||||
|
@ -13,6 +13,7 @@
|
||||
#![recursion_limit = "128"]
|
||||
#![allow(clippy::needless_pass_by_value)]
|
||||
|
||||
extern crate indexmap;
|
||||
extern crate inflections;
|
||||
extern crate proc_macro2;
|
||||
#[macro_use]
|
||||
|
@ -1,5 +1,6 @@
|
||||
use crate::types;
|
||||
|
||||
use indexmap::IndexMap;
|
||||
use syn::{Data, DataStruct, DeriveInput, Ident, Item};
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
@ -107,13 +108,13 @@ fn introspect_struct(
|
||||
all_fields_pub = false;
|
||||
}
|
||||
|
||||
types::Field::new(
|
||||
(
|
||||
field.ident.as_ref().unwrap().to_string(),
|
||||
introspect_type(&field.ty, items, tokens),
|
||||
)
|
||||
})
|
||||
.collect(),
|
||||
syn::Fields::Unit => vec![],
|
||||
syn::Fields::Unit => IndexMap::new(),
|
||||
_ => panic!("Struct representation not supported"),
|
||||
};
|
||||
|
||||
|
@ -1,3 +1,5 @@
|
||||
use indexmap::IndexMap;
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::ops;
|
||||
|
||||
@ -18,7 +20,7 @@ pub enum Node {
|
||||
pub struct Struct {
|
||||
ident: String,
|
||||
features: Features,
|
||||
fields: Vec<Field>,
|
||||
fields: IndexMap<String, Type>,
|
||||
all_fields_pub: bool,
|
||||
}
|
||||
|
||||
@ -35,13 +37,6 @@ pub struct Variant {
|
||||
fields: Vec<Type>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct Field {
|
||||
ident: String,
|
||||
#[serde(rename = "type")]
|
||||
ty: Type,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum Type {
|
||||
@ -107,7 +102,7 @@ impl Struct {
|
||||
pub fn new(
|
||||
ident: String,
|
||||
features: Features,
|
||||
fields: Vec<Field>,
|
||||
fields: IndexMap<String, Type>,
|
||||
all_fields_pub: bool,
|
||||
) -> Struct {
|
||||
Struct {
|
||||
@ -122,7 +117,7 @@ impl Struct {
|
||||
&self.features
|
||||
}
|
||||
|
||||
pub fn fields(&self) -> &[Field] {
|
||||
pub fn fields(&self) -> &IndexMap<String, Type> {
|
||||
&self.fields
|
||||
}
|
||||
|
||||
@ -159,20 +154,6 @@ impl Variant {
|
||||
}
|
||||
}
|
||||
|
||||
impl Field {
|
||||
pub fn new(ident: String, ty: Type) -> Field {
|
||||
Field { ident, ty }
|
||||
}
|
||||
|
||||
pub fn ident(&self) -> &str {
|
||||
&self.ident
|
||||
}
|
||||
|
||||
pub fn ty(&self) -> &Type {
|
||||
&self.ty
|
||||
}
|
||||
}
|
||||
|
||||
impl Punctuated {
|
||||
pub fn new(element: Type, punct: String) -> Self {
|
||||
Punctuated { element: Box::new(element), punct }
|
||||
|
Loading…
Reference in New Issue
Block a user