Skip to content

Commit

Permalink
minor improvements to rustical_xml errors
Browse files Browse the repository at this point in the history
  • Loading branch information
lennart-k committed Jan 4, 2025
1 parent fd4ed57 commit a304714
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 17 deletions.
7 changes: 5 additions & 2 deletions crates/caldav/src/calendar/methods/report/sync_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ impl Value for SyncLevel {
Ok(match val {
"1" => Self::One,
"Infinity" => Self::Infinity,
// TODO: proper error
_ => return Err(rustical_xml::XmlDeError::UnknownError),
_ => {
return Err(rustical_xml::XmlDeError::Other(
"Invalid sync-level".to_owned(),
))
}
})
}
fn serialize(&self) -> String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ impl Value for SyncLevel {
Ok(match val {
"1" => Self::One,
"Infinity" => Self::Infinity,
// TODO: proper error
_ => return Err(rustical_xml::XmlDeError::UnknownError),
_ => {
return Err(rustical_xml::XmlDeError::Other(
"Invalid sync-level".to_owned(),
))
}
})
}
fn serialize(&self) -> String {
Expand Down
5 changes: 3 additions & 2 deletions crates/store/src/calendar/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ impl Value for UtcDateTime {
let input = <String as Value>::deserialize(val)?;
Ok(Self(
NaiveDateTime::parse_from_str(&input, UTC_DATE_TIME)
// TODO: proper error
.map_err(|_| rustical_xml::XmlDeError::UnknownError)?
.map_err(|_| {
rustical_xml::XmlDeError::Other("Could not parse as UTC timestamp".to_owned())
})?
.and_utc(),
))
}
Expand Down
26 changes: 22 additions & 4 deletions crates/xml/derive/src/xml_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl Enum {
) -> Result<Self, rustical_xml::XmlDeError> {
#(#variant_branches);*

Err(rustical_xml::XmlDeError::UnknownError)
Err(rustical_xml::XmlDeError::InvalidVariant("could not match".to_owned()))
}
}
}
Expand Down Expand Up @@ -162,12 +162,30 @@ impl Enum {
let empty = matches!(event, Event::Empty(_));

match event {
Event::Decl(_) => { /* <?xml ... ?> ignore this */ }
Event::Comment(_) => { /* ignore this */ }
Event::Start(start) | Event::Empty(start) => {
return <Self as ::rustical_xml::XmlDeserialize>::deserialize(&mut reader, &start, empty);
}
_ => return Err(::rustical_xml::XmlDeError::UnknownError),

Event::Eof => return Err(::rustical_xml::XmlDeError::Eof),
Event::Text(bytes_text) => {
return Err(::rustical_xml::XmlDeError::UnsupportedEvent("Text"));
}
Event::CData(cdata) => {
return Err(::rustical_xml::XmlDeError::UnsupportedEvent("CDATA"));
}
Event::Comment(_) => { /* ignore */ }
Event::Decl(_) => {
return Err(::rustical_xml::XmlDeError::UnsupportedEvent("Declaration"));
}
Event::PI(_) => {
return Err(::rustical_xml::XmlDeError::UnsupportedEvent("Processing instruction"));
}
Event::DocType(doctype) => {
return Err(::rustical_xml::XmlDeError::UnsupportedEvent("Doctype in the middle of the document"));
}
Event::End(end) => {
return Err(::rustical_xml::XmlDeError::UnsupportedEvent("Premature end"));
}
};
}
}
Expand Down
6 changes: 2 additions & 4 deletions crates/xml/src/de.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use quick_xml::name::Namespace;
use quick_xml::events::{BytesStart, Event};
use quick_xml::name::ResolveResult;
use std::io::BufRead;
pub use xml_derive::XmlDeserialize;
pub use xml_derive::XmlDocument;

use quick_xml::events::{BytesStart, Event};

use crate::XmlDeError;
use crate::XmlRootTag;

Expand Down Expand Up @@ -69,7 +67,7 @@ impl<T: XmlRootTag + XmlDeserialize> XmlDocument for T {

return Self::deserialize(&mut reader, &start, empty);
}
_ => return Err(XmlDeError::UnknownError),
_ => return Err(XmlDeError::UnsupportedEvent("unknown, todo")),
};
}
}
Expand Down
2 changes: 0 additions & 2 deletions crates/xml/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ pub enum XmlDeError {
QuickXmlError(#[from] quick_xml::Error),
#[error(transparent)]
QuickXmlAttrError(#[from] quick_xml::events::attributes::AttrError),
#[error("Unknown error")]
UnknownError,
#[error("Invalid tag [{0}]{1}. Expected [{2}]{3}")]
InvalidTag(String, String, String, String),
#[error("Missing field {0}")]
Expand Down
2 changes: 1 addition & 1 deletion crates/xml/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl Value for &str {
self.to_string()
}

fn deserialize(val: &str) -> Result<Self, XmlDeError> {
fn deserialize(_val: &str) -> Result<Self, XmlDeError> {
Err(XmlDeError::Other("TODO: Handle this error".to_owned()))
}
}
Expand Down

0 comments on commit a304714

Please sign in to comment.