1use std::sync::Arc;
4
5use crate::{graph::Graph, validate::ValidationReport};
6
7#[derive(Clone)]
15pub struct PackingError {
16 pub(crate) graph: Arc<Graph>,
18}
19
20#[derive(Debug, Clone)]
22pub enum Error {
23 ValidationFailed(ValidationReport),
29 PackingFailed(PackingError),
35 InvalidInput(&'static str),
37}
38
39impl PackingError {
40 #[cfg(feature = "dot2")]
44 pub fn write_graph_viz(&self, path: impl AsRef<std::path::Path>) -> std::io::Result<()> {
45 self.graph.write_graph_viz(path)
46 }
47}
48
49impl std::fmt::Display for Error {
50 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
51 match self {
52 Error::ValidationFailed(report) => report.fmt(f),
53 Error::PackingFailed(error) => error.fmt(f),
54 Error::InvalidInput(msg) => write!(f, "Invalid input: {}", msg),
55 }
56 }
57}
58
59impl std::fmt::Display for PackingError {
60 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61 write!(
62 f,
63 "Table packing failed with {} overflows",
64 self.graph.find_overflows().len()
65 )
66 }
67}
68
69impl std::fmt::Debug for PackingError {
70 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
71 write!(f, "{self}")
72 }
73}
74
75impl std::error::Error for PackingError {}
76impl std::error::Error for Error {}
77
78impl From<ValidationReport> for Error {
79 fn from(value: ValidationReport) -> Self {
80 Error::ValidationFailed(value)
81 }
82}
83
84#[cfg(test)]
85mod tests {
86 use super::*;
87
88 #[test]
90 fn assert_compiler_error_is_send() {
91 fn send_me_baby<T: Send>() {}
92 send_me_baby::<Error>();
93 }
94}