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.

JSON

Represented as {"TimeStamp": string}, where string is formatted as RFC 3339 such as "2014-11-28T21:45:59.324310806+09:00".

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.

JSON

Represented as {"OnOff": string}, where string is "On" or "Off".

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.

JSON

Represented as {"OpenClosed": string}, where string is "Open" or "Closed".

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.

JSON

Represented as {"TimeStamp": string}, where string is formatted as RFC 3339 such as "2014-11-28T21:45:59.324310806+09:00".

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.

JSON

Represented by {Duration: float}, where the number, is a (floating-point) number of seconds. If this value use used for time of day, the duration is since the start of the day, in local time.

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.

JSON

Represented by {Temperature: {C: float}} or {Temperature: {F: float}}.

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.

JSON

Represented by {Color: {r: float, g: float, b: float, a: float}} where each of r, g, b, a is in [0, 1]. Value a can be omitted, in which case it is assumed to be 0.

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.

JSON

Represented by {String: string}.

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 Arc to make sure that cloning remains inexpensive.

JSON

Represented by {Json: JSON} where JSON is a JSON object.

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.

JSON

Represented by {Binary: {part: number}}, where number represents the number of the multipart containing the binary payload. ```

Methods

impl Value

fn get_type(&self) -> Type

fn as_timestamp(&self) -> Result<&TimeStamp, TypeError>

fn as_duration(&self) -> Result<&Duration, TypeError>

Trait Implementations

impl Deserialize for Value

fn deserialize<__D>(deserializer: &mut __D) -> Result<Value, __D::Error> where __D: Deserializer

impl Serialize for Value

fn serialize<__S>(&self, serializer: &mut __S) -> Result<(), __S::Error> where __S: Serializer

impl Parser<Value> for Value

fn description() -> String

fn parse(path: Path, source: &mut JSON) -> Result<Self, ParseError>

fn from_str(source: &str) -> Result<T, ParseError>

fn take(path: Path, source: &mut JSON, field_name: &str) -> Result<T, ParseError>

fn take_opt(path: Path, source: &mut JSON, field_name: &str) -> Option<Result<T, ParseError>>

fn take_vec_opt(path: Path, source: &mut JSON, field_name: &str) -> Option<Result<Vec<T>, ParseError>>

fn take_vec(path: Path, source: &mut JSON, field_name: &str) -> Result<Vec<T>, ParseError>

impl ToJSON for Value

fn to_json(&self, parts: &mut BinaryParts) -> JSON

impl PartialOrd for Value

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

1.0.0fn lt(&self, other: &Rhs) -> bool

1.0.0fn le(&self, other: &Rhs) -> bool

1.0.0fn gt(&self, other: &Rhs) -> bool

1.0.0fn ge(&self, other: &Rhs) -> bool

Derived Implementations

impl PartialEq for Value

fn eq(&self, __arg_0: &Value) -> bool

fn ne(&self, __arg_0: &Value) -> bool

impl Clone for Value

fn clone(&self) -> Value

1.0.0fn clone_from(&mut self, source: &Self)

impl Debug for Value

fn fmt(&self, __arg_0: &mut Formatter) -> Result