use std::path::PathBuf;
use std::{fmt, io, result};
pub(crate) type Result<T, E = OscError> = result::Result<T, E>;
#[derive(Debug, thiserror::Error)]
pub enum OscError {
#[error("I/O error: {0}")]
Io(#[from] io::Error),
#[error("{}: {source}", path.display())]
File {
path: PathBuf,
source: io::Error,
},
#[error("{position}: {kind}")]
Xml {
position: XmlPosition,
kind: XmlError,
},
#[error("{origin}: invalid state file: {kind}")]
State {
origin: String,
kind: StateError,
},
#[error("{url}: {source}")]
Fetch {
url: String,
source: io::Error,
},
#[error("{url} is not on the server, though the server's state says it is published")]
NotFound {
url: String,
},
#[error("{url} is not on the server; is the replication URL right?")]
NoStream {
url: String,
},
#[error("sequence {sequence} is past the source's latest state, {latest}")]
NotPublished {
sequence: u64,
latest: u64,
},
#[error("the thread reading diffs ahead stopped unexpectedly")]
ReadAhead,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct XmlPosition {
pub file: Option<PathBuf>,
pub offset: u64,
pub element: Option<String>,
}
impl fmt::Display for XmlPosition {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(file) = &self.file {
write!(f, "{}: ", file.display())?;
}
write!(f, "byte {}", self.offset)?;
if let Some(element) = &self.element {
write!(f, ", <{element}>")?;
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum XmlError {
#[error("the file ends before the end of the osmChange element")]
Truncated,
#[error("malformed XML: {0}")]
Syntax(&'static str),
#[error("DTDs, entity declarations and CDATA sections are not allowed")]
Declaration,
#[error("text is not allowed between elements")]
Text,
#[error("the element is not allowed {0}")]
Unexpected(&'static str),
#[error("the end tag does not match the open element <{0}>")]
Mismatched(&'static str),
#[error("the required attribute {0:?} is missing")]
MissingAttribute(&'static str),
#[error("the attribute {0:?} is repeated")]
DuplicateAttribute(&'static str),
#[error("invalid {attribute} {value:?}")]
Value {
attribute: &'static str,
value: String,
},
#[error("an attribute value has an invalid character or reference")]
Escape,
#[error("an attribute value is not valid UTF-8")]
Utf8,
#[error("a node outside a delete section has no location")]
NoLocation,
#[error("a node has only one of lat and lon")]
HalfLocation,
#[error(
"an element is larger than 16 MiB, or an object holds over a million items or 64 MiB of text"
)]
TooLarge,
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum StateError {
#[error("{0} is missing")]
Missing(&'static str),
#[error("invalid {key} {value:?}")]
Value {
key: &'static str,
value: String,
},
}