use std::{error::Error, io};
pub type Result<T> = std::result::Result<T, ReadError>;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Limit {
SampleBytes,
Depth,
RecordBytes,
EventBytes,
}
#[derive(Debug, thiserror::Error)]
pub enum ReadError {
#[error("invalid configuration: {0}")]
Configuration(String),
#[error("XML input at byte {byte_offset}: {source}")]
Xml {
byte_offset: u64,
source: Box<dyn Error + Send + Sync>,
},
#[error("{kind:?} limit {limit} exceeded at byte {byte_offset}")]
LimitExceeded {
kind: Limit,
limit: usize,
byte_offset: u64,
},
#[error("record {record_index} at byte {byte_offset}: {source}")]
Conversion {
record_index: u64,
byte_offset: u64,
source: crate::Error,
},
}
impl ReadError {
pub(super) fn configuration(error: crate::Error) -> Self {
Self::Configuration(match error {
crate::Error::Configuration(message) => message,
error => error.to_string(),
})
}
pub(super) fn xml(byte_offset: u64, source: impl Error + Send + Sync + 'static) -> Self {
Self::Xml {
byte_offset,
source: Box::new(source),
}
}
pub(super) fn invalid(byte_offset: u64, message: &str) -> Self {
Self::xml(
byte_offset,
io::Error::new(io::ErrorKind::InvalidData, message.to_owned()),
)
}
pub(super) fn limit(kind: Limit, limit: usize, byte_offset: u64) -> Self {
Self::LimitExceeded {
kind,
limit,
byte_offset,
}
}
}