#[rustfmt::skip] use crate::text_delta::{TextAttribute, TextAttributeKey, TextAttributes, TextAttributeValue}; use serde::{ de, de::{MapAccess, Visitor}, ser::SerializeMap, Deserialize, Deserializer, Serialize, Serializer, }; use std::fmt; impl Serialize for TextAttribute { fn serialize(&self, serializer: S) -> Result<::Ok, ::Error> where S: Serializer, { let mut map = serializer.serialize_map(Some(1))?; let _ = serial_attribute(&mut map, &self.key, &self.value)?; map.end() } } impl Serialize for TextAttributes { fn serialize(&self, serializer: S) -> Result where S: Serializer, { if self.is_empty() { return serializer.serialize_none(); } let mut map = serializer.serialize_map(Some(self.inner.len()))?; for (k, v) in &self.inner { let _ = serial_attribute(&mut map, k, v)?; } map.end() } } fn serial_attribute(map_serializer: &mut S, key: &TextAttributeKey, value: &TextAttributeValue) -> Result<(), E> where S: SerializeMap, E: From<::Error>, { if let Some(v) = &value.0 { match key { TextAttributeKey::Bold | TextAttributeKey::Italic | TextAttributeKey::Underline | TextAttributeKey::StrikeThrough | TextAttributeKey::CodeBlock | TextAttributeKey::InlineCode | TextAttributeKey::BlockQuote => match &v.parse::() { Ok(value) => map_serializer.serialize_entry(&key, value)?, Err(e) => log::error!("Serial {:?} failed. {:?}", &key, e), }, TextAttributeKey::Font | TextAttributeKey::Size | TextAttributeKey::Header | TextAttributeKey::Indent | TextAttributeKey::Width | TextAttributeKey::Height => match &v.parse::() { Ok(value) => map_serializer.serialize_entry(&key, value)?, Err(e) => log::error!("Serial {:?} failed. {:?}", &key, e), }, TextAttributeKey::Link | TextAttributeKey::Color | TextAttributeKey::Background | TextAttributeKey::Align | TextAttributeKey::List => { map_serializer.serialize_entry(&key, v)?; } } } else { map_serializer.serialize_entry(&key, "")?; } Ok(()) } impl<'de> Deserialize<'de> for TextAttributes { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { struct AttributesVisitor; impl<'de> Visitor<'de> for AttributesVisitor { type Value = TextAttributes; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("Expect map") } fn visit_map(self, mut map: A) -> Result where A: MapAccess<'de>, { let mut attributes = TextAttributes::new(); while let Some(key) = map.next_key::()? { let value = map.next_value::()?; attributes.insert(key, value); } Ok(attributes) } } deserializer.deserialize_map(AttributesVisitor {}) } } impl Serialize for TextAttributeValue { fn serialize(&self, serializer: S) -> Result where S: Serializer, { match &self.0 { None => serializer.serialize_none(), Some(val) => serializer.serialize_str(val), } } } impl<'de> Deserialize<'de> for TextAttributeValue { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { struct AttributeValueVisitor; impl<'de> Visitor<'de> for AttributeValueVisitor { type Value = TextAttributeValue; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("bool, usize or string") } fn visit_bool(self, value: bool) -> Result where E: de::Error, { Ok(value.into()) } fn visit_i8(self, value: i8) -> Result where E: de::Error, { Ok(TextAttributeValue(Some(format!("{}", value)))) } fn visit_i16(self, value: i16) -> Result where E: de::Error, { Ok(TextAttributeValue(Some(format!("{}", value)))) } fn visit_i32(self, value: i32) -> Result where E: de::Error, { Ok(TextAttributeValue(Some(format!("{}", value)))) } fn visit_i64(self, value: i64) -> Result where E: de::Error, { Ok(TextAttributeValue(Some(format!("{}", value)))) } fn visit_u8(self, value: u8) -> Result where E: de::Error, { Ok(TextAttributeValue(Some(format!("{}", value)))) } fn visit_u16(self, value: u16) -> Result where E: de::Error, { Ok(TextAttributeValue(Some(format!("{}", value)))) } fn visit_u32(self, value: u32) -> Result where E: de::Error, { Ok(TextAttributeValue(Some(format!("{}", value)))) } fn visit_u64(self, value: u64) -> Result where E: de::Error, { Ok(TextAttributeValue(Some(format!("{}", value)))) } fn visit_str(self, s: &str) -> Result where E: de::Error, { Ok(s.into()) } fn visit_none(self) -> Result where E: de::Error, { Ok(TextAttributeValue(None)) } fn visit_unit(self) -> Result where E: de::Error, { // the value that contains null will be processed here. Ok(TextAttributeValue(None)) } fn visit_map(self, map: A) -> Result where A: MapAccess<'de>, { // https://github.com/serde-rs/json/issues/505 let mut map = map; let value = map.next_value::()?; Ok(value) } } deserializer.deserialize_any(AttributeValueVisitor) } }