use core::fmt;
use crate::{Level, Tick};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Error {
InvalidBounds {
min: Level,
max: Level,
},
BudgetExhausted {
limit: usize,
},
BeforeStart {
at: Tick,
start: Tick,
},
EmptyInterval {
start: Tick,
end: Tick,
},
NonPositiveAmount {
amount: Level,
},
KindMismatch,
Overflow,
OutOfMemory,
CapacityExceeded {
container: Container,
capacity: usize,
},
InvalidWindow {
earliest: Tick,
latest: Tick,
},
BufferTooSmall {
needed: usize,
},
Malformed,
ChecksumMismatch,
UnsupportedVersion {
version: u8,
},
InvalidStep {
step: Tick,
},
UnknownResource,
UnknownActivity {
activity: crate::ActivityId,
},
InconsistentPrecedences,
NegativeDuration {
shortest: Tick,
},
SearchExhausted {
checks: usize,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum Container {
Impacts,
ChangePoints,
Targets,
Sets,
Requirements,
Timelines,
States,
Contributors,
Scratch,
}
impl Error {
#[must_use]
pub const fn is_capacity(&self) -> bool {
matches!(
self,
Self::OutOfMemory | Self::CapacityExceeded { .. } | Self::BudgetExhausted { .. }
)
}
}
impl fmt::Display for Container {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Impacts => "impact list",
Self::ChangePoints => "change-point list",
Self::Targets => "target list",
Self::Sets => "set list",
Self::Requirements => "requirement list",
Self::Timelines => "timeline list",
Self::States => "state timeline list",
Self::Contributors => "contributor list",
Self::Scratch => "scratch buffer",
})
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Self::InvalidBounds { min, max } => write!(f, "invalid bounds [{min}, {max}]"),
Self::BudgetExhausted { limit } => write!(f, "impact budget of {limit} exhausted"),
Self::BeforeStart { at, start } => {
write!(f, "impact at tick {at} precedes timeline start {start}")
}
Self::EmptyInterval { start, end } => write!(f, "empty interval [{start}, {end})"),
Self::NonPositiveAmount { amount } => {
write!(f, "claim amount {amount} is not positive")
}
Self::KindMismatch => f.write_str("claimable timelines accept claims only"),
Self::Overflow => f.write_str("arithmetic overflow"),
Self::OutOfMemory => f.write_str("allocation failed"),
Self::CapacityExceeded {
container,
capacity,
} => write!(f, "{container} is full at {capacity} elements"),
Self::InvalidWindow { earliest, latest } => {
write!(f, "invalid start window [{earliest}, {latest}]")
}
Self::BufferTooSmall { needed } => write!(f, "buffer too small, {needed} bytes needed"),
Self::Malformed => f.write_str("malformed encoding"),
Self::ChecksumMismatch => f.write_str("checksum mismatch"),
Self::UnsupportedVersion { version } => {
write!(f, "unsupported encoding version {version}")
}
Self::InvalidStep { step } => write!(f, "sampling step {step} is not positive"),
Self::UnknownResource => f.write_str("demand names an unknown timeline"),
Self::UnknownActivity { activity } => {
write!(f, "precedence names unknown activity {}", activity.0)
}
Self::InconsistentPrecedences => f.write_str("precedences admit no schedule"),
Self::NegativeDuration { shortest } => {
write!(f, "shortest duration {shortest} is negative")
}
Self::SearchExhausted { checks } => {
write!(f, "search exhausted its budget of {checks} checks")
}
}
}
}
impl core::error::Error for Error {}