2020-12-15 10:53:31 +00:00
|
|
|
// Take a look at the license at the top of the repository in the LICENSE file.
|
2018-07-16 19:46:10 +00:00
|
|
|
|
2018-07-22 13:48:26 +00:00
|
|
|
use serde::de;
|
2018-07-29 14:04:39 +00:00
|
|
|
use serde::de::{Deserialize, Deserializer, EnumAccess, SeqAccess, VariantAccess, Visitor};
|
2018-07-22 14:55:24 +00:00
|
|
|
use serde::ser::{Serialize, SerializeSeq, SerializeTuple, Serializer};
|
2018-07-16 19:46:10 +00:00
|
|
|
|
|
|
|
use std::fmt;
|
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
use crate::Caps;
|
|
|
|
use crate::CapsFeatures;
|
|
|
|
use crate::CapsFeaturesRef;
|
|
|
|
use crate::CapsRef;
|
|
|
|
use crate::Structure;
|
|
|
|
use crate::StructureRef;
|
2018-07-16 19:46:10 +00:00
|
|
|
|
2018-07-29 14:04:39 +00:00
|
|
|
enum CapsVariantKinds {
|
|
|
|
Any,
|
|
|
|
Empty,
|
|
|
|
Some,
|
|
|
|
}
|
|
|
|
|
|
|
|
const CAPS_VARIANT_ANY_ID: u32 = 0;
|
|
|
|
const CAPS_VARIANT_ANY_STR: &str = "Any";
|
|
|
|
const CAPS_VARIANT_EMPTY_ID: u32 = 1;
|
|
|
|
const CAPS_VARIANT_EMPTY_STR: &str = "Empty";
|
|
|
|
const CAPS_VARIANT_SOME_ID: u32 = 2;
|
|
|
|
const CAPS_VARIANT_SOME_STR: &str = "Some";
|
|
|
|
|
|
|
|
const CAPS_VARIANT_NAMES: &[&str] = &[
|
|
|
|
&CAPS_VARIANT_ANY_STR,
|
|
|
|
&CAPS_VARIANT_EMPTY_STR,
|
|
|
|
&CAPS_VARIANT_SOME_STR,
|
|
|
|
];
|
|
|
|
|
2018-09-28 09:00:08 +00:00
|
|
|
struct CapsItemSe<'a>(&'a StructureRef, Option<&'a CapsFeaturesRef>);
|
2018-07-22 13:48:26 +00:00
|
|
|
impl<'a> Serialize for CapsItemSe<'a> {
|
|
|
|
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
|
|
|
let mut tup = serializer.serialize_tuple(2)?;
|
|
|
|
tup.serialize_element(self.0)?;
|
2018-09-28 09:00:08 +00:00
|
|
|
tup.serialize_element(&self.1)?;
|
2018-07-22 13:48:26 +00:00
|
|
|
tup.end()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 14:04:39 +00:00
|
|
|
struct CapsForIterSe<'a>(&'a CapsRef);
|
|
|
|
impl<'a> Serialize for CapsForIterSe<'a> {
|
2018-07-16 19:46:10 +00:00
|
|
|
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
2018-09-28 09:00:08 +00:00
|
|
|
let iter = self.0.iter_with_features();
|
2018-07-16 19:46:10 +00:00
|
|
|
let size = iter.size_hint().0;
|
|
|
|
if size > 0 {
|
|
|
|
let mut seq = serializer.serialize_seq(Some(size))?;
|
2018-09-28 09:00:08 +00:00
|
|
|
for (structure, features) in iter {
|
2018-10-28 13:47:02 +00:00
|
|
|
let features = if !features.is_any()
|
2020-11-21 13:46:48 +00:00
|
|
|
&& features.is_equal(crate::CAPS_FEATURES_MEMORY_SYSTEM_MEMORY.as_ref())
|
2018-09-28 09:00:08 +00:00
|
|
|
{
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(features)
|
|
|
|
};
|
|
|
|
seq.serialize_element(&CapsItemSe(structure, features))?;
|
2018-07-16 19:46:10 +00:00
|
|
|
}
|
|
|
|
seq.end()
|
|
|
|
} else {
|
|
|
|
let seq = serializer.serialize_seq(None)?;
|
|
|
|
seq.end()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 14:04:39 +00:00
|
|
|
impl Serialize for CapsRef {
|
|
|
|
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
|
|
|
if self.is_any() {
|
|
|
|
serializer.serialize_unit_variant(
|
|
|
|
stringify!(Caps),
|
|
|
|
CAPS_VARIANT_ANY_ID,
|
|
|
|
CAPS_VARIANT_ANY_STR,
|
|
|
|
)
|
|
|
|
} else if self.is_empty() {
|
|
|
|
serializer.serialize_unit_variant(
|
|
|
|
stringify!(Caps),
|
|
|
|
CAPS_VARIANT_EMPTY_ID,
|
|
|
|
CAPS_VARIANT_EMPTY_STR,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
serializer.serialize_newtype_variant(
|
|
|
|
stringify!(Caps),
|
|
|
|
CAPS_VARIANT_SOME_ID,
|
|
|
|
CAPS_VARIANT_SOME_STR,
|
|
|
|
&CapsForIterSe(&self),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-22 13:48:26 +00:00
|
|
|
impl Serialize for Caps {
|
2018-07-16 19:46:10 +00:00
|
|
|
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
|
|
|
self.as_ref().serialize(serializer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-28 09:00:08 +00:00
|
|
|
struct CapsItemDe(Structure, Option<CapsFeatures>);
|
2018-07-22 13:48:26 +00:00
|
|
|
|
|
|
|
struct CapsItemVisitor;
|
|
|
|
impl<'de> Visitor<'de> for CapsItemVisitor {
|
|
|
|
type Value = CapsItemDe;
|
|
|
|
|
|
|
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
formatter.write_str("a tuple `(Structure, Option<CapsFeature>)`")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
|
2018-07-22 14:55:24 +00:00
|
|
|
let structure = seq
|
|
|
|
.next_element::<Structure>()?
|
2018-10-11 08:25:28 +00:00
|
|
|
.ok_or_else(|| de::Error::custom("Expected a `Structure` for `Caps` item"))?;
|
2018-10-28 13:47:02 +00:00
|
|
|
let features_option = seq.next_element::<Option<CapsFeatures>>()?.ok_or_else(|| {
|
|
|
|
de::Error::custom("Expected an `Option<CapsFeature>` for `Caps` item")
|
|
|
|
})?;
|
2018-09-28 09:00:08 +00:00
|
|
|
|
|
|
|
Ok(CapsItemDe(structure, features_option))
|
2018-07-22 13:48:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'de> Deserialize<'de> for CapsItemDe {
|
|
|
|
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<CapsItemDe, D::Error> {
|
2020-03-22 14:18:47 +00:00
|
|
|
skip_assert_initialized!();
|
2018-07-22 13:48:26 +00:00
|
|
|
deserializer.deserialize_tuple(2, CapsItemVisitor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 14:04:39 +00:00
|
|
|
struct CapsSome(Caps);
|
|
|
|
|
|
|
|
struct CapsSomeVisitor;
|
|
|
|
impl<'de> Visitor<'de> for CapsSomeVisitor {
|
|
|
|
type Value = CapsSome;
|
2018-07-16 19:46:10 +00:00
|
|
|
|
|
|
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
2018-07-22 13:48:26 +00:00
|
|
|
formatter.write_str("a sequence of `(Structure, Option<CapsFeature>)`")
|
2018-07-16 19:46:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
|
|
|
|
let mut caps = Caps::new_empty();
|
|
|
|
{
|
|
|
|
let caps = caps.get_mut().unwrap();
|
2018-07-22 13:48:26 +00:00
|
|
|
while let Some(caps_item) = seq.next_element::<CapsItemDe>()? {
|
2018-09-28 09:00:08 +00:00
|
|
|
caps.append_structure_full(caps_item.0, caps_item.1);
|
2018-07-16 19:46:10 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-29 14:04:39 +00:00
|
|
|
Ok(CapsSome(caps))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'de> Deserialize<'de> for CapsSome {
|
|
|
|
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<CapsSome, D::Error> {
|
2020-03-22 14:18:47 +00:00
|
|
|
skip_assert_initialized!();
|
2018-07-29 14:04:39 +00:00
|
|
|
deserializer.deserialize_seq(CapsSomeVisitor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CapsVariantKindsVisitor;
|
|
|
|
impl<'de> Visitor<'de> for CapsVariantKindsVisitor {
|
|
|
|
type Value = CapsVariantKinds;
|
|
|
|
|
|
|
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
formatter.write_str("a Caps variant kind (`Any`, `None` or `Some`)")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_u32<E: de::Error>(self, value: u32) -> Result<Self::Value, E> {
|
|
|
|
match value {
|
|
|
|
CAPS_VARIANT_ANY_ID => Ok(CapsVariantKinds::Any),
|
|
|
|
CAPS_VARIANT_EMPTY_ID => Ok(CapsVariantKinds::Empty),
|
|
|
|
CAPS_VARIANT_SOME_ID => Ok(CapsVariantKinds::Some),
|
|
|
|
_ => Err(de::Error::invalid_value(
|
2019-02-28 08:54:32 +00:00
|
|
|
de::Unexpected::Unsigned(u64::from(value)),
|
2018-07-29 14:04:39 +00:00
|
|
|
&self,
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_str<E: de::Error>(self, value: &str) -> Result<Self::Value, E> {
|
|
|
|
match value {
|
|
|
|
CAPS_VARIANT_ANY_STR => Ok(CapsVariantKinds::Any),
|
|
|
|
CAPS_VARIANT_EMPTY_STR => Ok(CapsVariantKinds::Empty),
|
|
|
|
CAPS_VARIANT_SOME_STR => Ok(CapsVariantKinds::Some),
|
|
|
|
_ => Err(de::Error::unknown_variant(value, CAPS_VARIANT_NAMES)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'de> Deserialize<'de> for CapsVariantKinds {
|
|
|
|
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
2020-03-22 14:18:47 +00:00
|
|
|
skip_assert_initialized!();
|
2018-07-29 14:04:39 +00:00
|
|
|
deserializer.deserialize_identifier(CapsVariantKindsVisitor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CapsVisitor;
|
|
|
|
impl<'de> Visitor<'de> for CapsVisitor {
|
|
|
|
type Value = Caps;
|
|
|
|
|
|
|
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
formatter.write_str("a Caps enum (`Any`, `None` or `Some()`)")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_enum<A: EnumAccess<'de>>(self, data: A) -> Result<Self::Value, A::Error> {
|
|
|
|
let res = match data.variant()? {
|
|
|
|
(CapsVariantKinds::Any, _v) => Caps::new_any(),
|
|
|
|
(CapsVariantKinds::Empty, _v) => Caps::new_empty(),
|
|
|
|
(CapsVariantKinds::Some, v) => v
|
|
|
|
.newtype_variant::<CapsSome>()
|
|
|
|
.map(|caps_some| caps_some.0)?,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(res)
|
2018-07-16 19:46:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'de> Deserialize<'de> for Caps {
|
|
|
|
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
2020-03-22 14:18:47 +00:00
|
|
|
skip_assert_initialized!();
|
2018-07-29 14:04:39 +00:00
|
|
|
deserializer.deserialize_enum(stringify!(Caps), CAPS_VARIANT_NAMES, CapsVisitor)
|
2018-07-16 19:46:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-11-21 13:46:48 +00:00
|
|
|
use crate::Array;
|
|
|
|
use crate::Caps;
|
|
|
|
use crate::CapsFeatures;
|
|
|
|
use crate::Fraction;
|
2018-07-16 19:46:10 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_serialize() {
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::init().unwrap();
|
2018-07-16 19:46:10 +00:00
|
|
|
|
|
|
|
let caps = Caps::builder("foo/bar")
|
|
|
|
.field("int", &12)
|
|
|
|
.field("bool", &true)
|
|
|
|
.field("string", &"bla")
|
|
|
|
.field("fraction", &Fraction::new(1, 2))
|
|
|
|
.field("array", &Array::new(&[&1, &2]))
|
|
|
|
.build();
|
|
|
|
|
2021-01-01 19:35:43 +00:00
|
|
|
let pretty_config = ron::ser::PrettyConfig::new().with_new_line("".to_string());
|
2018-07-16 19:46:10 +00:00
|
|
|
|
2019-12-22 07:59:23 +00:00
|
|
|
let res = ron::ser::to_string_pretty(&caps, pretty_config);
|
2018-07-16 19:46:10 +00:00
|
|
|
assert_eq!(
|
2018-07-22 14:55:24 +00:00
|
|
|
Ok(concat!(
|
2018-07-29 14:04:39 +00:00
|
|
|
"Some([",
|
2018-07-22 14:55:24 +00:00
|
|
|
" ((\"foo/bar\", [",
|
|
|
|
" (\"int\", \"i32\", 12),",
|
|
|
|
" (\"bool\", \"bool\", true),",
|
2019-08-16 22:55:09 +00:00
|
|
|
" (\"string\", \"String\", Some(\"bla\")),",
|
2018-07-22 14:55:24 +00:00
|
|
|
" (\"fraction\", \"Fraction\", (1, 2)),",
|
|
|
|
" (\"array\", \"Array\", [",
|
|
|
|
" (\"i32\", 1),",
|
|
|
|
" (\"i32\", 2),",
|
|
|
|
" ]),",
|
|
|
|
" ]), None),",
|
2018-07-29 14:04:39 +00:00
|
|
|
"])"
|
2018-10-08 12:02:23 +00:00
|
|
|
)
|
|
|
|
.to_owned()),
|
2018-07-16 19:46:10 +00:00
|
|
|
res,
|
|
|
|
);
|
2018-07-29 14:04:39 +00:00
|
|
|
|
2018-09-28 09:00:08 +00:00
|
|
|
let caps = Caps::builder("foo/bar")
|
|
|
|
.field("int", &12)
|
|
|
|
.field("bool", &true)
|
|
|
|
.field("string", &"bla")
|
|
|
|
.field("fraction", &Fraction::new(1, 2))
|
|
|
|
.field("array", &Array::new(&[&1, &2]))
|
|
|
|
.features(&["foo:bar", "foo:baz"])
|
|
|
|
.build();
|
|
|
|
|
2021-01-01 19:35:43 +00:00
|
|
|
let pretty_config = ron::ser::PrettyConfig::new().with_new_line("".to_string());
|
2018-09-28 09:00:08 +00:00
|
|
|
|
2019-12-22 07:59:23 +00:00
|
|
|
let res = ron::ser::to_string_pretty(&caps, pretty_config);
|
2018-09-28 09:00:08 +00:00
|
|
|
assert_eq!(
|
|
|
|
Ok(concat!(
|
|
|
|
"Some([",
|
|
|
|
" ((\"foo/bar\", [",
|
|
|
|
" (\"int\", \"i32\", 12),",
|
|
|
|
" (\"bool\", \"bool\", true),",
|
2019-08-16 22:55:09 +00:00
|
|
|
" (\"string\", \"String\", Some(\"bla\")),",
|
2018-09-28 09:00:08 +00:00
|
|
|
" (\"fraction\", \"Fraction\", (1, 2)),",
|
|
|
|
" (\"array\", \"Array\", [",
|
|
|
|
" (\"i32\", 1),",
|
|
|
|
" (\"i32\", 2),",
|
|
|
|
" ]),",
|
|
|
|
" ]), Some(Some([",
|
|
|
|
" \"foo:bar\",",
|
|
|
|
" \"foo:baz\",",
|
|
|
|
" ]))),",
|
|
|
|
"])"
|
2018-10-08 12:02:23 +00:00
|
|
|
)
|
|
|
|
.to_owned()),
|
2018-09-28 09:00:08 +00:00
|
|
|
res,
|
|
|
|
);
|
|
|
|
|
|
|
|
let caps = Caps::builder("foo/bar")
|
|
|
|
.field("int", &12)
|
|
|
|
.field("bool", &true)
|
|
|
|
.field("string", &"bla")
|
|
|
|
.field("fraction", &Fraction::new(1, 2))
|
|
|
|
.field("array", &Array::new(&[&1, &2]))
|
|
|
|
.any_features()
|
|
|
|
.build();
|
|
|
|
|
2021-01-01 19:35:43 +00:00
|
|
|
let pretty_config = ron::ser::PrettyConfig::new().with_new_line("".to_string());
|
2018-09-28 09:00:08 +00:00
|
|
|
|
|
|
|
let res = ron::ser::to_string_pretty(&caps, pretty_config.clone());
|
|
|
|
assert_eq!(
|
|
|
|
Ok(concat!(
|
|
|
|
"Some([",
|
|
|
|
" ((\"foo/bar\", [",
|
|
|
|
" (\"int\", \"i32\", 12),",
|
|
|
|
" (\"bool\", \"bool\", true),",
|
2019-08-16 22:55:09 +00:00
|
|
|
" (\"string\", \"String\", Some(\"bla\")),",
|
2018-09-28 09:00:08 +00:00
|
|
|
" (\"fraction\", \"Fraction\", (1, 2)),",
|
|
|
|
" (\"array\", \"Array\", [",
|
|
|
|
" (\"i32\", 1),",
|
|
|
|
" (\"i32\", 2),",
|
|
|
|
" ]),",
|
|
|
|
" ]), Some(Any)),",
|
|
|
|
"])"
|
2018-10-08 12:02:23 +00:00
|
|
|
)
|
|
|
|
.to_owned()),
|
2018-09-28 09:00:08 +00:00
|
|
|
res,
|
|
|
|
);
|
|
|
|
|
2018-07-29 14:04:39 +00:00
|
|
|
let caps_any = Caps::new_any();
|
|
|
|
let res = ron::ser::to_string_pretty(&caps_any, pretty_config.clone());
|
|
|
|
assert_eq!(Ok("Any".to_owned()), res);
|
|
|
|
|
|
|
|
let caps_empty = Caps::new_empty();
|
2019-12-22 07:59:23 +00:00
|
|
|
let res = ron::ser::to_string_pretty(&caps_empty, pretty_config);
|
2018-07-29 14:04:39 +00:00
|
|
|
assert_eq!(Ok("Empty".to_owned()), res);
|
2018-07-16 19:46:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_deserialize() {
|
2020-11-21 13:46:48 +00:00
|
|
|
use crate::Structure;
|
2018-07-16 19:46:10 +00:00
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::init().unwrap();
|
2018-07-16 19:46:10 +00:00
|
|
|
|
2018-07-29 14:04:39 +00:00
|
|
|
let caps_ron = "Any";
|
|
|
|
let caps: Caps = ron::de::from_str(caps_ron).unwrap();
|
|
|
|
assert!(caps.is_any());
|
|
|
|
|
|
|
|
let caps_ron = "Empty";
|
|
|
|
let caps: Caps = ron::de::from_str(caps_ron).unwrap();
|
|
|
|
assert!(caps.is_empty());
|
|
|
|
|
2018-07-16 19:46:10 +00:00
|
|
|
let caps_ron = r#"
|
2018-07-29 14:04:39 +00:00
|
|
|
Some([
|
2018-07-22 13:48:26 +00:00
|
|
|
(
|
|
|
|
("foo/bar", [
|
|
|
|
("int", "i32", 12),
|
|
|
|
("bool", "bool", true),
|
2019-08-16 22:55:09 +00:00
|
|
|
("string", "String", Some("bla")),
|
2018-07-22 13:48:26 +00:00
|
|
|
("fraction", "Fraction", (1, 2)),
|
|
|
|
("array", "Array", [
|
|
|
|
("i32", 1),
|
|
|
|
("i32", 2),
|
|
|
|
]),
|
2018-07-16 19:46:10 +00:00
|
|
|
]),
|
2018-07-22 13:48:26 +00:00
|
|
|
None,
|
|
|
|
),
|
2018-07-29 14:04:39 +00:00
|
|
|
])"#;
|
2018-07-16 19:46:10 +00:00
|
|
|
let caps: Caps = ron::de::from_str(caps_ron).unwrap();
|
|
|
|
let s = caps.get_structure(0).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
s,
|
|
|
|
Structure::new(
|
|
|
|
"foo/bar",
|
|
|
|
&[
|
|
|
|
("int", &12),
|
|
|
|
("bool", &true),
|
|
|
|
("string", &"bla"),
|
|
|
|
("fraction", &Fraction::new(1, 2)),
|
|
|
|
("array", &Array::new(&[&1, &2])),
|
|
|
|
],
|
2018-10-08 12:02:23 +00:00
|
|
|
)
|
|
|
|
.as_ref()
|
2018-07-16 19:46:10 +00:00
|
|
|
);
|
2018-09-28 09:00:08 +00:00
|
|
|
|
|
|
|
let caps_ron = r#"
|
|
|
|
Some([
|
|
|
|
(
|
|
|
|
("foo/bar", [
|
|
|
|
("int", "i32", 12),
|
|
|
|
("bool", "bool", true),
|
2019-08-16 22:55:09 +00:00
|
|
|
("string", "String", None),
|
2018-09-28 09:00:08 +00:00
|
|
|
("fraction", "Fraction", (1, 2)),
|
|
|
|
("array", "Array", [
|
|
|
|
("i32", 1),
|
|
|
|
("i32", 2),
|
|
|
|
]),
|
|
|
|
]),
|
|
|
|
Some(Some(["foo:bar", "foo:baz"])),
|
|
|
|
),
|
|
|
|
])"#;
|
|
|
|
let caps: Caps = ron::de::from_str(caps_ron).unwrap();
|
|
|
|
let s = caps.get_structure(0).unwrap();
|
2019-08-16 22:55:09 +00:00
|
|
|
let str_none: Option<&str> = None;
|
2018-09-28 09:00:08 +00:00
|
|
|
assert_eq!(
|
|
|
|
s,
|
|
|
|
Structure::new(
|
|
|
|
"foo/bar",
|
|
|
|
&[
|
|
|
|
("int", &12),
|
|
|
|
("bool", &true),
|
2019-08-16 22:55:09 +00:00
|
|
|
("string", &str_none),
|
2018-09-28 09:00:08 +00:00
|
|
|
("fraction", &Fraction::new(1, 2)),
|
|
|
|
("array", &Array::new(&[&1, &2])),
|
|
|
|
],
|
2018-10-08 12:02:23 +00:00
|
|
|
)
|
|
|
|
.as_ref()
|
2018-09-28 09:00:08 +00:00
|
|
|
);
|
|
|
|
let f = caps.get_features(0).unwrap();
|
|
|
|
assert!(f.is_equal(CapsFeatures::new(&["foo:bar", "foo:baz"]).as_ref()));
|
|
|
|
|
|
|
|
let caps_ron = r#"
|
|
|
|
Some([
|
|
|
|
(
|
|
|
|
("foo/bar", [
|
|
|
|
("int", "i32", 12),
|
|
|
|
("bool", "bool", true),
|
2019-08-16 22:55:09 +00:00
|
|
|
("string", "String", Some("bla")),
|
2018-09-28 09:00:08 +00:00
|
|
|
("fraction", "Fraction", (1, 2)),
|
|
|
|
("array", "Array", [
|
|
|
|
("i32", 1),
|
|
|
|
("i32", 2),
|
|
|
|
]),
|
|
|
|
]),
|
|
|
|
Some(Any),
|
|
|
|
),
|
|
|
|
])"#;
|
|
|
|
let caps: Caps = ron::de::from_str(caps_ron).unwrap();
|
|
|
|
let s = caps.get_structure(0).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
s,
|
|
|
|
Structure::new(
|
|
|
|
"foo/bar",
|
|
|
|
&[
|
|
|
|
("int", &12),
|
|
|
|
("bool", &true),
|
|
|
|
("string", &"bla"),
|
|
|
|
("fraction", &Fraction::new(1, 2)),
|
|
|
|
("array", &Array::new(&[&1, &2])),
|
|
|
|
],
|
2018-10-08 12:02:23 +00:00
|
|
|
)
|
|
|
|
.as_ref()
|
2018-09-28 09:00:08 +00:00
|
|
|
);
|
|
|
|
let f = caps.get_features(0).unwrap();
|
|
|
|
assert!(f.is_any());
|
2018-07-16 19:46:10 +00:00
|
|
|
}
|
2018-07-29 14:04:39 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_serde_roundtrip() {
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::init().unwrap();
|
2018-07-29 14:04:39 +00:00
|
|
|
|
|
|
|
let caps = Caps::new_any();
|
|
|
|
let caps_ser = ron::ser::to_string(&caps).unwrap();
|
|
|
|
let caps_de: Caps = ron::de::from_str(caps_ser.as_str()).unwrap();
|
|
|
|
assert!(caps_de.is_any());
|
|
|
|
|
|
|
|
let caps = Caps::new_empty();
|
|
|
|
let caps_ser = ron::ser::to_string(&caps).unwrap();
|
|
|
|
let caps_de: Caps = ron::de::from_str(caps_ser.as_str()).unwrap();
|
|
|
|
assert!(caps_de.is_empty());
|
|
|
|
|
|
|
|
let caps = Caps::builder("foo/bar")
|
|
|
|
.field("int", &12)
|
|
|
|
.field("bool", &true)
|
|
|
|
.field("string", &"bla")
|
|
|
|
.field("fraction", &Fraction::new(1, 2))
|
|
|
|
.field("array", &Array::new(&[&1, &2]))
|
|
|
|
.build();
|
|
|
|
let caps_ser = ron::ser::to_string(&caps).unwrap();
|
|
|
|
let caps_de: Caps = ron::de::from_str(caps_ser.as_str()).unwrap();
|
|
|
|
assert!(caps_de.is_strictly_equal(&caps));
|
2018-09-28 09:00:08 +00:00
|
|
|
|
|
|
|
let caps = Caps::builder("foo/bar")
|
|
|
|
.field("int", &12)
|
|
|
|
.field("bool", &true)
|
|
|
|
.field("string", &"bla")
|
|
|
|
.field("fraction", &Fraction::new(1, 2))
|
|
|
|
.field("array", &Array::new(&[&1, &2]))
|
|
|
|
.features(&["foo:bar", "foo:baz"])
|
|
|
|
.build();
|
|
|
|
let caps_ser = ron::ser::to_string(&caps).unwrap();
|
|
|
|
let caps_de: Caps = ron::de::from_str(caps_ser.as_str()).unwrap();
|
|
|
|
assert!(caps_de.is_strictly_equal(&caps));
|
|
|
|
|
|
|
|
let caps = Caps::builder("foo/bar")
|
|
|
|
.field("int", &12)
|
|
|
|
.field("bool", &true)
|
|
|
|
.field("string", &"bla")
|
|
|
|
.field("fraction", &Fraction::new(1, 2))
|
|
|
|
.field("array", &Array::new(&[&1, &2]))
|
|
|
|
.any_features()
|
|
|
|
.build();
|
|
|
|
let caps_ser = ron::ser::to_string(&caps).unwrap();
|
|
|
|
let caps_de: Caps = ron::de::from_str(caps_ser.as_str()).unwrap();
|
|
|
|
assert!(caps_de.is_strictly_equal(&caps));
|
2018-07-29 14:04:39 +00:00
|
|
|
}
|
2018-07-16 19:46:10 +00:00
|
|
|
}
|