use std::fmt;
use std::path::PathBuf;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum XtceError {
#[error("failed to read {path}")]
Io {
path: PathBuf,
#[source]
source: std::io::Error,
},
#[error(transparent)]
Parse(#[from] ParseError),
#[error("expected a <SpaceSystem> root element, found <{found}>")]
NotXtce {
found: String,
},
#[error("missing {what} at {path}")]
Missing {
what: &'static str,
path: String,
},
#[error("invalid {what} {value:?} at {path}: {reason}")]
Invalid {
what: &'static str,
value: String,
path: String,
reason: String,
},
#[error("unresolved {kind} reference {reference:?} at {path}")]
UnresolvedReference {
kind: RefKind,
reference: String,
path: String,
},
#[error("duplicate {kind} definition {name:?} at {path}")]
DuplicateDefinition {
kind: RefKind,
name: String,
path: String,
},
#[error("container inheritance cycle: {}", .chain.join(" -> "))]
InheritanceCycle {
chain: Vec<String>,
},
#[error("unsupported XTCE construct <{element}> at {path}")]
Unsupported {
element: String,
path: String,
},
#[error("cannot expand the array at {path}: {reason}")]
ArrayNotExpanded {
reason: String,
path: String,
},
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum RefKind {
Parameter,
ParameterType,
ArgumentType,
Container,
MetaCommand,
}
impl fmt::Display for RefKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Parameter => "parameter",
Self::ParameterType => "parameter type",
Self::ArgumentType => "argument type",
Self::Container => "container",
Self::MetaCommand => "telecommand",
})
}
}
#[derive(Debug, thiserror::Error)]
pub struct ParseError {
kind: ParseErrorKind,
offset: Option<u64>,
}
impl ParseError {
pub(crate) fn new(kind: ParseErrorKind) -> Self {
Self { kind, offset: None }
}
pub(crate) fn at_offset(offset: u64, source: impl Into<ParseErrorKind>) -> Self {
Self {
kind: source.into(),
offset: Some(offset),
}
}
#[must_use]
pub fn offset(&self) -> Option<u64> {
self.offset
}
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.offset {
Some(offset) => write!(f, "XML error at byte {offset}: {}", self.kind),
None => write!(f, "XML error: {}", self.kind),
}
}
}
#[derive(Debug, thiserror::Error)]
pub(crate) enum ParseErrorKind {
#[error(transparent)]
Xml(#[from] quick_xml::Error),
#[error("document contains no elements")]
EmptyDocument,
}
impl From<quick_xml::encoding::EncodingError> for ParseErrorKind {
fn from(value: quick_xml::encoding::EncodingError) -> Self {
Self::Xml(quick_xml::Error::Encoding(value))
}
}
impl From<quick_xml::events::attributes::AttrError> for ParseErrorKind {
fn from(value: quick_xml::events::attributes::AttrError) -> Self {
Self::Xml(quick_xml::Error::InvalidAttr(value))
}
}