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
|
|
|
|
2019-08-19 13:32:40 +00:00
|
|
|
use glib::{Date, ToValue};
|
2018-07-16 19:46:10 +00:00
|
|
|
|
|
|
|
use serde::de;
|
2018-07-21 13:09:22 +00:00
|
|
|
use serde::de::{Deserialize, DeserializeSeed, Deserializer, SeqAccess, Visitor};
|
2018-07-16 19:46:10 +00:00
|
|
|
use serde::ser;
|
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::Buffer;
|
|
|
|
use crate::DateTime;
|
|
|
|
use crate::Sample;
|
2018-07-16 19:46:10 +00:00
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
use crate::date_time_serde;
|
|
|
|
use crate::value::*;
|
|
|
|
use crate::value_serde::*;
|
2018-07-16 19:46:10 +00:00
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
use crate::Structure;
|
|
|
|
use crate::StructureRef;
|
2018-07-16 19:46:10 +00:00
|
|
|
|
2020-03-12 18:17:15 +00:00
|
|
|
struct FieldSe<'a>(&'static str, &'a glib::SendValue);
|
2018-07-16 19:46:10 +00:00
|
|
|
impl<'a> Serialize for FieldSe<'a> {
|
|
|
|
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
|
|
|
ser_value!(self.1, |type_, value| {
|
|
|
|
let mut tup = serializer.serialize_tuple(3)?;
|
|
|
|
tup.serialize_element(self.0)?;
|
|
|
|
tup.serialize_element(type_)?;
|
|
|
|
tup.serialize_element(&value)?;
|
|
|
|
tup.end()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct StructureForIter<'a>(&'a StructureRef);
|
|
|
|
impl<'a> Serialize for StructureForIter<'a> {
|
|
|
|
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
|
|
|
let iter = self.0.iter();
|
|
|
|
let size = iter.size_hint().0;
|
|
|
|
if size > 0 {
|
|
|
|
let mut seq = serializer.serialize_seq(Some(size))?;
|
|
|
|
for field in iter {
|
|
|
|
seq.serialize_element(&FieldSe(field.0, field.1))?;
|
|
|
|
}
|
|
|
|
seq.end()
|
|
|
|
} else {
|
|
|
|
let seq = serializer.serialize_seq(None)?;
|
|
|
|
seq.end()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for StructureRef {
|
|
|
|
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
|
|
|
let mut tup = serializer.serialize_tuple(2)?;
|
|
|
|
tup.serialize_element(self.get_name())?;
|
|
|
|
tup.serialize_element(&StructureForIter(self))?;
|
|
|
|
tup.end()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for Structure {
|
|
|
|
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
|
|
|
self.as_ref().serialize(serializer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct FieldDe(String, SendValue);
|
|
|
|
impl From<FieldDe> for (String, glib::SendValue) {
|
|
|
|
fn from(field_de: FieldDe) -> Self {
|
2020-03-22 14:18:47 +00:00
|
|
|
skip_assert_initialized!();
|
2018-07-16 19:46:10 +00:00
|
|
|
(field_de.0, field_de.1.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct FieldVisitor;
|
|
|
|
impl<'de> Visitor<'de> for FieldVisitor {
|
|
|
|
type Value = FieldDe;
|
|
|
|
|
|
|
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
formatter.write_str(
|
2018-07-22 14:55:24 +00:00
|
|
|
"a tuple of 3 elements (name: `String`, type name: `String`, value: `Value`)",
|
2018-07-16 19:46:10 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
|
2018-07-22 14:55:24 +00:00
|
|
|
let name = seq
|
|
|
|
.next_element::<String>()?
|
2018-10-11 08:25:28 +00:00
|
|
|
.ok_or_else(|| de::Error::custom("Expected a value for `Value` name"))?;
|
2018-07-22 14:55:24 +00:00
|
|
|
let type_name = seq
|
|
|
|
.next_element::<String>()?
|
2018-10-11 08:25:28 +00:00
|
|
|
.ok_or_else(|| de::Error::custom("Expected a value for `Value` type"))?;
|
2018-07-17 20:24:41 +00:00
|
|
|
let send_value = de_send_value!(type_name, seq)?
|
2018-10-11 08:25:28 +00:00
|
|
|
.ok_or_else(|| de::Error::custom("Expected a value for `Value`"))?;
|
2018-07-17 20:24:41 +00:00
|
|
|
Ok(FieldDe(name, send_value))
|
2018-07-16 19:46:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'de> Deserialize<'de> for FieldDe {
|
|
|
|
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
2020-03-22 14:18:47 +00:00
|
|
|
skip_assert_initialized!();
|
2018-07-16 19:46:10 +00:00
|
|
|
deserializer.deserialize_tuple(3, FieldVisitor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-21 13:09:22 +00:00
|
|
|
struct FieldsDe<'a>(&'a mut StructureRef);
|
2018-07-16 19:46:10 +00:00
|
|
|
|
2018-07-21 13:09:22 +00:00
|
|
|
struct FieldsVisitor<'a>(&'a mut StructureRef);
|
|
|
|
impl<'de, 'a> Visitor<'de> for FieldsVisitor<'a> {
|
|
|
|
type Value = ();
|
2018-07-16 19:46:10 +00:00
|
|
|
|
|
|
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
2018-07-21 13:09:22 +00:00
|
|
|
formatter.write_str("a sequence of `Structure` `Field`s")
|
2018-07-16 19:46:10 +00:00
|
|
|
}
|
|
|
|
|
2018-07-21 13:09:22 +00:00
|
|
|
fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<(), A::Error> {
|
2018-07-16 19:46:10 +00:00
|
|
|
while let Some(field) = seq.next_element::<FieldDe>()? {
|
|
|
|
let (name, value): (String, glib::SendValue) = field.into();
|
2018-07-21 13:09:22 +00:00
|
|
|
self.0.set_value(name.as_str(), value);
|
2018-07-16 19:46:10 +00:00
|
|
|
}
|
|
|
|
|
2018-07-21 13:09:22 +00:00
|
|
|
Ok(())
|
2018-07-16 19:46:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-21 13:09:22 +00:00
|
|
|
impl<'de, 'a> DeserializeSeed<'de> for FieldsDe<'a> {
|
|
|
|
type Value = ();
|
|
|
|
|
|
|
|
fn deserialize<D: Deserializer<'de>>(self, deserializer: D) -> Result<(), D::Error> {
|
2020-03-22 14:18:47 +00:00
|
|
|
skip_assert_initialized!();
|
2018-07-21 13:09:22 +00:00
|
|
|
deserializer.deserialize_seq(FieldsVisitor(self.0))
|
2018-07-16 19:46:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct StructureVisitor;
|
|
|
|
impl<'de> Visitor<'de> for StructureVisitor {
|
|
|
|
type Value = Structure;
|
|
|
|
|
|
|
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
2018-07-21 13:09:22 +00:00
|
|
|
formatter.write_str("a `Structure` (name, sequence of `Field`s)")
|
2018-07-16 19:46:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
|
2018-07-22 14:55:24 +00:00
|
|
|
let name = seq
|
|
|
|
.next_element::<String>()?
|
2018-10-11 08:25:28 +00:00
|
|
|
.ok_or_else(|| de::Error::custom("Expected a name for the `Structure`"))?;
|
2018-07-21 13:09:22 +00:00
|
|
|
let mut structure = Structure::new_empty(&name);
|
|
|
|
seq.next_element_seed(FieldsDe(structure.as_mut()))?
|
2018-10-11 08:25:28 +00:00
|
|
|
.ok_or_else(|| de::Error::custom("Expected a sequence of `Field`s"))?;
|
2018-07-16 19:46:10 +00:00
|
|
|
|
|
|
|
Ok(structure)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'de> Deserialize<'de> for Structure {
|
|
|
|
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
2020-03-22 14:18:47 +00:00
|
|
|
skip_assert_initialized!();
|
2018-07-16 19:46:10 +00:00
|
|
|
deserializer.deserialize_tuple(2, StructureVisitor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-08-19 13:32:40 +00:00
|
|
|
use glib::{Date, DateMonth};
|
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
use crate::Array;
|
|
|
|
use crate::DateTime;
|
|
|
|
use crate::Fraction;
|
|
|
|
use crate::Structure;
|
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 s = Structure::builder("test")
|
|
|
|
.field("f1", &"abc")
|
|
|
|
.field("f2", &String::from("bcd"))
|
|
|
|
.field("f3", &123i32)
|
|
|
|
.field("fraction", &Fraction::new(1, 2))
|
2019-08-19 13:32:40 +00:00
|
|
|
.field("date", &Date::new_dmy(19, DateMonth::August, 2019))
|
|
|
|
.field(
|
|
|
|
"date_time",
|
2020-03-22 10:23:16 +00:00
|
|
|
&DateTime::new(2f32, 2019, 8, 19, 13, 34, 42f64).unwrap(),
|
2019-08-19 13:32:40 +00:00
|
|
|
)
|
2018-07-16 19:46:10 +00:00
|
|
|
.field("array", &Array::new(&[&1, &2]))
|
|
|
|
.build();
|
|
|
|
|
|
|
|
let mut pretty_config = ron::ser::PrettyConfig::default();
|
|
|
|
pretty_config.new_line = "".to_string();
|
|
|
|
|
|
|
|
let res = ron::ser::to_string_pretty(&s, pretty_config);
|
|
|
|
assert_eq!(
|
2018-07-22 14:55:24 +00:00
|
|
|
Ok(concat!(
|
2019-08-19 13:32:40 +00:00
|
|
|
r#"("test", ["#,
|
|
|
|
r#" ("f1", "String", Some("abc")),"#,
|
|
|
|
r#" ("f2", "String", Some("bcd")),"#,
|
|
|
|
r#" ("f3", "i32", 123),"#,
|
|
|
|
r#" ("fraction", "Fraction", (1, 2)),"#,
|
|
|
|
r#" ("date", "Date", Some(YMD(2019, 8, 19))),"#,
|
|
|
|
r#" ("date_time", "DateTime", Some(YMDhmsTz(2019, 8, 19, 13, 34, 42, 2))),"#,
|
|
|
|
r#" ("array", "Array", ["#,
|
|
|
|
r#" ("i32", 1),"#,
|
|
|
|
r#" ("i32", 2),"#,
|
|
|
|
r#" ]),"#,
|
|
|
|
r#"])"#,
|
2018-10-08 12:02:23 +00:00
|
|
|
)
|
|
|
|
.to_owned()),
|
2018-07-16 19:46:10 +00:00
|
|
|
res,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_deserialize() {
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::init().unwrap();
|
2018-07-16 19:46:10 +00:00
|
|
|
|
|
|
|
let s_ron = r#"
|
|
|
|
("test", [
|
2019-08-16 22:55:09 +00:00
|
|
|
("f1", "String", Some("abc")),
|
|
|
|
("f2", "String", Some("bcd")),
|
2018-07-16 19:46:10 +00:00
|
|
|
("f3", "i32", 123),
|
|
|
|
("fraction", "Fraction", (1, 2)),
|
2019-08-19 13:32:40 +00:00
|
|
|
("date", "Date", Some(YMD(2019, 8, 19))),
|
|
|
|
("date_time", "DateTime", Some(YMDhmsTz(2019, 8, 19, 13, 34, 42, 2))),
|
2018-07-16 19:46:10 +00:00
|
|
|
("array", "Array", [
|
|
|
|
("i32", 1),
|
|
|
|
("i32", 2),
|
|
|
|
]),
|
|
|
|
])"#;
|
|
|
|
let s: Structure = ron::de::from_str(s_ron).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
s.as_ref(),
|
|
|
|
Structure::new(
|
|
|
|
"test",
|
|
|
|
&[
|
|
|
|
("f1", &"abc"),
|
|
|
|
("f2", &"bcd"),
|
|
|
|
("f3", &123),
|
2019-08-19 13:32:40 +00:00
|
|
|
("date", &Date::new_dmy(19, DateMonth::August, 2019)),
|
|
|
|
(
|
|
|
|
"date_time",
|
2020-03-22 10:23:16 +00:00
|
|
|
&DateTime::new(2f32, 2019, 8, 19, 13, 34, 42f64).unwrap()
|
2019-08-19 13:32:40 +00:00
|
|
|
),
|
2018-07-16 19:46:10 +00:00
|
|
|
("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-07-29 16:18:24 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_serde_roundtrip() {
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::init().unwrap();
|
2018-07-29 16:18:24 +00:00
|
|
|
|
|
|
|
let s = Structure::builder("test")
|
|
|
|
.field("f1", &"abc")
|
2019-08-16 22:55:09 +00:00
|
|
|
.field("f2", &"bcd".to_owned())
|
2018-07-29 16:18:24 +00:00
|
|
|
.field("f3", &123i32)
|
|
|
|
.field("fraction", &Fraction::new(1, 2))
|
2019-08-19 13:32:40 +00:00
|
|
|
.field("date", &Date::new_dmy(19, DateMonth::August, 2019))
|
|
|
|
.field(
|
|
|
|
"date_time",
|
2020-03-22 10:23:16 +00:00
|
|
|
&DateTime::new(2f32, 2019, 8, 19, 13, 34, 42f64).unwrap(),
|
2019-08-19 13:32:40 +00:00
|
|
|
)
|
2018-07-29 16:18:24 +00:00
|
|
|
.field("array", &Array::new(&[&1, &2]))
|
|
|
|
.build();
|
|
|
|
let s_ser = ron::ser::to_string(&s).unwrap();
|
|
|
|
let s_de: Structure = ron::de::from_str(s_ser.as_str()).unwrap();
|
|
|
|
assert_eq!(s_de.as_ref(), s.as_ref());
|
|
|
|
}
|
2018-07-16 19:46:10 +00:00
|
|
|
}
|