aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPhilipp A2018-11-16 18:15:49 +0100
committerPhilipp A2018-11-16 18:15:49 +0100
commit0fa6ad2e25df27e281b04d51d2c64b5658dc8876 (patch)
treea8c238e3e4eaef3356b9479bb3e8eb76eb3a1a5f
parent8f8270f4b4745087dcddba60b23d88d6f3fa4fb6 (diff)
downloadrust-rst-0fa6ad2e25df27e281b04d51d2c64b5658dc8876.tar.bz2
WIP serialization
-rw-r--r--Cargo.toml2
-rw-r--r--src/document_tree/elements.rs42
2 files changed, 43 insertions, 1 deletions
diff --git a/Cargo.toml b/Cargo.toml
index c16f653..0c0e538 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -29,7 +29,7 @@ unicode_categories = '0.1.1'
pest = { git = 'https://github.com/flying-sheep/pest', branch = 'the-future' }
pest_derive = { git = 'https://github.com/flying-sheep/pest', branch = 'the-future' }
serde = '1.0.80'
-serde_json = "1.0.33"
+serde_json = '1.0.33'
quicli = '0.3.1'
structopt = '0.2.13'
diff --git a/src/document_tree/elements.rs b/src/document_tree/elements.rs
index ba5d78e..77efa34 100644
--- a/src/document_tree/elements.rs
+++ b/src/document_tree/elements.rs
@@ -1,4 +1,12 @@
use url::Url;
+use serde::{
+ Serialize,
+ Serializer,
+ ser::{
+ SerializeSeq,
+ SerializeStruct,
+ },
+};
use super::extra_attributes::{self,ExtraAttributes};
use super::element_categories::*;
@@ -72,26 +80,60 @@ macro_rules! impl_new {(
}
)}
+macro_rules! impl_serialize {
+ ($name: ident, $extra: ident, $childtype: ident) => {
+ impl Serialize for $name {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
+ let mut state = serializer.serialize_struct(stringify!($name), 3)?;
+ // TODO: common attrs
+ impl_serialize_extra!($extra);
+ impl_serialize_children!($childtype, self, serializer);
+ state.end()
+ }
+ }
+ };
+}
+
+macro_rules! impl_serialize_extra {
+ (__) => {};
+ ($name: ident) => {
+ // TODO extra_attributes::$name
+ };
+}
+
+macro_rules! impl_serialize_children {
+ (__, $_: ident, $__: ident) => {};
+ ($childtype: ident, $self: ident, $serializer: ident) => {
+ use serde::ser::SerializeStruct;
+ $serializer.serialize_field("children", $self.children());
+ };
+}
+
macro_rules! impl_elem {
($name:ident) => {
impl_new!(#[derive(Default,Debug)] pub struct $name { common: CommonAttributes });
impl_element!($name);
+ impl_serialize!($name, __, __);
};
($name:ident; +) => {
impl_new!(#[derive(Default,Debug)] pub struct $name { common: CommonAttributes, extra: extra_attributes::$name });
impl_element!($name); impl_extra!($name);
+ impl_serialize!($name, $name, __);
};
($name:ident; *) => { //same as above with no default
impl_new!(#[derive(Debug)] pub struct $name { common: CommonAttributes, extra: extra_attributes::$name });
impl_element!($name); impl_extra!($name);
+ impl_serialize!($name, $name, __);
};
($name:ident, $childtype:ident) => {
impl_new!(#[derive(Default,Debug)] pub struct $name { common: CommonAttributes, children: Vec<$childtype> });
impl_element!($name); impl_children!($name, $childtype);
+ impl_serialize!($name, __, $childtype);
};
($name:ident, $childtype:ident; +) => {
impl_new!(#[derive(Default,Debug)] pub struct $name { common: CommonAttributes, extra: extra_attributes::$name, children: Vec<$childtype> });
impl_element!($name); impl_extra!($name); impl_children!($name, $childtype);
+ impl_serialize!($name, $name, $childtype);
};
}