Skip to content

Commit

Permalink
Implements as_comparable() for serde_json
Browse files Browse the repository at this point in the history
  • Loading branch information
tailhook committed Sep 24, 2017
1 parent 0028b3f commit b3ef5da
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use serde_json::Value;

use {DataError, Variable, Var, Context, Template, RenderError, Pos, Output};
use {Number};
use compare::Comparable;

pub const TRUE: &'static &'static str = &"true";
pub const FALSE: &'static &'static str = &"false";
Expand Down Expand Up @@ -77,6 +78,25 @@ impl<'render> Variable<'render> for Value {
_ => Err(DataError::NumberUnsupported(self.typename())),
}
}
fn as_comparable(&self) -> Result<Comparable, DataError> {
use serde_json::Value::*;
match *self {
Bool(x) => Ok(x.into()),
Number(ref val) if val.is_u64() => {
Ok(val.as_u64().unwrap().into())
}
Number(ref val) if val.is_i64() => {
Ok(val.as_i64().unwrap().into())
}
Number(ref val) => {
Ok(val.as_f64().unwrap().into())
}
String(ref s) => Ok(s[..].into()),
Array(_) => Err(DataError::ComparisonUnsupported(self.typename())),
Object(_) => Err(DataError::ComparisonUnsupported(self.typename())),
Null => Err(DataError::ComparisonUnsupported(self.typename())),
}
}
fn output(&self) -> Result<Output, DataError> {
use serde_json::Value::*;
match *self {
Expand Down

0 comments on commit b3ef5da

Please sign in to comment.