diff --git a/serde_codegen/Cargo.toml b/serde_codegen/Cargo.toml index e068c198..4cdc20ce 100644 --- a/serde_codegen/Cargo.toml +++ b/serde_codegen/Cargo.toml @@ -18,7 +18,7 @@ with-syntex = [ "quasi/with-syntex", "quasi_codegen", "quasi_codegen/with-syntex", - "serde_item/with-syntex", + "serde_internals/with-syntex", "syntex", "syntex_syntax", ] @@ -32,6 +32,6 @@ aster = { version = "^0.19.0", default-features = false } clippy = { version = "^0.*", optional = true } quasi = { version = "^0.13.0", default-features = false } quasi_macros = { version = "^0.13.0", optional = true } -serde_item = { version = "^0.2.0", path = "../serde_item", default-features = false } +serde_internals = { version = "^0.1.0", path = "../serde_internals", default-features = false } syntex = { version = "^0.36.0", optional = true } syntex_syntax = { version = "^0.36.0", optional = true } diff --git a/serde_codegen/src/bound.rs b/serde_codegen/src/bound.rs index 0acbcab8..9e6b182d 100644 --- a/serde_codegen/src/bound.rs +++ b/serde_codegen/src/bound.rs @@ -6,7 +6,7 @@ use syntax::ast; use syntax::ptr::P; use syntax::visit; -use item::{attr, Item}; +use internals::{attr, Item}; // Remove the default from every type parameter because in the generated impls // they look like associated types: "error: associated type bindings are not diff --git a/serde_codegen/src/de.rs b/serde_codegen/src/de.rs index 175d7c22..d3c236bf 100644 --- a/serde_codegen/src/de.rs +++ b/serde_codegen/src/de.rs @@ -7,8 +7,7 @@ use syntax::parse::token::InternedString; use syntax::ptr::P; use bound; -use error::Error; -use item::{self, attr}; +use internals::{attr, Body, Error, Field, Item, Style, Variant}; pub fn expand_derive_deserialize( cx: &mut ExtCtxt, @@ -27,9 +26,9 @@ pub fn expand_derive_deserialize( } }; - let item = match item::Item::from_ast(cx, item) { + let item = match Item::from_ast(cx, item) { Ok(item) => item, - Err(item::Error::UnexpectedItemKind) => { + Err(Error::UnexpectedItemKind) => { cx.span_err(item.span, "`#[derive(Deserialize)]` may only be applied to structs and enums"); return; @@ -49,7 +48,7 @@ pub fn expand_derive_deserialize( fn deserialize_item( cx: &ExtCtxt, builder: &aster::AstBuilder, - item: &item::Item, + item: &Item, ) -> P { let impl_generics = build_impl_generics(builder, &item); @@ -88,7 +87,7 @@ fn deserialize_item( // each generic field type that will be set to a default value. fn build_impl_generics( builder: &aster::AstBuilder, - item: &item::Item, + item: &Item, ) -> ast::Generics { let generics = bound::without_defaults(item.generics); @@ -131,12 +130,12 @@ fn requires_default(attrs: &attr::Field) -> bool { fn deserialize_body( cx: &ExtCtxt, builder: &aster::AstBuilder, - item: &item::Item, + item: &Item, impl_generics: &ast::Generics, ty: P, ) -> P { match item.body { - item::Body::Enum(ref variants) => { + Body::Enum(ref variants) => { deserialize_item_enum( cx, builder, @@ -146,7 +145,7 @@ fn deserialize_body( variants, &item.attrs) } - item::Body::Struct(item::Style::Struct, ref fields) => { + Body::Struct(Style::Struct, ref fields) => { if fields.iter().any(|field| field.ident.is_none()) { cx.span_bug(item.span, "struct has unnamed fields") } @@ -161,8 +160,8 @@ fn deserialize_body( fields, &item.attrs) } - item::Body::Struct(item::Style::Tuple, ref fields) | - item::Body::Struct(item::Style::Newtype, ref fields) => { + Body::Struct(Style::Tuple, ref fields) | + Body::Struct(Style::Newtype, ref fields) => { if fields.iter().any(|field| field.ident.is_some()) { cx.span_bug(item.span, "tuple struct has named fields") } @@ -177,7 +176,7 @@ fn deserialize_body( fields, &item.attrs) } - item::Body::Struct(item::Style::Unit, _) => { + Body::Struct(Style::Unit, _) => { deserialize_unit_struct( cx, builder, @@ -308,7 +307,7 @@ fn deserialize_tuple( variant_ident: Option, impl_generics: &ast::Generics, ty: P, - fields: &[item::Field], + fields: &[Field], item_attrs: &attr::Item, ) -> P { let where_clause = &impl_generics.where_clause; @@ -390,7 +389,7 @@ fn deserialize_seq( type_ident: Ident, type_path: ast::Path, impl_generics: &ast::Generics, - fields: &[item::Field], + fields: &[Field], is_struct: bool, ) -> P { let let_values: Vec<_> = fields.iter() @@ -470,7 +469,7 @@ fn deserialize_newtype_struct( type_ident: Ident, type_path: &ast::Path, impl_generics: &ast::Generics, - field: &item::Field, + field: &Field, ) -> Vec { let value = match field.attrs.deserialize_with() { None => { @@ -505,7 +504,7 @@ fn deserialize_struct( variant_ident: Option, impl_generics: &ast::Generics, ty: P, - fields: &[item::Field], + fields: &[Field], item_attrs: &attr::Item, ) -> P { let where_clause = &impl_generics.where_clause; @@ -587,7 +586,7 @@ fn deserialize_item_enum( type_ident: Ident, impl_generics: &ast::Generics, ty: P, - variants: &[item::Variant], + variants: &[Variant], item_attrs: &attr::Item ) -> P { let where_clause = &impl_generics.where_clause; @@ -678,19 +677,19 @@ fn deserialize_variant( type_ident: Ident, generics: &ast::Generics, ty: P, - variant: &item::Variant, + variant: &Variant, item_attrs: &attr::Item, ) -> P { let variant_ident = variant.ident; match variant.style { - item::Style::Unit => { + Style::Unit => { quote_expr!(cx, { try!(visitor.visit_unit()); Ok($type_ident::$variant_ident) }) } - item::Style::Newtype => { + Style::Newtype => { deserialize_newtype_variant( cx, builder, @@ -700,7 +699,7 @@ fn deserialize_variant( &variant.fields[0], ) } - item::Style::Tuple => { + Style::Tuple => { deserialize_tuple( cx, builder, @@ -712,7 +711,7 @@ fn deserialize_variant( item_attrs, ) } - item::Style::Struct => { + Style::Struct => { deserialize_struct( cx, builder, @@ -733,7 +732,7 @@ fn deserialize_newtype_variant( type_ident: Ident, variant_ident: Ident, impl_generics: &ast::Generics, - field: &item::Field, + field: &Field, ) -> P { let visit = match field.attrs.deserialize_with() { None => { @@ -919,7 +918,7 @@ fn deserialize_struct_visitor( type_ident: Ident, struct_path: ast::Path, impl_generics: &ast::Generics, - fields: &[item::Field], + fields: &[Field], item_attrs: &attr::Item, ) -> (Vec>, ast::Stmt, P) { let field_exprs = fields.iter() @@ -971,7 +970,7 @@ fn deserialize_map( type_ident: Ident, struct_path: ast::Path, impl_generics: &ast::Generics, - fields: &[item::Field], + fields: &[Field], item_attrs: &attr::Item, ) -> P { // Create the field names for the fields. @@ -1187,14 +1186,14 @@ fn name_expr( fn check_no_str( cx: &ExtCtxt, - item: &item::Item, -) -> Result<(), Error> { - let fail = |field: &item::Field| { + item: &Item, +) -> Result<(), ()> { + let fail = |field: &Field| { cx.span_err( field.span, "Serde does not support deserializing fields of type &str; \ consider using String instead"); - Err(Error) + Err(()) }; for field in item.body.all_fields() { diff --git a/serde_codegen/src/error.rs b/serde_codegen/src/error.rs deleted file mode 100644 index 8a76cf30..00000000 --- a/serde_codegen/src/error.rs +++ /dev/null @@ -1,2 +0,0 @@ -/// Error returned if failed to parse attribute. -pub struct Error; diff --git a/serde_codegen/src/lib.rs b/serde_codegen/src/lib.rs index 881d9cc2..898f34cd 100644 --- a/serde_codegen/src/lib.rs +++ b/serde_codegen/src/lib.rs @@ -7,7 +7,7 @@ extern crate aster; extern crate quasi; -extern crate serde_item as item; +extern crate serde_internals as internals; #[cfg(feature = "with-syntex")] extern crate syntex; diff --git a/serde_codegen/src/lib.rs.in b/serde_codegen/src/lib.rs.in index 0a80f38a..49726851 100644 --- a/serde_codegen/src/lib.rs.in +++ b/serde_codegen/src/lib.rs.in @@ -1,4 +1,3 @@ mod bound; mod de; -mod error; mod ser; diff --git a/serde_codegen/src/ser.rs b/serde_codegen/src/ser.rs index 403d077c..0d833eb5 100644 --- a/serde_codegen/src/ser.rs +++ b/serde_codegen/src/ser.rs @@ -6,7 +6,7 @@ use syntax::ext::base::{Annotatable, ExtCtxt}; use syntax::ptr::P; use bound; -use item::{self, attr}; +use internals::{attr, Body, Error, Field, Item, Style, Variant}; pub fn expand_derive_serialize( cx: &mut ExtCtxt, @@ -25,9 +25,9 @@ pub fn expand_derive_serialize( } }; - let item = match item::Item::from_ast(cx, item) { + let item = match Item::from_ast(cx, item) { Ok(item) => item, - Err(item::Error::UnexpectedItemKind) => { + Err(Error::UnexpectedItemKind) => { cx.span_err(item.span, "`#[derive(Serialize)]` may only be applied to structs and enums"); return; @@ -43,7 +43,7 @@ pub fn expand_derive_serialize( fn serialize_item( cx: &ExtCtxt, builder: &aster::AstBuilder, - item: &item::Item, + item: &Item, ) -> P { let impl_generics = build_impl_generics(builder, &item); @@ -81,7 +81,7 @@ fn serialize_item( // field type that will be serialized by us. fn build_impl_generics( builder: &aster::AstBuilder, - item: &item::Item, + item: &Item, ) -> ast::Generics { let generics = bound::without_defaults(item.generics); @@ -114,12 +114,12 @@ fn needs_serialize_bound(attrs: &attr::Field) -> bool { fn serialize_body( cx: &ExtCtxt, builder: &aster::AstBuilder, - item: &item::Item, + item: &Item, impl_generics: &ast::Generics, ty: P, ) -> P { match item.body { - item::Body::Enum(ref variants) => { + Body::Enum(ref variants) => { serialize_item_enum( cx, builder, @@ -129,7 +129,7 @@ fn serialize_body( variants, &item.attrs) } - item::Body::Struct(item::Style::Struct, ref fields) => { + Body::Struct(Style::Struct, ref fields) => { if fields.iter().any(|field| field.ident.is_none()) { cx.span_bug(item.span, "struct has unnamed fields") } @@ -142,7 +142,7 @@ fn serialize_body( fields, &item.attrs) } - item::Body::Struct(item::Style::Tuple, ref fields) => { + Body::Struct(Style::Tuple, ref fields) => { if fields.iter().any(|field| field.ident.is_some()) { cx.span_bug(item.span, "tuple struct has named fields") } @@ -155,7 +155,7 @@ fn serialize_body( fields, &item.attrs) } - item::Body::Struct(item::Style::Newtype, ref fields) => { + Body::Struct(Style::Newtype, ref fields) => { serialize_newtype_struct( cx, builder, @@ -164,7 +164,7 @@ fn serialize_body( &fields[0], &item.attrs) } - item::Body::Struct(item::Style::Unit, _) => { + Body::Struct(Style::Unit, _) => { serialize_unit_struct( cx, builder, @@ -190,7 +190,7 @@ fn serialize_newtype_struct( builder: &aster::AstBuilder, impl_generics: &ast::Generics, item_ty: P, - field: &item::Field, + field: &Field, item_attrs: &attr::Item, ) -> P { let type_name = name_expr(builder, item_attrs.name()); @@ -211,7 +211,7 @@ fn serialize_tuple_struct( builder: &aster::AstBuilder, impl_generics: &ast::Generics, ty: P, - fields: &[item::Field], + fields: &[Field], item_attrs: &attr::Item, ) -> P { let (visitor_struct, visitor_impl) = serialize_tuple_struct_visitor( @@ -246,7 +246,7 @@ fn serialize_struct( builder: &aster::AstBuilder, impl_generics: &ast::Generics, ty: P, - fields: &[item::Field], + fields: &[Field], item_attrs: &attr::Item, ) -> P { let (visitor_struct, visitor_impl) = serialize_struct_visitor( @@ -282,7 +282,7 @@ fn serialize_item_enum( type_ident: Ident, impl_generics: &ast::Generics, ty: P, - variants: &[item::Variant], + variants: &[Variant], item_attrs: &attr::Item, ) -> P { let arms: Vec<_> = @@ -315,7 +315,7 @@ fn serialize_variant( type_ident: Ident, generics: &ast::Generics, ty: P, - variant: &item::Variant, + variant: &Variant, variant_index: usize, item_attrs: &attr::Item, ) -> ast::Arm { @@ -325,7 +325,7 @@ fn serialize_variant( let variant_name = name_expr(builder, variant.attrs.name()); match variant.style { - item::Style::Unit => { + Style::Unit => { quote_arm!(cx, $type_ident::$variant_ident => { _serde::ser::Serializer::serialize_unit_variant( @@ -337,7 +337,7 @@ fn serialize_variant( } ) }, - item::Style::Newtype => { + Style::Newtype => { let expr = serialize_newtype_variant( cx, builder, @@ -353,7 +353,7 @@ fn serialize_variant( $type_ident::$variant_ident(ref __simple_value) => { $expr } ) }, - item::Style::Tuple => { + Style::Tuple => { let field_names: Vec = (0 .. variant.fields.len()) .map(|i| builder.id(format!("__field{}", i))) .collect(); @@ -382,7 +382,7 @@ fn serialize_variant( $pat => { $expr } ) } - item::Style::Struct => { + Style::Struct => { let field_names: Vec<_> = (0 .. variant.fields.len()) .map(|i| builder.id(format!("__field{}", i))) .collect(); @@ -432,7 +432,7 @@ fn serialize_newtype_variant( variant_name: P, item_ty: P, generics: &ast::Generics, - field: &item::Field, + field: &Field, ) -> P { let mut field_expr = quote_expr!(cx, __simple_value); if let Some(path) = field.attrs.serialize_with() { @@ -459,7 +459,7 @@ fn serialize_tuple_variant( variant_name: P, generics: &ast::Generics, structure_ty: P, - fields: &[item::Field], + fields: &[Field], field_names: Vec, ) -> P { let variant_ty = builder.ty().tuple() @@ -510,7 +510,7 @@ fn serialize_struct_variant( variant_name: P, generics: &ast::Generics, ty: P, - fields: &[item::Field], + fields: &[Field], field_names: Vec, item_attrs: &attr::Item, ) -> P { @@ -597,7 +597,7 @@ fn serialize_tuple_struct_visitor( structure_ty: P, variant_ty: P, serializer_method: ast::Ident, - fields: &[item::Field], + fields: &[Field], generics: &ast::Generics, is_enum: bool, ) -> (P, P) { @@ -678,7 +678,7 @@ fn serialize_struct_visitor( structure_ty: P, variant_ty: P, serializer_method: ast::Ident, - fields: &[item::Field], + fields: &[Field], generics: &ast::Generics, is_enum: bool, ) -> (P, P) { diff --git a/serde_item/Cargo.toml b/serde_internals/Cargo.toml similarity index 92% rename from serde_item/Cargo.toml rename to serde_internals/Cargo.toml index cce2640a..f2864280 100644 --- a/serde_item/Cargo.toml +++ b/serde_internals/Cargo.toml @@ -1,6 +1,6 @@ [package] -name = "serde_item" -version = "0.2.0" +name = "serde_internals" +version = "0.1.0" authors = ["Erick Tryzelaar "] license = "MIT/Apache-2.0" description = "AST representation used by Serde codegen. Unstable." diff --git a/serde_item/src/attr.rs b/serde_internals/src/attr.rs similarity index 100% rename from serde_item/src/attr.rs rename to serde_internals/src/attr.rs diff --git a/serde_item/src/error.rs b/serde_internals/src/error.rs similarity index 100% rename from serde_item/src/error.rs rename to serde_internals/src/error.rs diff --git a/serde_item/src/lib.rs b/serde_internals/src/lib.rs similarity index 100% rename from serde_item/src/lib.rs rename to serde_internals/src/lib.rs