use core::fmt;
use crate::graph::UnitId;
use crate::millis::Millis;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum GraphError {
UnitLimitExceeded {
limit: usize,
},
UnknownUnit {
id: UnitId,
units: usize,
},
SelfDependency {
unit: UnitId,
},
MetadataEdgeFromOpaqueUnit {
producer: UnitId,
consumer: UnitId,
},
MetadataAfterCompletion {
unit: UnitId,
metadata_at: Millis,
total: Millis,
},
Cycle {
units: Vec<UnitId>,
},
}
impl fmt::Display for GraphError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UnitLimitExceeded { limit } => write!(
f,
"the graph cannot hold more than {limit} units; \
analyse a subset of the workspace instead"
),
Self::UnknownUnit { id, units } => write!(
f,
"unit {id} was named by an edge but this graph holds only {units} units; \
an identifier is only meaningful for the graph that produced it, and one from \
elsewhere is caught here only when it happens to point past the end"
),
Self::SelfDependency { unit } => write!(
f,
"unit {unit} depends on itself, so it can never start; \
drop the edge, since a unit is always available to itself"
),
Self::MetadataEdgeFromOpaqueUnit { producer, consumer } => write!(
f,
"the edge from unit {producer} to unit {consumer} is a metadata edge, \
but unit {producer} emits no metadata; \
make it a full edge, or give unit {producer} a metadata_at time"
),
Self::MetadataAfterCompletion {
unit,
metadata_at,
total,
} => write!(
f,
"unit {unit} emits metadata at {metadata_at} but finishes after {total}; \
metadata cannot be emitted after the unit that emits it has ended, \
so check the parser that produced these two numbers"
),
Self::Cycle { units } => {
write!(
f,
"the dependencies contain a cycle, so no build order exists: "
)?;
for (i, unit) in units.iter().enumerate() {
if i > 0 {
write!(f, " -> ")?;
}
write!(f, "unit {unit}")?;
}
write!(
f,
"; a build graph is acyclic, so this is a defect in whatever produced it"
)
}
}
}
}
impl std::error::Error for GraphError {}