summary refs log tree commit diff
path: root/src/error.rs
blob: 8e51880e46161460ba1b77b22b5145dd2b659566 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use crate::path::GenericPath;


type ParseError<'a> =
  lalrpop_util::ParseError<usize, lalrpop_util::lexer::Token<'a>, &'a str>;

#[derive(Debug)]
pub enum Error {
  IO(std::io::Error),
  Parse(String),
  PathListHasEmptyComponents(String),
  PathIsAFile(GenericPath),
  PathIsRelative(GenericPath),
  PathInvalid(GenericPath),
}

impl std::error::Error for Error { }

impl std::fmt::Display for Error {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match self {
      Error::IO(e) => e.fmt(f),
      Error::Parse(e) => e.fmt(f),
      Error::PathListHasEmptyComponents(path_list) =>
        f.write_fmt(format_args!(
            "Path list has empty components: {}",
            path_list)),
      Error::PathIsAFile(path) =>
        f.write_fmt(format_args!(
            "There's a file at {}, not a directory.",
            path)),
      Error::PathIsRelative(path) =>
        f.write_fmt(format_args!(
            "The path {} is relative, not absolute.",
            path)),
      Error::PathInvalid(path) =>
        f.write_fmt(format_args!(
            "This isn't a valid path. {}",
            path)),
    }
  }
}

impl From<std::io::Error> for Error {
  fn from(e: std::io::Error) -> Error {
    Error::IO(e)
  }
}

impl From<ParseError<'_>> for Error {
  fn from(e: ParseError<'_>) -> Error {
    Error::Parse(format!("{}", e))
  }
}