1use std::sync::Arc;
4
5use types::Tag;
6
7use crate::{graph::Graph, validate::ValidationReport};
8
9#[derive(Clone, Debug)]
14#[non_exhaustive]
15pub struct BuilderError {
16 pub tag: Tag,
18 pub inner: Error,
20}
21
22#[derive(Clone)]
30pub struct PackingError {
31 pub(crate) graph: Arc<Graph>,
33}
34
35#[derive(Debug, Clone)]
37pub enum Error {
38 ValidationFailed(ValidationReport),
44 PackingFailed(PackingError),
50 InvalidInput(&'static str),
52}
53
54impl PackingError {
55 #[cfg(feature = "dot2")]
59 pub fn write_graph_viz(&self, path: impl AsRef<std::path::Path>) -> std::io::Result<()> {
60 self.graph.write_graph_viz(path)
61 }
62}
63
64impl std::fmt::Display for BuilderError {
65 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66 write!(f, "failed to build '{}' table: '{}'", self.tag, self.inner)
67 }
68}
69
70impl std::error::Error for BuilderError {
71 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
72 Some(&self.inner)
73 }
74}
75
76impl std::fmt::Display for Error {
77 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
78 match self {
79 Error::ValidationFailed(report) => report.fmt(f),
80 Error::PackingFailed(error) => error.fmt(f),
81 Error::InvalidInput(msg) => write!(f, "Invalid input: {}", msg),
82 }
83 }
84}
85
86impl std::fmt::Display for PackingError {
87 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
88 write!(
89 f,
90 "Table packing failed with {} overflows",
91 self.graph.find_overflows().len()
92 )
93 }
94}
95
96impl std::fmt::Debug for PackingError {
97 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
98 write!(f, "{self}")
99 }
100}
101
102impl std::error::Error for PackingError {}
103impl std::error::Error for Error {}
104
105impl From<ValidationReport> for Error {
106 fn from(value: ValidationReport) -> Self {
107 Error::ValidationFailed(value)
108 }
109}
110
111#[cfg(test)]
112mod tests {
113 use super::*;
114
115 #[test]
117 fn assert_compiler_error_is_send() {
118 fn send_me_baby<T: Send>() {}
119 send_me_baby::<Error>();
120 }
121}