use serde_json::{Map,Value}; use serde::{Serialize, Deserialize}; use std::convert::Into; #[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] pub struct GenericFrontmatter { map: Map, } impl Into for GenericFrontmatter { fn into(self) -> serde_json::Value { self.map.into() } } impl GenericFrontmatter { pub fn from_yaml(text: &str) -> Result { let maybe: Result, serde_yaml::Error> = serde_yaml::from_str(text); maybe.map(|f| f.into()) .map_err(|e| e.into()) } pub fn from_toml(text: &str) -> Result { let maybe: Result, toml::de::Error> = toml::from_str(text); maybe.map(|f| f.into()) .map_err(|e| e.into()) } } impl From> for GenericFrontmatter { fn from(m: Map) -> GenericFrontmatter { GenericFrontmatter { map: m } } } #[derive(Debug)] pub enum Error { Deserialization(DeserializationError), //Specialization(serde_json::Error), Serialization(SerializationError) } impl Into for SerializationError { fn into(self) -> Error { Error::Serialization(self) } } impl Into for DeserializationError { fn into(self) -> Error { Error::Deserialization(self) } } #[derive(Debug)] pub enum DeserializationError { TOML(toml::de::Error), YAML(serde_yaml::Error), JSON(serde_json::Error) } impl From for DeserializationError { fn from(e: serde_yaml::Error) -> Self { DeserializationError::YAML(e) } } impl From for DeserializationError { fn from(e: toml::de::Error) -> Self { DeserializationError::TOML(e) } } impl From for DeserializationError { fn from(e: serde_json::Error) -> Self { DeserializationError::JSON(e) } } #[derive(Debug)] pub enum SerializationError { TOML(toml::ser::Error), YAML(serde_yaml::Error), JSON(serde_json::Error) } impl From for SerializationError { fn from (e: serde_yaml::Error) -> Self { SerializationError::YAML(e) } } impl From for SerializationError { fn from(e: toml::ser::Error) -> Self { SerializationError::TOML(e) } } impl From for SerializationError { fn from(e: serde_json:: Error) -> Self { SerializationError::JSON(e) } } /* For future tests fn main() { let body = "test: lol\ntest2: lol\nextra:\n extra_key: value"; let body2 = "test = \"lol\"\n[extra]\nextra_key = \"value\""; let body3 = "test: lol\nextra:\n extra_key: value\ntest2: lol\ntest3: lol2"; let frontmatter = GenericFrontmatter::from_yaml(&body).expect("FAIL"); let special: PageFrontmatter = serde_json::from_value(frontmatter.into()).expect("FAIL2"); println!("{:?}", special); }*/