Rename serde_item to serde_internals

This commit is contained in:
David Tolnay 2016-06-21 19:12:08 -07:00
parent d1be5ef187
commit c6b6e2a5d9
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
11 changed files with 59 additions and 63 deletions

View File

@ -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 }

View File

@ -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

View File

@ -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<ast::Item> {
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<ast::Ty>,
) -> P<ast::Expr> {
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<Ident>,
impl_generics: &ast::Generics,
ty: P<ast::Ty>,
fields: &[item::Field],
fields: &[Field],
item_attrs: &attr::Item,
) -> P<ast::Expr> {
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<ast::Expr> {
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<ast::TokenTree> {
let value = match field.attrs.deserialize_with() {
None => {
@ -505,7 +504,7 @@ fn deserialize_struct(
variant_ident: Option<Ident>,
impl_generics: &ast::Generics,
ty: P<ast::Ty>,
fields: &[item::Field],
fields: &[Field],
item_attrs: &attr::Item,
) -> P<ast::Expr> {
let where_clause = &impl_generics.where_clause;
@ -587,7 +586,7 @@ fn deserialize_item_enum(
type_ident: Ident,
impl_generics: &ast::Generics,
ty: P<ast::Ty>,
variants: &[item::Variant],
variants: &[Variant],
item_attrs: &attr::Item
) -> P<ast::Expr> {
let where_clause = &impl_generics.where_clause;
@ -678,19 +677,19 @@ fn deserialize_variant(
type_ident: Ident,
generics: &ast::Generics,
ty: P<ast::Ty>,
variant: &item::Variant,
variant: &Variant,
item_attrs: &attr::Item,
) -> P<ast::Expr> {
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<ast::Expr> {
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<P<ast::Item>>, ast::Stmt, P<ast::Expr>) {
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<ast::Expr> {
// 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() {

View File

@ -1,2 +0,0 @@
/// Error returned if failed to parse attribute.
pub struct Error;

View File

@ -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;

View File

@ -1,4 +1,3 @@
mod bound;
mod de;
mod error;
mod ser;

View File

@ -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<ast::Item> {
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<ast::Ty>,
) -> P<ast::Expr> {
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<ast::Ty>,
field: &item::Field,
field: &Field,
item_attrs: &attr::Item,
) -> P<ast::Expr> {
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<ast::Ty>,
fields: &[item::Field],
fields: &[Field],
item_attrs: &attr::Item,
) -> P<ast::Expr> {
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<ast::Ty>,
fields: &[item::Field],
fields: &[Field],
item_attrs: &attr::Item,
) -> P<ast::Expr> {
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<ast::Ty>,
variants: &[item::Variant],
variants: &[Variant],
item_attrs: &attr::Item,
) -> P<ast::Expr> {
let arms: Vec<_> =
@ -315,7 +315,7 @@ fn serialize_variant(
type_ident: Ident,
generics: &ast::Generics,
ty: P<ast::Ty>,
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<ast::Ident> = (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<ast::Expr>,
item_ty: P<ast::Ty>,
generics: &ast::Generics,
field: &item::Field,
field: &Field,
) -> P<ast::Expr> {
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<ast::Expr>,
generics: &ast::Generics,
structure_ty: P<ast::Ty>,
fields: &[item::Field],
fields: &[Field],
field_names: Vec<Ident>,
) -> P<ast::Expr> {
let variant_ty = builder.ty().tuple()
@ -510,7 +510,7 @@ fn serialize_struct_variant(
variant_name: P<ast::Expr>,
generics: &ast::Generics,
ty: P<ast::Ty>,
fields: &[item::Field],
fields: &[Field],
field_names: Vec<Ident>,
item_attrs: &attr::Item,
) -> P<ast::Expr> {
@ -597,7 +597,7 @@ fn serialize_tuple_struct_visitor(
structure_ty: P<ast::Ty>,
variant_ty: P<ast::Ty>,
serializer_method: ast::Ident,
fields: &[item::Field],
fields: &[Field],
generics: &ast::Generics,
is_enum: bool,
) -> (P<ast::Item>, P<ast::Item>) {
@ -678,7 +678,7 @@ fn serialize_struct_visitor(
structure_ty: P<ast::Ty>,
variant_ty: P<ast::Ty>,
serializer_method: ast::Ident,
fields: &[item::Field],
fields: &[Field],
generics: &ast::Generics,
is_enum: bool,
) -> (P<ast::Item>, P<ast::Item>) {

View File

@ -1,6 +1,6 @@
[package]
name = "serde_item"
version = "0.2.0"
name = "serde_internals"
version = "0.1.0"
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
license = "MIT/Apache-2.0"
description = "AST representation used by Serde codegen. Unstable."