Rename #[serde(alias)] to #[serde(rename)] and add tests
Closes #9 and #47.
This commit is contained in:
parent
54a970e694
commit
2cd49060fa
@ -5,7 +5,7 @@ use syntax::ptr::P;
|
||||
|
||||
use aster;
|
||||
|
||||
fn field_alias(field: &ast::StructField) -> Option<&ast::Lit> {
|
||||
fn field_rename(field: &ast::StructField) -> Option<&ast::Lit> {
|
||||
field.node.attrs.iter()
|
||||
.find(|sa| {
|
||||
if let ast::MetaList(ref n, _) = sa.node.value.node {
|
||||
@ -19,7 +19,7 @@ fn field_alias(field: &ast::StructField) -> Option<&ast::Lit> {
|
||||
attr::mark_used(&sa);
|
||||
vals.iter().fold(None, |v, mi| {
|
||||
if let ast::MetaNameValue(ref n, ref lit) = mi.node {
|
||||
if n == &"alias" {
|
||||
if n == &"rename" {
|
||||
Some(lit)
|
||||
} else {
|
||||
v
|
||||
@ -41,8 +41,8 @@ pub fn struct_field_strs(
|
||||
) -> Vec<P<ast::Expr>> {
|
||||
struct_def.fields.iter()
|
||||
.map(|field| {
|
||||
match field_alias(field) {
|
||||
Some(alias) => builder.expr().build_lit(P(alias.clone())),
|
||||
match field_rename(field) {
|
||||
Some(rename) => builder.expr().build_lit(P(rename.clone())),
|
||||
None => {
|
||||
match field.node.kind {
|
||||
ast::NamedField(name, _) => {
|
||||
|
40
tests/test_annotations.rs
Normal file
40
tests/test_annotations.rs
Normal file
@ -0,0 +1,40 @@
|
||||
#![feature(custom_attribute, custom_derive, plugin, test)]
|
||||
#![plugin(serde_macros)]
|
||||
|
||||
extern crate test;
|
||||
extern crate serde;
|
||||
|
||||
use serde::json;
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
struct Default {
|
||||
a1: i32,
|
||||
#[serde(default)]
|
||||
a2: i32,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
struct Rename {
|
||||
a1: i32,
|
||||
#[serde(rename="a3")]
|
||||
a2: i32,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_default() {
|
||||
let deserialized_value: Default = json::from_str(&"{\"a1\":1,\"a2\":2}").unwrap();
|
||||
assert_eq!(deserialized_value, Default { a1: 1, a2: 2 });
|
||||
|
||||
let deserialized_value: Default = json::from_str(&"{\"a1\":1}").unwrap();
|
||||
assert_eq!(deserialized_value, Default { a1: 1, a2: 0 });
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rename() {
|
||||
let value = Rename { a1: 1, a2: 2 };
|
||||
let serialized_value = json::to_string(&value).unwrap();
|
||||
assert_eq!(serialized_value, "{\"a1\":1,\"a3\":2}");
|
||||
|
||||
let deserialized_value: Rename = json::from_str(&serialized_value).unwrap();
|
||||
assert_eq!(value, deserialized_value);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user