mod read;
mod write;
pub use read::{Node, Seq, leaf, parse, parse_date, parse_zeitstempel, unwrap_soap};
pub use write::{WriteError, WriteErrorKind, Writer, escape};
use core::fmt;
use crate::ValueError;
pub const NS_XSI: &str = "http://www.w3.org/2001/XMLSchema-instance";
pub const NS_SOAP: &str = "http://schemas.xmlsoap.org/soap/envelope/";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Limits {
pub max_bytes: usize,
pub max_depth: usize,
pub max_elements: usize,
pub max_text: usize,
pub max_attachment: usize,
}
impl Limits {
#[must_use]
pub const fn for_attachments(max_attachment: usize) -> Self {
let base64 = max_attachment.div_ceil(3).saturating_mul(4);
Self { max_bytes: (1 << 20) + base64, max_attachment, ..Self::DEFAULT }
}
const DEFAULT: Self =
Self { max_bytes: 1 << 20, max_depth: 32, max_elements: 10_000, max_text: 4096, max_attachment: 1 << 20 };
}
impl Default for Limits {
fn default() -> Self {
Self::DEFAULT
}
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub struct XmlError {
pub path: String,
pub offset: Option<u64>,
pub kind: XmlErrorKind,
}
impl XmlError {
#[must_use]
pub fn at(path: &str, kind: XmlErrorKind) -> Self {
Self { path: path.to_owned(), offset: None, kind }
}
}
impl fmt::Display for XmlError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.path.is_empty() {
write!(f, "{}", self.kind)?;
} else {
write!(f, "{}: {}", self.path, self.kind)?;
}
if let Some(offset) = self.offset {
write!(f, " (byte {offset})")?;
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum XmlErrorKind {
#[error("input of {actual} bytes exceeds the limit of {limit}")]
TooLarge {
limit: usize,
actual: usize,
},
#[error("elements nest deeper than {limit}")]
TooDeep {
limit: usize,
},
#[error("more than {limit} elements")]
TooManyElements {
limit: usize,
},
#[error("text longer than {limit} characters")]
TextTooLong {
limit: usize,
},
#[error("embedded document larger than {limit} bytes")]
AttachmentTooLarge {
limit: usize,
},
#[error("{element} occurs more than {max} times")]
TooMany {
element: &'static str,
max: usize,
},
#[error("DOCTYPE declarations are not accepted")]
Doctype,
#[error("unknown entity reference")]
Entity,
#[error("malformed XML: {0}")]
Malformed(String),
#[error("missing element {0}")]
Missing(&'static str),
#[error("unexpected element {0}")]
Unexpected(String),
#[error("mixed content is not allowed")]
MixedContent,
#[error("xsi:type {0}")]
XsiType(String),
#[error(transparent)]
Value(ValueError),
#[error("invalid {0}")]
Invalid(&'static str),
}