Implemented disallow_unknown

* Added codegen for disallow_unknown
* ... with new default to ignore unknown values during deserialization
* Added ContainerAttrs
This commit is contained in:
John Heitmann
2016-01-10 19:34:48 -08:00
parent 59285f627b
commit bfac1a581c
8 changed files with 366 additions and 10 deletions
+63
View File
@@ -243,3 +243,66 @@ impl<'a> FieldAttrsBuilder<'a> {
}
}
}
/// Represents container (e.g. struct) attribute information
#[derive(Debug)]
pub struct ContainerAttrs {
disallow_unknown: bool,
}
impl ContainerAttrs {
pub fn disallow_unknown(&self) -> bool {
self.disallow_unknown
}
}
pub struct ContainerAttrsBuilder {
disallow_unknown: bool,
}
impl ContainerAttrsBuilder {
pub fn new() -> ContainerAttrsBuilder {
ContainerAttrsBuilder {
disallow_unknown: false,
}
}
pub fn attrs(self, attrs: &[ast::Attribute]) -> ContainerAttrsBuilder {
attrs.iter().fold(self, ContainerAttrsBuilder::attr)
}
pub fn attr(self, attr: &ast::Attribute) -> ContainerAttrsBuilder {
match attr.node.value.node {
ast::MetaList(ref name, ref items) if name == &"serde" => {
attr::mark_used(&attr);
items.iter().fold(self, ContainerAttrsBuilder::meta_item)
}
_ => {
self
}
}
}
pub fn meta_item(self, meta_item: &P<ast::MetaItem>) -> ContainerAttrsBuilder {
match meta_item.node {
ast::MetaWord(ref name) if name == &"disallow_unknown" => {
self.disallow_unknown()
}
_ => {
// Ignore unknown meta variables for now.
self
}
}
}
pub fn disallow_unknown(mut self) -> ContainerAttrsBuilder {
self.disallow_unknown = true;
self
}
pub fn build(self) -> ContainerAttrs {
ContainerAttrs {
disallow_unknown: self.disallow_unknown,
}
}
}