Skip to main content

turtle_build/
error.rs

1use crate::{
2    build_graph::BuildGraphError, compile::CompileError, ir::Build,
3    module_dependency::ModuleDependencyError, parse::ParseError,
4};
5use alloc::sync::Arc;
6use core::{
7    error::Error,
8    fmt::{self, Display, Formatter},
9};
10use tokio::{io, task::JoinError};
11
12/// A build error.
13#[derive(Clone, Debug, Eq, PartialEq)]
14pub enum BuildError {
15    /// A build failure.
16    Build,
17    /// A build graph error.
18    BuildGraph(BuildGraphError),
19    /// A compile error.
20    Compile(CompileError),
21    /// A default output not found.
22    DefaultOutputNotFound(Arc<str>),
23    /// A dynamic dependency not found.
24    DynamicDependencyNotFound(Arc<Build>),
25    /// A file not found.
26    FileNotFound(String),
27    /// An input not built.
28    InputNotBuilt(String),
29    /// An input not found.
30    InputNotFound(String),
31    /// A module dependency error.
32    ModuleDependency(ModuleDependencyError),
33    /// Other errors.
34    Other(String),
35    /// An output not found.
36    OutputNotFound(String),
37    /// A parse error.
38    Parse(ParseError),
39}
40
41impl Error for BuildError {}
42
43impl Display for BuildError {
44    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
45        match self {
46            Self::Build => write!(formatter, "build failed"),
47            Self::Compile(error) => write!(formatter, "{error}"),
48            Self::DefaultOutputNotFound(output) => {
49                write!(formatter, "default output \"{output}\" not found")
50            }
51            Self::DynamicDependencyNotFound(build) => {
52                write!(
53                    formatter,
54                    "outputs {} not found in dynamic dependency file {}",
55                    build.outputs().join(", "),
56                    build.dynamic_module().unwrap()
57                )
58            }
59            Self::FileNotFound(path) => write!(formatter, "file \"{path}\" not found"),
60            Self::InputNotBuilt(input) => {
61                write!(formatter, "input \"{input}\" not built yet")
62            }
63            Self::InputNotFound(input) => {
64                write!(formatter, "input \"{input}\" not found")
65            }
66            Self::ModuleDependency(error) => {
67                write!(formatter, "{error}")
68            }
69            Self::Other(message) => write!(formatter, "{message}"),
70            Self::OutputNotFound(output) => {
71                write!(formatter, "output \"{output}\" not found")
72            }
73            Self::Parse(error) => write!(formatter, "{error}"),
74            Self::BuildGraph(error) => write!(formatter, "{error}"),
75        }
76    }
77}
78
79impl From<Box<dyn Error>> for BuildError {
80    fn from(error: Box<dyn Error>) -> Self {
81        Self::Other(error.to_string())
82    }
83}
84
85impl From<CompileError> for BuildError {
86    fn from(error: CompileError) -> Self {
87        Self::Compile(error)
88    }
89}
90
91impl From<io::Error> for BuildError {
92    fn from(error: io::Error) -> Self {
93        Self::Other(error.to_string())
94    }
95}
96
97impl From<JoinError> for BuildError {
98    fn from(error: JoinError) -> Self {
99        Self::Other(error.to_string())
100    }
101}
102
103impl From<ModuleDependencyError> for BuildError {
104    fn from(error: ModuleDependencyError) -> Self {
105        Self::ModuleDependency(error)
106    }
107}
108
109impl From<ParseError> for BuildError {
110    fn from(error: ParseError) -> Self {
111        Self::Parse(error)
112    }
113}
114
115impl From<BuildGraphError> for BuildError {
116    fn from(error: BuildGraphError) -> Self {
117        Self::BuildGraph(error)
118    }
119}