Skip to content

Commit

Permalink
Taxonomy: Using IsLocked for DOOR_IS_LOCKED
Browse files Browse the repository at this point in the history
  • Loading branch information
David Rajchenbach-Teller committed Jun 10, 2016
1 parent 4ced0a6 commit cc7dd5c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
6 changes: 3 additions & 3 deletions components/openzwave-adapter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ fn ozw_vid_as_taxo_value(vid: &ValueID) -> Option<Value> {
if ref_eq(kind, &DOOR_IS_OPEN) {
Some(Value::new(if value {OpenClosed::Open} else {OpenClosed::Closed}))
} else if ref_eq(kind, &DOOR_IS_LOCKED) {
Some(Value::new(if value {DoorLocked::Locked} else {DoorLocked::Unlocked}))
Some(Value::new(if value {IsLocked::Locked} else {IsLocked::Unlocked}))
} else {
None
}
Expand All @@ -170,8 +170,8 @@ fn set_ozw_vid_from_taxo_value(vid: &ValueID, value: Value) -> Result<(), TaxoEr
ValueType::ValueType_Bool => {
if let Some(open_closed) = value.downcast::<OpenClosed>() {
vid.set_bool(*open_closed == OpenClosed::Open)
} else if let Some(locked_unlocked) = value.downcast::<DoorLocked>() {
vid.set_bool(*locked_unlocked == DoorLocked::Locked)
} else if let Some(locked_unlocked) = value.downcast::<IsLocked>() {
vid.set_bool(*locked_unlocked == IsLocked::Locked)
} else {
return Err(TaxoError::InvalidValue) // TODO InvalidType would be better but we'll need to fix specific types for specific TaxoIds
}
Expand Down
8 changes: 4 additions & 4 deletions components/taxonomy/src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,11 @@ lazy_static! {
/// - watch this channel to be informed when it is (un)locked.
pub static ref DOOR_IS_LOCKED : Channel = Channel {
feature: Id::new("door/is-locked"),
supports_send: Some(Signature::accepts(Maybe::Required(format::OPEN_CLOSED.clone()))),
supports_fetch: Some(Signature::returns(Maybe::Required(format::OPEN_CLOSED.clone()))),
supports_send: Some(Signature::accepts(Maybe::Required(format::IS_LOCKED.clone()))),
supports_fetch: Some(Signature::returns(Maybe::Required(format::IS_LOCKED.clone()))),
supports_watch: Some(Signature {
accepts: Maybe::Optional(format::OPEN_CLOSED.clone()),
returns: Maybe::Required(format::OPEN_CLOSED.clone())
accepts: Maybe::Optional(format::IS_LOCKED.clone()),
returns: Maybe::Required(format::IS_LOCKED.clone())
}),
.. Channel::default()
};
Expand Down
45 changes: 23 additions & 22 deletions components/taxonomy/src/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ impl Data for OpenClosed {
///
/// Values of this type are represented by strings "Locked" | "Unlocked".
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum DoorLocked {
pub enum IsLocked {
/// # JSON
///
/// Represented by "Locked".
Expand All @@ -428,10 +428,10 @@ pub enum DoorLocked {
/// use foxbox_taxonomy::values::*;
/// use foxbox_taxonomy::parse::*;
///
/// let parsed = DoorLocked::from_str("\"Locked\"").unwrap();
/// assert_eq!(parsed, DoorLocked::Locked);
/// let parsed = IsLocked::from_str("\"Locked\"").unwrap();
/// assert_eq!(parsed, IsLocked::Locked);
///
/// let serialized: JSON = DoorLocked::Locked.to_json();
/// let serialized: JSON = IsLocked::Locked.to_json();
/// assert_eq!(serialized.as_string().unwrap(), "Locked");
/// ```
Locked,
Expand All @@ -444,61 +444,61 @@ pub enum DoorLocked {
/// use foxbox_taxonomy::values::*;
/// use foxbox_taxonomy::parse::*;
///
/// let parsed = DoorLocked::from_str("\"Unlocked\"").unwrap();
/// assert_eq!(parsed, DoorLocked::Unlocked);
/// let parsed = IsLocked::from_str("\"Unlocked\"").unwrap();
/// assert_eq!(parsed, IsLocked::Unlocked);
///
/// let serialized: JSON = DoorLocked::Unlocked.to_json();
/// let serialized: JSON = IsLocked::Unlocked.to_json();
/// assert_eq!(serialized.as_string().unwrap(), "Unlocked");
/// ```
Unlocked,
}

impl DoorLocked {
impl IsLocked {
fn as_bool(&self) -> bool {
match *self {
DoorLocked::Locked => true,
DoorLocked::Unlocked => false,
IsLocked::Locked => true,
IsLocked::Unlocked => false,
}
}
}

impl Data for DoorLocked {
impl Data for IsLocked {
fn description() -> String {
"DoorLocked".to_owned()
"IsLocked".to_owned()
}
fn parse(path: Path, source: &JSON, _binary: &BinarySource) -> Result<Self, Error> {
match source.as_string() {
Some("Locked") => Ok(DoorLocked::Locked),
Some("Unlocked") => Ok(DoorLocked::Unlocked),
Some("Locked") => Ok(IsLocked::Locked),
Some("Unlocked") => Ok(IsLocked::Unlocked),
Some(str) => Err(Error::ParseError(ParseError::unknown_constant(str, &path))),
None => Err(Error::ParseError(ParseError::type_error("DoorLocked", &path, "string")))
None => Err(Error::ParseError(ParseError::type_error("IsLocked", &path, "string")))
}
}
fn serialize(source: &Self, _binary: &BinaryTarget) -> Result<JSON, Error> {
let str = match *source {
DoorLocked::Locked => "Locked",
DoorLocked::Unlocked => "Unlocked"
IsLocked::Locked => "Locked",
IsLocked::Unlocked => "Unlocked"
};
Ok(JSON::String(str.to_owned()))
}
}

impl ToJSON for DoorLocked {
impl ToJSON for IsLocked {
fn to_json(&self) -> JSON {
match *self {
DoorLocked::Locked => JSON::String("Locked".to_owned()),
DoorLocked::Unlocked => JSON::String("Unlocked".to_owned())
IsLocked::Locked => JSON::String("Locked".to_owned()),
IsLocked::Unlocked => JSON::String("Unlocked".to_owned())
}
}
}

impl PartialOrd for DoorLocked {
impl PartialOrd for IsLocked {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl Ord for DoorLocked {
impl Ord for IsLocked {
fn cmp(&self, other: &Self) -> Ordering {
self.as_bool().cmp(&other.as_bool())
}
Expand Down Expand Up @@ -1189,6 +1189,7 @@ pub mod format {
pub static ref ON_OFF : Arc<Format> = Arc::new(Format::new::<OnOff>());
pub static ref OPEN_CLOSED : Arc<Format> = Arc::new(Format::new::<OpenClosed>());
pub static ref IS_SECURE : Arc<Format> = Arc::new(Format::new::<IsSecure>());
pub static ref IS_LOCKED : Arc<Format> = Arc::new(Format::new::<IsLocked>());
pub static ref COLOR : Arc<Format> = Arc::new(Format::new::<Color>());
pub static ref JSON: Arc<Format> = Arc::new(Format::new::<Json>());
pub static ref STRING : Arc<Format> = Arc::new(Format::new::<String>());
Expand Down

0 comments on commit cc7dd5c

Please sign in to comment.