use thiserror::Error;
use tycho_common::simulation::errors::SimulationError;
#[derive(Clone, Debug, Error)]
pub enum FeedError {
#[error("feed connection error: {0}")]
Connection(String),
#[error("feed parsing error: {0}")]
Parsing(String),
#[error("feed fatal error: {0}")]
Fatal(String),
#[error("feed invalid input error: {0}")]
InvalidInput(String),
}
impl FeedError {
pub fn is_fatal(&self) -> bool {
matches!(self, FeedError::Fatal(_))
}
pub(crate) fn in_context(self, context: impl std::fmt::Display) -> Self {
match self {
FeedError::Connection(message) => {
FeedError::Connection(format!("{context}: {message}"))
}
FeedError::Parsing(message) => FeedError::Parsing(format!("{context}: {message}")),
FeedError::Fatal(message) => FeedError::Fatal(format!("{context}: {message}")),
FeedError::InvalidInput(message) => {
FeedError::InvalidInput(format!("{context}: {message}"))
}
}
}
}
impl From<FeedError> for SimulationError {
fn from(err: FeedError) -> Self {
match err {
FeedError::Connection(message) | FeedError::Parsing(message) => {
SimulationError::RecoverableError(message)
}
FeedError::Fatal(message) => SimulationError::FatalError(message),
FeedError::InvalidInput(message) => SimulationError::InvalidInput(message, None),
}
}
}