1use core::fmt;
4use std::path::PathBuf;
5
6#[derive(Debug)]
10#[non_exhaustive]
11pub enum Error {
12 Read {
14 path: PathBuf,
16 source: std::io::Error,
18 },
19 Parse {
21 path: PathBuf,
23 source: imping_ir::Error,
25 },
26 Generate {
28 path: PathBuf,
30 source: imping_codegen::Error,
32 },
33 Write {
35 path: PathBuf,
37 source: std::io::Error,
39 },
40 OutDirNotSet,
43}
44
45impl fmt::Display for Error {
46 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47 match self {
48 Error::Read { path, source } => {
49 write!(f, "failed to read IR file `{}`: {source}", path.display())
50 }
51 Error::Parse { path, source } => {
52 write!(f, "`{}` is not a valid IR file: {source}", path.display())?;
53 let mut cause = core::error::Error::source(source);
55 while let Some(error) = cause {
56 write!(f, ": {error}")?;
57 cause = error.source();
58 }
59 Ok(())
60 }
61 Error::Generate { path, source } => write!(
62 f,
63 "failed to generate bindings from `{}`: {source}",
64 path.display()
65 ),
66 Error::Write { path, source } => {
67 write!(f, "failed to write `{}`: {source}", path.display())
68 }
69 Error::OutDirNotSet => f.write_str(
70 "OUT_DIR is not set; `generate()` is for build.rs, use `generate_to(path)` elsewhere",
71 ),
72 }
73 }
74}
75
76impl core::error::Error for Error {
77 fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
78 match self {
79 Error::Read { source, .. } | Error::Write { source, .. } => Some(source),
80 Error::Parse { source, .. } => Some(source),
81 Error::Generate { source, .. } => Some(source),
82 Error::OutDirNotSet => None,
83 }
84 }
85}