Enum foxbox_taxonomy::values::Value
[−]
[src]
pub enum Value { Unit, OnOff(OnOff), OpenClosed(OpenClosed), TimeStamp(TimeStamp), Duration(Duration), Temperature(Temperature), Color(Color), String(Arc<String>), ThinkerbellRule(ThinkerbellRule), ExtBool(ExtValue<bool>), ExtNumeric(ExtValue<f64>), Json(Arc<Json>), Binary(Binary), }
Representation of an actual value that can be sent to/received from a service.
JSON
Values of this state are represented by an object { key: value }
, where key is one of
Unit
, OnOff
, OpenClosed
, ... The value
for Unit
is ignored.
Other forms of (de)serialization
Values of this state are represented by an object { key: value }
, where key is one of
Unit
, OnOff
, OpenClosed
, ... The value
for Unit
is ignored.
extern crate serde; extern crate serde_json; extern crate foxbox_taxonomy; use foxbox_taxonomy::values::Value::*; use foxbox_taxonomy::values::OnOff::*; use foxbox_taxonomy::values::OpenClosed::*; let unit = serde_json::to_string(&Unit).unwrap(); assert_eq!(unit, "{\"Unit\":[]}"); let unit : foxbox_taxonomy::values::Value = serde_json::from_str("{\"Unit\":[]}").unwrap(); assert_eq!(unit, Unit); let on = serde_json::to_string(&OnOff(On)).unwrap(); assert_eq!(on, "{\"OnOff\":\"On\"}"); let on : foxbox_taxonomy::values::Value = serde_json::from_str("{\"OnOff\":\"On\"}").unwrap(); assert_eq!(on, OnOff(On)); let open = serde_json::to_string(&OpenClosed(Open)).unwrap(); assert_eq!(open, "{\"OpenClosed\":\"Open\"}"); let open : foxbox_taxonomy::values::Value = serde_json::from_str("{\"OpenClosed\":\"Open\"}").unwrap(); assert_eq!(open, OpenClosed(Open));
Variants
Unit | An absolute time and date. JSONRepresented as extern crate foxbox_taxonomy; use foxbox_taxonomy::values::*; use foxbox_taxonomy::parse::*; use foxbox_taxonomy::serialize::*; let source = "{ \"Unit\": [] }"; let parsed = Value::from_str(source).unwrap(); if let Value::Unit = parsed { // ok } else { panic!(); } let serialized: JSON = parsed.to_json(&mut MultiPart::new()); if let JSON::Object(ref obj) = serialized { let serialized = obj.get("Unit").unwrap(); assert!(serialized.is_null()); } | |
OnOff | An on/off value. JSONRepresented as extern crate foxbox_taxonomy; use foxbox_taxonomy::values::*; use foxbox_taxonomy::parse::*; use foxbox_taxonomy::serialize::*; let source = "{ \"OnOff\": \"On\" }"; let parsed = Value::from_str(source).unwrap(); if let Value::OnOff(OnOff::On) = parsed { // ok } else { panic!(); } let serialized: JSON = parsed.to_json(&mut MultiPart::new()); if let JSON::Object(ref obj) = serialized { let serialized = obj.get("OnOff").unwrap(); assert_eq!(serialized.as_string().unwrap(), "On"); } | |
OpenClosed | An open/closed value. JSONRepresented as extern crate foxbox_taxonomy; use foxbox_taxonomy::values::*; use foxbox_taxonomy::parse::*; use foxbox_taxonomy::serialize::*; let source = "{ \"OpenClosed\": \"Open\" }"; let parsed = Value::from_str(source).unwrap(); if let Value::OpenClosed(OpenClosed::Open) = parsed { // ok } else { panic!(); } let serialized: JSON = parsed.to_json(&mut MultiPart::new()); if let JSON::Object(ref obj) = serialized { let serialized = obj.get("OpenClosed").unwrap(); assert_eq!(serialized.as_string().unwrap(), "Open"); } | |
TimeStamp | An absolute time and date. JSONRepresented as extern crate chrono; extern crate foxbox_taxonomy; use foxbox_taxonomy::values::*; use foxbox_taxonomy::parse::*; use foxbox_taxonomy::serialize::*; use chrono::Datelike; let source = "{ \"TimeStamp\": \"2014-11-28T21:45:59.324310806+09:00\" }"; let parsed = Value::from_str(source).unwrap(); if let Value::TimeStamp(ref ts) = parsed { let date_time = ts.as_datetime(); assert_eq!(date_time.year(), 2014); assert_eq!(date_time.month(), 11); assert_eq!(date_time.day(), 28); } else { panic!(); } let serialized: JSON = parsed.to_json(&mut MultiPart::new()); if let JSON::Object(ref obj) = serialized { let serialized = obj.get("TimeStamp").unwrap(); assert!(serialized.as_string().unwrap().starts_with("2014-11-28")); } else { panic!(); } | |
Duration | A duration, also used to represent a time of day. JSONRepresented by extern crate foxbox_taxonomy; extern crate chrono; use foxbox_taxonomy::values::*; use foxbox_taxonomy::parse::*; use foxbox_taxonomy::serialize::*; use chrono::Duration as ChronoDuration; let parsed = Value::from_str("{\"Duration\": 60.01}").unwrap(); if let Value::Duration(d) = parsed.clone() { let duration : ChronoDuration = d.into(); assert_eq!(duration.num_seconds(), 60); assert_eq!(duration.num_milliseconds(), 60010); } else { panic!(); } let serialized: JSON = parsed.to_json(&mut MultiPart::new()); if let JSON::Object(ref obj) = serialized { let serialized = obj.get("Duration").unwrap(); assert!(serialized.as_f64().unwrap() >= 60. && serialized.as_f64().unwrap() < 61.); } else { panic!(); } | |
Temperature | A temperature. JSONRepresented by extern crate foxbox_taxonomy; extern crate chrono; use foxbox_taxonomy::values::*; use foxbox_taxonomy::parse::*; use foxbox_taxonomy::serialize::*; let source = "{ \"Temperature\": { \"C\": 2.0 } }"; let parsed = Value::from_str(source).unwrap(); if let Value::Temperature(Temperature::C(ref val)) = parsed { assert_eq!(*val, 2.0); } else { panic!(); } let serialized: JSON = parsed.to_json(&mut MultiPart::new()); let val = serialized.find_path(&["Temperature", "C"]).unwrap().as_f64().unwrap(); assert_eq!(val, 2.0); | |
Color | A color. JSONRepresented by extern crate foxbox_taxonomy; extern crate chrono; use foxbox_taxonomy::values::*; use foxbox_taxonomy::parse::*; use foxbox_taxonomy::serialize::*; let source = "{ \"Color\": { \"r\": 0.1, \"g\": 0.2, \"b\": 0.4 } }"; let parsed = Value::from_str(source).unwrap(); if let Value::Color(Color::RGBA(0.1, 0.2, 0.4, 0.0)) = parsed { // Ok. } else { panic!(); } let serialized: JSON = parsed.to_json(&mut MultiPart::new()); let val = serialized.find_path(&["Color", "g"]).unwrap().as_f64().unwrap(); assert_eq!(val, 0.2); | |
String | A string. JSONRepresented by extern crate foxbox_taxonomy; extern crate chrono; use foxbox_taxonomy::values::*; use foxbox_taxonomy::parse::*; use foxbox_taxonomy::serialize::*; let source = "{ \"String\": \"foobar\" }"; let parsed = Value::from_str(source).unwrap(); if let Value::String(ref str) = parsed { assert_eq!(&*str as &str, "foobar"); } else { panic!(); } let serialized: JSON = parsed.to_json(&mut MultiPart::new()); let val = serialized.find_path(&["String"]).unwrap().as_string().unwrap(); assert_eq!(&val as &str, "foobar"); | |
ThinkerbellRule | ||
ExtBool | A boolean value representing a unit that has not been standardized yet into the API. | |
ExtNumeric | A numeric value representing a unit that has not been standardized yet into the API. | |
Json | A Json value. We put it behind an JSONRepresented by extern crate foxbox_taxonomy; extern crate chrono; use foxbox_taxonomy::values::*; use foxbox_taxonomy::parse::*; use foxbox_taxonomy::serialize::*; let source = "{ \"Json\": { \"foo\": \"bar\" } }"; let parsed = Value::from_str(source).unwrap(); if let Value::Json(ref obj) = parsed { assert_eq!(obj.0.find_path(&["foo"]).unwrap().as_string().unwrap(), "bar") } else { panic!(); } let serialized: JSON = parsed.to_json(&mut MultiPart::new()); let val = serialized.find_path(&["Json", "foo"]).unwrap().as_string().unwrap(); assert_eq!(val, "bar"); | |
Binary | Binary data. JSONRepresented by |