Skip to content

Commit

Permalink
pattern/deser: create parse_uuid() function
Browse files Browse the repository at this point in the history
Signed-off-by: Tibor Benke <ihrwein@gmail.com>
  • Loading branch information
ihrwein committed Aug 18, 2015
1 parent 801e031 commit bcb6103
Showing 1 changed file with 29 additions and 23 deletions.
52 changes: 29 additions & 23 deletions src/matcher/pattern/deser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,33 @@ impl serde::Deserialize for Field {

struct PatternVisitor;

impl PatternVisitor {
pub fn parse_uuid<V: serde::de::MapVisitor>(uuid: Option<String>) -> Result<Uuid, V::Error> {
let uuid = match uuid {
Some(uuid) => {
match Uuid::parse_str(&uuid) {
Ok(value) => Some(value),
Err(err) => {
error!("Invalid field 'uuid': uuid={:?} error={}", &uuid, err);
None
}
}
},
None => {
None
}
};

match uuid {
Some(uuid) => Ok(uuid),
None => {
//error!("Missing field 'uuid': name={:?}", name);
try!(Err(serde::de::Error::missing_field_error("uuid")))
}
}
}
}

impl serde::de::Visitor for PatternVisitor {
type Value = Pattern;

Expand All @@ -81,21 +108,7 @@ impl serde::de::Visitor for PatternVisitor {
}
}

let uuid = match uuid {
Some(uuid) => {
match Uuid::parse_str(&uuid) {
Ok(value) => Some(value),
Err(err) => {
error!("Invalid field 'uuid': uuid={:?} error={}", &uuid, err);
None
}
}
},
None => {
None
}
};

let uuid = try!(PatternVisitor::parse_uuid::<V>(uuid));
let name = match name {
Some(name) => name,
None => try!(visitor.missing_field("name")),
Expand All @@ -117,16 +130,9 @@ impl serde::de::Visitor for PatternVisitor {
}
};

let uuid_final = match uuid {
Some(uuid) => uuid,
None => {
error!("Missing field 'uuid': name={:?}", name);
try!(Err(serde::de::Error::missing_field_error("uuid")))
}
};

try!(visitor.end());

Ok(Pattern::new(name, uuid_final, pattern, test_messages, values, tags))
Ok(Pattern::new(name, uuid, pattern, test_messages, values, tags))
}
}

0 comments on commit bcb6103

Please sign in to comment.