diff --git a/crates/caldav/src/calendar/methods/report/sync_collection.rs b/crates/caldav/src/calendar/methods/report/sync_collection.rs index 08c3fbd..5227dda 100644 --- a/crates/caldav/src/calendar/methods/report/sync_collection.rs +++ b/crates/caldav/src/calendar/methods/report/sync_collection.rs @@ -9,7 +9,7 @@ use rustical_store::{ synctoken::{format_synctoken, parse_synctoken}, CalendarStore, }; -use rustical_xml::{Value, XmlDeserialize}; +use rustical_xml::{ValueDeserialize, ValueSerialize, XmlDeserialize}; use crate::{ calendar_object::resource::{CalendarObjectProp, CalendarObjectResource}, @@ -22,7 +22,7 @@ pub(crate) enum SyncLevel { Infinity, } -impl Value for SyncLevel { +impl ValueDeserialize for SyncLevel { fn deserialize(val: &str) -> Result { Ok(match val { "1" => Self::One, @@ -34,6 +34,9 @@ impl Value for SyncLevel { } }) } +} + +impl ValueSerialize for SyncLevel { fn serialize(&self) -> String { match self { SyncLevel::One => "1", diff --git a/crates/caldav/src/calendar/prop.rs b/crates/caldav/src/calendar/prop.rs index f22981a..4a66a85 100644 --- a/crates/caldav/src/calendar/prop.rs +++ b/crates/caldav/src/calendar/prop.rs @@ -1,13 +1,13 @@ use derive_more::derive::From; -use rustical_xml::{XmlDeserialize, XmlSerialize}; +use rustical_xml::XmlSerialize; -#[derive(Debug, Clone, XmlDeserialize, XmlSerialize, PartialEq, From)] +#[derive(Debug, Clone, XmlSerialize, PartialEq, From)] pub struct SupportedCalendarComponent { #[xml(ty = "attr")] pub name: &'static str, } -#[derive(Debug, Clone, XmlDeserialize, XmlSerialize, PartialEq)] +#[derive(Debug, Clone, XmlSerialize, PartialEq)] pub struct SupportedCalendarComponentSet { #[xml(ns = "rustical_dav::namespace::NS_CALDAV", flatten)] pub comp: Vec, diff --git a/crates/caldav/src/principal/mod.rs b/crates/caldav/src/principal/mod.rs index 1e81918..1bbafa7 100644 --- a/crates/caldav/src/principal/mod.rs +++ b/crates/caldav/src/principal/mod.rs @@ -29,7 +29,7 @@ pub enum PrincipalProp { Displayname(String), // Scheduling Extensions to CalDAV (RFC 6638) - #[xml(ns = "rustical_dav::namespace::NS_CALDAV")] + #[xml(ns = "rustical_dav::namespace::NS_CALDAV", skip_deserializing)] CalendarUserType(&'static str), #[xml(ns = "rustical_dav::namespace::NS_CALDAV")] CalendarUserAddressSet(HrefElement), diff --git a/crates/carddav/src/addressbook/methods/report/sync_collection.rs b/crates/carddav/src/addressbook/methods/report/sync_collection.rs index 2bec1a7..d1888ba 100644 --- a/crates/carddav/src/addressbook/methods/report/sync_collection.rs +++ b/crates/carddav/src/addressbook/methods/report/sync_collection.rs @@ -13,7 +13,7 @@ use rustical_store::{ synctoken::{format_synctoken, parse_synctoken}, AddressbookStore, }; -use rustical_xml::{Value, XmlDeserialize}; +use rustical_xml::{ValueDeserialize, ValueSerialize, XmlDeserialize}; #[derive(Clone, Debug, PartialEq)] pub(crate) enum SyncLevel { @@ -21,7 +21,7 @@ pub(crate) enum SyncLevel { Infinity, } -impl Value for SyncLevel { +impl ValueDeserialize for SyncLevel { fn deserialize(val: &str) -> Result { Ok(match val { "1" => Self::One, @@ -33,6 +33,8 @@ impl Value for SyncLevel { } }) } +} +impl ValueSerialize for SyncLevel { fn serialize(&self) -> String { match self { SyncLevel::One => "1", diff --git a/crates/store/src/calendar/timestamp.rs b/crates/store/src/calendar/timestamp.rs index 242970b..c218349 100644 --- a/crates/store/src/calendar/timestamp.rs +++ b/crates/store/src/calendar/timestamp.rs @@ -7,7 +7,7 @@ use ical::{ property::Property, }; use lazy_static::lazy_static; -use rustical_xml::Value; +use rustical_xml::{ValueDeserialize, ValueSerialize}; use std::{collections::HashMap, ops::Add}; lazy_static! { @@ -21,9 +21,9 @@ pub const LOCAL_DATE: &str = "%Y%m%d"; #[derive(Debug, Clone, Deref, PartialEq)] pub struct UtcDateTime(DateTime); -impl Value for UtcDateTime { +impl ValueDeserialize for UtcDateTime { fn deserialize(val: &str) -> Result { - let input = ::deserialize(val)?; + let input = ::deserialize(val)?; Ok(Self( NaiveDateTime::parse_from_str(&input, UTC_DATE_TIME) .map_err(|_| { @@ -32,6 +32,8 @@ impl Value for UtcDateTime { .and_utc(), )) } +} +impl ValueSerialize for UtcDateTime { fn serialize(&self) -> String { format!("{}", self.0.format(UTC_DATE_TIME)) } diff --git a/crates/xml/derive/src/field.rs b/crates/xml/derive/src/field.rs index bf13cbf..bd51294 100644 --- a/crates/xml/derive/src/field.rs +++ b/crates/xml/derive/src/field.rs @@ -232,7 +232,7 @@ impl Field { let builder_field_ident = self.builder_field_ident(); let value = wrap_option_if_no_default( quote! { - rustical_xml::Value::deserialize(text.as_ref())? + ::rustical_xml::ValueDeserialize::deserialize(text.as_ref())? }, self.attrs.default.is_some(), ); @@ -250,8 +250,8 @@ impl Field { let value = wrap_option_if_no_default( quote! { - rustical_xml::Value::deserialize(attr.unescape_value()?.as_ref())? - }, + ::rustical_xml::ValueDeserialize::deserialize(attr.unescape_value()?.as_ref())? + }, self.attrs.default.is_some(), ); @@ -270,7 +270,7 @@ impl Field { let value = wrap_option_if_no_default( quote! { - rustical_xml::Value::deserialize(&String::from_utf8_lossy(name.as_ref()))? + rustical_xml::ValueDeserialize::deserialize(&String::from_utf8_lossy(name.as_ref()))? }, self.attrs.default.is_some(), ); diff --git a/crates/xml/derive/src/xml_struct.rs b/crates/xml/derive/src/xml_struct.rs index 1a10103..6bfafbb 100644 --- a/crates/xml/derive/src/xml_struct.rs +++ b/crates/xml/derive/src/xml_struct.rs @@ -239,7 +239,7 @@ impl NamedStruct { quote! { ::quick_xml::events::attributes::Attribute { key: ::quick_xml::name::QName(#field_name), - value: ::std::borrow::Cow::from(::rustical_xml::Value::serialize(&self.#field_index).into_bytes()) + value: ::std::borrow::Cow::from(::rustical_xml::ValueSerialize::serialize(&self.#field_index).into_bytes()) } } }); diff --git a/crates/xml/src/lib.rs b/crates/xml/src/lib.rs index c710d0f..3df20b0 100644 --- a/crates/xml/src/lib.rs +++ b/crates/xml/src/lib.rs @@ -13,7 +13,7 @@ pub use de::XmlDocument; pub use error::XmlDeError; pub use se::XmlSerialize; pub use se::XmlSerializeRoot; -pub use value::Value; +pub use value::{ParseValueError, ValueDeserialize, ValueSerialize}; pub use xml_derive::XmlRootTag; impl XmlDeserialize for () { diff --git a/crates/xml/src/value.rs b/crates/xml/src/value.rs index 51de4b4..63fa1f6 100644 --- a/crates/xml/src/value.rs +++ b/crates/xml/src/value.rs @@ -17,18 +17,23 @@ pub enum ParseValueError { ParseFloatError(#[from] ParseFloatError), } -pub trait Value: Sized { +pub trait ValueSerialize: Sized { fn serialize(&self) -> String; +} + +pub trait ValueDeserialize: Sized { fn deserialize(val: &str) -> Result; } macro_rules! impl_value_parse { ($t:ty) => { - impl Value for $t { + impl ValueSerialize for $t { fn serialize(&self) -> String { self.to_string() } + } + impl ValueDeserialize for $t { fn deserialize(val: &str) -> Result { val.parse() .map_err(ParseValueError::from) @@ -52,17 +57,13 @@ impl_value_parse!(u64); impl_value_parse!(isize); impl_value_parse!(usize); -impl Value for &str { +impl ValueSerialize for &str { fn serialize(&self) -> String { self.to_string() } - - fn deserialize(_val: &str) -> Result { - Err(XmlDeError::Other("TODO: Handle this error".to_owned())) - } } -impl XmlDeserialize for T { +impl XmlDeserialize for T { fn deserialize( reader: &mut quick_xml::NsReader, _start: &BytesStart, @@ -88,11 +89,11 @@ impl XmlDeserialize for T { } } - Value::deserialize(&string) + ValueDeserialize::deserialize(&string) } } -impl XmlSerialize for T { +impl XmlSerialize for T { fn serialize( &self, ns: Option,