#[cfg(not(feature = "extra-platforms"))]
use alloc::sync::Arc;
#[cfg(feature = "extra-platforms")]
use portable_atomic_util::Arc;
use crate::context;
use crate::error::EndOrError;
pub type Result<T> = core::result::Result<T, EndOrError>;
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
pub enum CommentMode {
#[default]
Reject,
Discard,
}
#[derive(Debug)]
pub struct Options {
pub max_token_length: usize,
pub context: Option<Arc<context::Context>>,
pub comments: CommentMode,
}
impl Clone for Options {
fn clone(&self) -> Self {
Self {
max_token_length: self.max_token_length,
context: self.context.as_ref().map(Arc::clone),
comments: self.comments,
}
}
}
impl From<Options> for crate::lexer::LexerOptions {
fn from(other: Options) -> Self {
Self {
max_token_length: other.max_token_length,
}
}
}
impl From<&Options> for crate::lexer::LexerOptions {
fn from(other: &Options) -> Self {
Self {
max_token_length: other.max_token_length,
}
}
}
impl From<Options> for super::raw::RawOptions {
fn from(other: Options) -> Self {
Self {
max_token_length: other.max_token_length,
comments: other.comments,
}
}
}
impl From<&Options> for super::raw::RawOptions {
fn from(other: &Options) -> Self {
Self {
max_token_length: other.max_token_length,
comments: other.comments,
}
}
}
impl Default for Options {
fn default() -> Self {
Self {
max_token_length: 8192,
context: None,
comments: CommentMode::default(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum XmlVersion {
V1_0,
}
pub const XMLNS_XML: &str = "http://www.w3.org/XML/1998/namespace";
pub const XMLNS_XMLNS: &str = "http://www.w3.org/2000/xmlns/";
pub(crate) const XMLNS_UNNAMESPACED: &str = "";
#[derive(Copy, Debug, Clone, PartialEq, Eq)]
pub struct EventMetrics {
pub(super) len: usize,
}
impl EventMetrics {
pub fn is_empty(&self) -> bool {
self.len == 0
}
pub fn len(&self) -> usize {
self.len
}
pub const fn new(len: usize) -> EventMetrics {
EventMetrics { len }
}
pub const fn zero() -> Self {
EventMetrics::new(0)
}
}
pub trait Parse {
type Output;
fn parse(&mut self, buf: &mut &[u8], at_eof: bool) -> Result<Option<Self::Output>>;
fn release_temporaries(&mut self);
fn parse_all<F: FnMut(Self::Output)>(
&mut self,
data: &mut &[u8],
at_eof: bool,
mut f: F,
) -> Result<()> {
loop {
match self.parse(data, at_eof)? {
None => return Ok(()),
Some(ev) => f(ev),
}
}
}
fn parse_buf<T: bytes::Buf>(
&mut self,
buf: &mut T,
at_eof: bool,
) -> Result<Option<Self::Output>> {
loop {
let mut chunk = buf.chunk();
let init_len = chunk.len();
let at_eof = at_eof && init_len == buf.remaining();
let result = self.parse(&mut chunk, at_eof);
let new_len = chunk.len();
let consumed = init_len - new_len;
buf.advance(consumed);
match result {
Err(EndOrError::NeedMoreData) => {
if buf.remaining() > 0 {
assert!(consumed > 0);
continue;
} else {
return Err(EndOrError::NeedMoreData);
}
}
other => return other,
}
}
}
fn parse_all_buf<T: bytes::Buf, F: FnMut(Self::Output)>(
&mut self,
buf: &mut T,
at_eof: bool,
mut f: F,
) -> Result<()> {
loop {
match self.parse_buf(buf, at_eof)? {
None => return Ok(()),
Some(ev) => f(ev),
}
}
}
}
pub trait WithOptions {
fn with_options(options: Options) -> Self;
}