#[cfg(doc)]
use crate::options::EipsOptions;
use core::fmt::{self, Debug, Display};
#[non_exhaustive]
#[derive(Clone, Copy, Debug)]
pub enum ChangeError<Id> {
BadParentId(Id),
BadDirection(Id),
MergeConflict(Id),
UnsupportedMove(Id),
BadOldLocation(Id),
UnexpectedMove(Id),
OldLocationIsMove(Id),
HiddenMove(Id),
TimestampOverflow {
id: Id,
timestamp: crate::MoveTimestamp,
},
}
impl<Id> ChangeError<Id> {
pub(crate) fn to_basic(&self) -> BasicChangeError {
use BasicChangeError as Basic;
match self {
Self::BadParentId(_) => Basic::BadParentId,
Self::BadDirection(_) => Basic::BadDirection,
Self::MergeConflict(_) => Basic::MergeConflict,
Self::UnsupportedMove(_) => Basic::UnsupportedMove,
Self::BadOldLocation(_) => Basic::BadOldLocation,
Self::UnexpectedMove(_) => Basic::UnexpectedMove,
Self::OldLocationIsMove(_) => Basic::OldLocationIsMove,
Self::HiddenMove(_) => Basic::HiddenMove,
Self::TimestampOverflow {
timestamp,
..
} => Basic::TimestampOverflow {
timestamp: *timestamp,
},
}
}
}
impl<Id: Display> Display for ChangeError<Id> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let basic = self.to_basic();
match self {
Self::BadParentId(id) => write!(f, "{basic}: {id}"),
Self::BadDirection(id) => write!(f, "{basic}: {id}"),
Self::MergeConflict(id) => write!(f, "{basic}: {id}"),
Self::UnsupportedMove(id) => write!(f, "{basic}: {id}"),
Self::BadOldLocation(id) => write!(f, "{basic}: {id}"),
Self::UnexpectedMove(id) => write!(f, "{basic}: {id}"),
Self::OldLocationIsMove(id) => write!(f, "{basic}: {id}"),
Self::HiddenMove(id) => write!(f, "{basic}: {id}"),
Self::TimestampOverflow {
id,
..
} => write!(f, "{basic} (id {id})"),
}
}
}
#[cfg(feature = "std")]
#[cfg_attr(feature = "doc_cfg", doc(cfg(feature = "std")))]
impl<Id: Debug + Display> std::error::Error for ChangeError<Id> {}
#[non_exhaustive]
#[derive(Clone, Copy, Debug)]
pub struct IndexError {
pub index: usize,
}
impl Display for IndexError {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "bad index: {}", self.index)
}
}
#[cfg(feature = "std")]
#[cfg_attr(feature = "doc_cfg", doc(cfg(feature = "std")))]
impl std::error::Error for IndexError {}
#[non_exhaustive]
#[derive(Clone, Copy, Debug)]
pub struct IdError<Id> {
pub id: Id,
}
impl<Id: Display> Display for IdError<Id> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "bad id: {}", self.id)
}
}
#[cfg(feature = "std")]
#[cfg_attr(feature = "doc_cfg", doc(cfg(feature = "std")))]
impl<Id: Debug + Display> std::error::Error for IdError<Id> {}
#[derive(Clone, Copy, Debug)]
pub(crate) enum BasicChangeError {
BadParentId,
BadDirection,
MergeConflict,
UnsupportedMove,
BadOldLocation,
UnexpectedMove,
OldLocationIsMove,
HiddenMove,
TimestampOverflow {
timestamp: crate::MoveTimestamp,
},
}
impl Display for BasicChangeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::BadParentId => write!(f, "bad parent id"),
Self::BadDirection => {
write!(f, "change has no parent but its direction is 'before'")
}
Self::MergeConflict => {
write!(f, "conflict between change and existing data")
}
Self::UnsupportedMove => {
write!(f, "change has move info but moves are unsupported")
}
Self::BadOldLocation => write!(f, "bad old location"),
Self::UnexpectedMove => {
write!(f, "change has no move info but is a move destination")
}
Self::OldLocationIsMove => {
write!(f, "old location is a move destination")
}
Self::HiddenMove => {
write!(f, "change is a move destination but is hidden")
}
Self::TimestampOverflow {
timestamp,
} => write!(f, "move timestamp is too large: {timestamp}"),
}
}
}