Skip to content

Commit

Permalink
xml: Rename XmlDeError to XmlError
Browse files Browse the repository at this point in the history
  • Loading branch information
lennart-k committed Jan 15, 2025
1 parent 95f7912 commit d74f0ba
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 53 deletions.
4 changes: 2 additions & 2 deletions crates/caldav/src/calendar/methods/report/sync_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ pub(crate) enum SyncLevel {
}

impl ValueDeserialize for SyncLevel {
fn deserialize(val: &str) -> Result<Self, rustical_xml::XmlDeError> {
fn deserialize(val: &str) -> Result<Self, rustical_xml::XmlError> {
Ok(match val {
"1" => Self::One,
"Infinity" => Self::Infinity,
_ => {
return Err(rustical_xml::XmlDeError::Other(
return Err(rustical_xml::XmlError::Other(
"Invalid sync-level".to_owned(),
))
}
Expand Down
2 changes: 1 addition & 1 deletion crates/caldav/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub enum Error {
DavError(#[from] rustical_dav::Error),

#[error(transparent)]
XmlDecodeError(#[from] rustical_xml::XmlDeError),
XmlDecodeError(#[from] rustical_xml::XmlError),

#[error(transparent)]
Other(#[from] anyhow::Error),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ pub(crate) enum SyncLevel {
}

impl ValueDeserialize for SyncLevel {
fn deserialize(val: &str) -> Result<Self, rustical_xml::XmlDeError> {
fn deserialize(val: &str) -> Result<Self, rustical_xml::XmlError> {
Ok(match val {
"1" => Self::One,
"Infinity" => Self::Infinity,
_ => {
return Err(rustical_xml::XmlDeError::Other(
return Err(rustical_xml::XmlError::Other(
"Invalid sync-level".to_owned(),
))
}
Expand Down
2 changes: 1 addition & 1 deletion crates/carddav/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub enum Error {
DavError(#[from] rustical_dav::Error),

#[error(transparent)]
XmlDecodeError(#[from] rustical_xml::XmlDeError),
XmlDecodeError(#[from] rustical_xml::XmlError),

#[error(transparent)]
Other(#[from] anyhow::Error),
Expand Down
2 changes: 1 addition & 1 deletion crates/dav/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub enum Error {
PropReadOnly,

#[error(transparent)]
XmlDeserializationError(#[from] rustical_xml::XmlDeError),
XmlDeserializationError(#[from] rustical_xml::XmlError),

#[error(transparent)]
IOError(#[from] std::io::Error),
Expand Down
1 change: 0 additions & 1 deletion crates/dav/src/push/push_notifier.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::xml::multistatus::PropstatElement;
use actix_web::http::StatusCode;
use reqwest::Url;
use rustical_store::{CollectionOperation, CollectionOperationType, SubscriptionStore};
use rustical_xml::{XmlRootTag, XmlSerialize, XmlSerializeRoot};
use std::sync::Arc;
Expand Down
4 changes: 2 additions & 2 deletions crates/store/src/calendar/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ pub const LOCAL_DATE: &str = "%Y%m%d";
pub struct UtcDateTime(DateTime<Utc>);

impl ValueDeserialize for UtcDateTime {
fn deserialize(val: &str) -> Result<Self, rustical_xml::XmlDeError> {
fn deserialize(val: &str) -> Result<Self, rustical_xml::XmlError> {
let input = <String as ValueDeserialize>::deserialize(val)?;
Ok(Self(
NaiveDateTime::parse_from_str(&input, UTC_DATE_TIME)
.map_err(|_| {
rustical_xml::XmlDeError::Other("Could not parse as UTC timestamp".to_owned())
rustical_xml::XmlError::Other("Could not parse as UTC timestamp".to_owned())
})?
.and_utc(),
))
Expand Down
2 changes: 1 addition & 1 deletion crates/xml/derive/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl Field {
(false, false, true) => quote! { builder.#builder_field_ident },
(false, false, false) => {
let field_ident = self.field_ident().into_token_stream().to_string();
quote! { builder.#builder_field_ident.ok_or(::rustical_xml::XmlDeError::MissingField(#field_ident))? }
quote! { builder.#builder_field_ident.ok_or(::rustical_xml::XmlError::MissingField(#field_ident))? }
}
};
quote! { #target_field_index: #builder_value }
Expand Down
24 changes: 12 additions & 12 deletions crates/xml/derive/src/xml_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ impl Enum {
reader: &mut quick_xml::NsReader<R>,
start: &quick_xml::events::BytesStart,
empty: bool
) -> Result<Self, rustical_xml::XmlDeError> {
) -> Result<Self, rustical_xml::XmlError> {
#(#variant_branches);*

Err(rustical_xml::XmlDeError::InvalidVariant("could not match".to_owned()))
Err(rustical_xml::XmlError::InvalidVariant("could not match".to_owned()))
}
}
}
Expand All @@ -49,14 +49,14 @@ impl Enum {
reader: &mut quick_xml::NsReader<R>,
start: &quick_xml::events::BytesStart,
empty: bool
) -> Result<Self, rustical_xml::XmlDeError> {
) -> Result<Self, rustical_xml::XmlError> {
let (_ns, name) = reader.resolve_element(start.name());

match name.as_ref() {
#(#variant_branches),*
name => {
// Handle invalid variant name
Err(rustical_xml::XmlDeError::InvalidVariant(String::from_utf8_lossy(name).to_string()))
Err(rustical_xml::XmlError::InvalidVariant(String::from_utf8_lossy(name).to_string()))
}
}
}
Expand Down Expand Up @@ -150,7 +150,7 @@ impl Enum {

quote! {
impl #impl_generics ::rustical_xml::XmlDocument for #ident #type_generics #where_clause {
fn parse<R: ::std::io::BufRead>(mut reader: ::quick_xml::NsReader<R>) -> Result<Self, ::rustical_xml::XmlDeError>
fn parse<R: ::std::io::BufRead>(mut reader: ::quick_xml::NsReader<R>) -> Result<Self, ::rustical_xml::XmlError>
where
Self: ::rustical_xml::XmlDeserialize
{
Expand All @@ -166,26 +166,26 @@ impl Enum {
return <Self as ::rustical_xml::XmlDeserialize>::deserialize(&mut reader, &start, empty);
}

Event::Eof => return Err(::rustical_xml::XmlDeError::Eof),
Event::Eof => return Err(::rustical_xml::XmlError::Eof),
Event::Text(bytes_text) => {
return Err(::rustical_xml::XmlDeError::UnsupportedEvent("Text"));
return Err(::rustical_xml::XmlError::UnsupportedEvent("Text"));
}
Event::CData(cdata) => {
return Err(::rustical_xml::XmlDeError::UnsupportedEvent("CDATA"));
return Err(::rustical_xml::XmlError::UnsupportedEvent("CDATA"));
}
Event::Comment(_) => { /* ignore */ }
Event::Decl(_) => {
/* ignore */
// return Err(::rustical_xml::XmlDeError::UnsupportedEvent("Declaration"));
// return Err(::rustical_xml::XmlError::UnsupportedEvent("Declaration"));
}
Event::PI(_) => {
return Err(::rustical_xml::XmlDeError::UnsupportedEvent("Processing instruction"));
return Err(::rustical_xml::XmlError::UnsupportedEvent("Processing instruction"));
}
Event::DocType(doctype) => {
return Err(::rustical_xml::XmlDeError::UnsupportedEvent("Doctype in the middle of the document"));
return Err(::rustical_xml::XmlError::UnsupportedEvent("Doctype in the middle of the document"));
}
Event::End(end) => {
return Err(::rustical_xml::XmlDeError::UnsupportedEvent("Premature end"));
return Err(::rustical_xml::XmlError::UnsupportedEvent("Premature end"));
}
};
}
Expand Down
18 changes: 9 additions & 9 deletions crates/xml/derive/src/xml_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn invalid_field_branch(ident: &syn::Ident, allow: bool) -> proc_macro2::TokenSt
quote! {}
} else {
quote! {
return Err(XmlDeError::InvalidFieldName(#ident, format!("[{ns:?}]{tag}", tag = String::from_utf8_lossy(tag)))) }
return Err(XmlError::InvalidFieldName(#ident, format!("[{ns:?}]{tag}", tag = String::from_utf8_lossy(tag)))) }
}
}

Expand Down Expand Up @@ -130,9 +130,9 @@ impl NamedStruct {
reader: &mut quick_xml::NsReader<R>,
start: &quick_xml::events::BytesStart,
empty: bool
) -> Result<Self, rustical_xml::XmlDeError> {
) -> Result<Self, rustical_xml::XmlError> {
use quick_xml::events::Event;
use rustical_xml::XmlDeError;
use rustical_xml::XmlError;

let mut buf = Vec::new();

Expand Down Expand Up @@ -163,7 +163,7 @@ impl NamedStruct {
Event::End(e) if e.name() == start.name() => {
break;
}
Event::Eof => return Err(XmlDeError::Eof),
Event::Eof => return Err(XmlError::Eof),
// start of a child element
Event::Start(start) | Event::Empty(start) => {
let empty = matches!(event, Event::Empty(_));
Expand All @@ -179,24 +179,24 @@ impl NamedStruct {
#(#text_field_branches)*
}
Event::CData(cdata) => {
return Err(XmlDeError::UnsupportedEvent("CDATA"));
return Err(XmlError::UnsupportedEvent("CDATA"));
}
Event::Comment(_) => { /* ignore */ }
Event::Decl(_) => {
// Error: not supported
return Err(XmlDeError::UnsupportedEvent("Declaration"));
return Err(XmlError::UnsupportedEvent("Declaration"));
}
Event::PI(_) => {
// Error: not supported
return Err(XmlDeError::UnsupportedEvent("Processing instruction"));
return Err(XmlError::UnsupportedEvent("Processing instruction"));
}
Event::DocType(doctype) => {
// Error: start of new document
return Err(XmlDeError::UnsupportedEvent("Doctype in the middle of the document"));
return Err(XmlError::UnsupportedEvent("Doctype in the middle of the document"));
}
Event::End(end) => {
// Error: premature end
return Err(XmlDeError::Other("Unexpected closing tag for wrong element".to_owned()));
return Err(XmlError::Other("Unexpected closing tag for wrong element".to_owned()));
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions crates/xml/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ use std::io::BufRead;
pub use xml_derive::XmlDeserialize;
pub use xml_derive::XmlDocument;

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

pub trait XmlDeserialize: Sized {
fn deserialize<R: BufRead>(
reader: &mut quick_xml::NsReader<R>,
start: &BytesStart,
empty: bool,
) -> Result<Self, XmlDeError>;
) -> Result<Self, XmlError>;
}

pub trait XmlDocument: XmlDeserialize {
fn parse<R: BufRead>(reader: quick_xml::NsReader<R>) -> Result<Self, XmlDeError>;
fn parse<R: BufRead>(reader: quick_xml::NsReader<R>) -> Result<Self, XmlError>;

#[inline]
fn parse_reader<R: BufRead>(input: R) -> Result<Self, XmlDeError>
fn parse_reader<R: BufRead>(input: R) -> Result<Self, XmlError>
where
Self: XmlDeserialize,
{
Expand All @@ -29,13 +29,13 @@ pub trait XmlDocument: XmlDeserialize {
}

#[inline]
fn parse_str(s: &str) -> Result<Self, XmlDeError> {
fn parse_str(s: &str) -> Result<Self, XmlError> {
Self::parse_reader(s.as_bytes())
}
}

impl<T: XmlRootTag + XmlDeserialize> XmlDocument for T {
fn parse<R: BufRead>(mut reader: quick_xml::NsReader<R>) -> Result<Self, XmlDeError>
fn parse<R: BufRead>(mut reader: quick_xml::NsReader<R>) -> Result<Self, XmlError>
where
Self: XmlDeserialize,
{
Expand All @@ -57,7 +57,7 @@ impl<T: XmlRootTag + XmlDeserialize> XmlDocument for T {
};
if !matches {
let root_ns = Self::root_ns();
return Err(XmlDeError::InvalidTag(
return Err(XmlError::InvalidTag(
format!("{ns:?}"),
String::from_utf8_lossy(name.as_ref()).to_string(),
format!("{root_ns:?}"),
Expand All @@ -67,7 +67,7 @@ impl<T: XmlRootTag + XmlDeserialize> XmlDocument for T {

return Self::deserialize(&mut reader, &start, empty);
}
_ => return Err(XmlDeError::UnsupportedEvent("unknown, todo")),
_ => return Err(XmlError::UnsupportedEvent("unknown, todo")),
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/xml/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use thiserror::Error;

#[derive(Debug, Error)]
pub enum XmlDeError {
pub enum XmlError {
#[error(transparent)]
QuickXmlError(#[from] quick_xml::Error),
#[error(transparent)]
Expand Down
8 changes: 4 additions & 4 deletions crates/xml/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod value;

pub use de::XmlDeserialize;
pub use de::XmlDocument;
pub use error::XmlDeError;
pub use error::XmlError;
pub use se::XmlSerialize;
pub use se::XmlSerializeRoot;
pub use value::{ParseValueError, ValueDeserialize, ValueSerialize};
Expand All @@ -21,15 +21,15 @@ impl XmlDeserialize for () {
reader: &mut quick_xml::NsReader<R>,
start: &BytesStart,
empty: bool,
) -> Result<Self, XmlDeError> {
) -> Result<Self, XmlError> {
if empty {
return Ok(());
}
let mut buf = Vec::new();
loop {
match reader.read_event_into(&mut buf)? {
Event::End(e) if e.name() == start.name() => return Ok(()),
Event::Eof => return Err(XmlDeError::Eof),
Event::Eof => return Err(XmlError::Eof),
_ => {}
};
}
Expand Down Expand Up @@ -91,7 +91,7 @@ impl XmlDeserialize for Unparsed {
reader: &mut quick_xml::NsReader<R>,
start: &BytesStart,
empty: bool,
) -> Result<Self, XmlDeError> {
) -> Result<Self, XmlError> {
// let reader_cloned = NsReader::from_reader(reader.get_ref().to_owned());
if !empty {
let mut buf = vec![];
Expand Down
16 changes: 8 additions & 8 deletions crates/xml/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::num::{ParseFloatError, ParseIntError};
use std::{convert::Infallible, io::BufRead};
use thiserror::Error;

use crate::{XmlDeError, XmlDeserialize, XmlSerialize};
use crate::{XmlError, XmlDeserialize, XmlSerialize};

#[derive(Debug, Error)]
pub enum ParseValueError {
Expand All @@ -22,7 +22,7 @@ pub trait ValueSerialize: Sized {
}

pub trait ValueDeserialize: Sized {
fn deserialize(val: &str) -> Result<Self, XmlDeError>;
fn deserialize(val: &str) -> Result<Self, XmlError>;
}

macro_rules! impl_value_parse {
Expand All @@ -34,10 +34,10 @@ macro_rules! impl_value_parse {
}

impl ValueDeserialize for $t {
fn deserialize(val: &str) -> Result<Self, XmlDeError> {
fn deserialize(val: &str) -> Result<Self, XmlError> {
val.parse()
.map_err(ParseValueError::from)
.map_err(XmlDeError::from)
.map_err(XmlError::from)
}
}
};
Expand Down Expand Up @@ -68,7 +68,7 @@ impl<T: ValueDeserialize> XmlDeserialize for T {
reader: &mut quick_xml::NsReader<R>,
_start: &BytesStart,
empty: bool,
) -> Result<Self, XmlDeError> {
) -> Result<Self, XmlError> {
let mut string = String::new();

if !empty {
Expand All @@ -78,13 +78,13 @@ impl<T: ValueDeserialize> XmlDeserialize for T {
Event::Text(text) => {
if !string.is_empty() {
// Content already written
return Err(XmlDeError::UnsupportedEvent("content already written"));
return Err(XmlError::UnsupportedEvent("content already written"));
}
string = String::from_utf8_lossy(text.as_ref()).to_string();
}
Event::End(_) => break,
Event::Eof => return Err(XmlDeError::Eof),
_ => return Err(XmlDeError::UnsupportedEvent("todo")),
Event::Eof => return Err(XmlError::Eof),
_ => return Err(XmlError::UnsupportedEvent("todo")),
};
}
}
Expand Down

0 comments on commit d74f0ba

Please sign in to comment.