Skip to content

Commit aa0ee3b

Browse files
author
David Rajchenbach-Teller
committed
Issue #11 Starting to document/doctest (de)serialization of Value
This patch introduces some documentation/doctests on Value. More still needs to be written.
1 parent 27c8ce9 commit aa0ee3b

File tree

1 file changed

+49
-10
lines changed

1 file changed

+49
-10
lines changed

src/values.rs

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,52 @@ impl PartialOrd for ExtNumeric {
311311
}
312312
}
313313

314+
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
315+
pub struct Binary {
316+
/// The actual data. We put it behind an `Arc` to make sure
317+
/// that cloning remains inexpensive.
318+
data: Arc<Vec<u8>>,
319+
/// The mime type. Should probably be an Id<MimeTypeId>.
320+
mimetype: String
321+
}
322+
314323
/// Representation of an actual value that can be sent to/received
315324
/// from a service.
325+
///
326+
/// # (De)serialization
327+
///
328+
/// Values of this state are represented by an object `{ key: value }`, where key is one of
329+
/// `Unit`, `OnOff`, `OpenClosed`, ... Note that the `value` for `Unit` is `[]`.
330+
///
331+
/// ```
332+
/// extern crate serde;
333+
/// extern crate serde_json;
334+
/// extern crate foxbox_taxonomy;
335+
///
336+
/// # fn main() {
337+
/// use foxbox_taxonomy::values::Value::*;
338+
/// use foxbox_taxonomy::values::OnOff::*;
339+
/// use foxbox_taxonomy::values::OpenClosed::*;
340+
///
341+
/// let unit = serde_json::to_string(&Unit).unwrap();
342+
/// assert_eq!(unit, "{\"Unit\":[]}");
343+
///
344+
/// let unit : foxbox_taxonomy::values::Value = serde_json::from_str("{\"Unit\":[]}").unwrap();
345+
/// assert_eq!(unit, Unit);
346+
///
347+
/// let on = serde_json::to_string(&OnOff(On)).unwrap();
348+
/// assert_eq!(on, "{\"OnOff\":\"On\"}");
349+
///
350+
/// let on : foxbox_taxonomy::values::Value = serde_json::from_str("{\"OnOff\":\"On\"}").unwrap();
351+
/// assert_eq!(on, OnOff(On));
352+
///
353+
/// let open = serde_json::to_string(&OpenClosed(Open)).unwrap();
354+
/// assert_eq!(open, "{\"OpenClosed\":\"Open\"}");
355+
///
356+
/// let open : foxbox_taxonomy::values::Value = serde_json::from_str("{\"OpenClosed\":\"Open\"}").unwrap();
357+
/// assert_eq!(open, OpenClosed(Open));
358+
/// # }
359+
/// ```
316360
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
317361
pub enum Value {
318362
Unit,
@@ -339,12 +383,7 @@ pub enum Value {
339383
Json(Arc<Json>),
340384

341385
/// Binary data.
342-
Binary {
343-
/// The actual data. We put it behind an `Arc` to make sure
344-
/// that cloning remains inexpensive.
345-
data: Arc<Vec<u8>>,
346-
mimetype: String
347-
}
386+
Binary(Binary),
348387
}
349388

350389
impl Value {
@@ -359,7 +398,7 @@ impl Value {
359398
Value::Temperature(_) => Type::Temperature,
360399
Value::Color(_) => Type::Color,
361400
Value::Json(_) => Type::Json,
362-
Value::Binary{..} => Type::Binary,
401+
Value::Binary(_) => Type::Binary,
363402
Value::ExtBool(_) => Type::ExtBool,
364403
Value::ExtNumeric(_) => Type::ExtNumeric,
365404
}
@@ -421,9 +460,9 @@ impl PartialOrd for Value {
421460
(&Json(ref a), &Json(ref b)) => a.partial_cmp(b),
422461
(&Json(_), _) => None,
423462

424-
(&Binary{mimetype: ref a_mimetype, data: ref a_data},
425-
&Binary{mimetype: ref b_mimetype, data: ref b_data}) if a_mimetype == b_mimetype => a_data.partial_cmp(b_data),
426-
(&Binary{..}, _) => None,
463+
(&Binary(self::Binary {mimetype: ref a_mimetype, data: ref a_data}),
464+
&Binary(self::Binary {mimetype: ref b_mimetype, data: ref b_data})) if a_mimetype == b_mimetype => a_data.partial_cmp(b_data),
465+
(&Binary(_), _) => None,
427466
}
428467
}
429468
}

0 commit comments

Comments
 (0)