1pub type Result<T> = std::result::Result<T, Error>;
2
3#[derive(Default, Debug)]
4pub struct Error {
5 message: String,
6 path: String,
7 span: Option<(usize, usize)>,
8}
9
10impl std::error::Error for Error {}
11
12impl From<Error> for std::io::Error {
13 fn from(error: Error) -> Self {
14 std::io::Error::new(kind:std::io::ErrorKind::Other, error:error.message.as_str())
15 }
16}
17
18impl From<syn::Error> for Error {
19 fn from(error: syn::Error) -> Self {
20 let start: LineColumn = error.span().start();
21 Self { message: error.to_string(), span: Some((start.line, start.column)), ..Self::default() }
22 }
23}
24
25impl std::fmt::Display for Error {
26 fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27 writeln!(fmt, "error: {}", self.message)?;
28 if !self.path.is_empty() {
29 if let Some((line: usize, column: usize)) = self.span {
30 writeln!(fmt, " --> {}:{line}:{column}", self.path)?;
31 } else {
32 writeln!(fmt, " --> {}", self.path)?;
33 }
34 }
35 Ok(())
36 }
37}
38
39impl Error {
40 pub(crate) fn new(message: &str) -> Self {
41 Self { message: message.to_string(), ..Self::default() }
42 }
43
44 pub(crate) fn with_path(self, path: &str) -> Self {
45 Self { path: path.to_string(), ..self }
46 }
47}
48