summary refs log tree commit diff
path: root/src/error.rs
blob: 1d291ed5ce2ce6286b4b9419ad2d869f631b5adf (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#![forbid(unsafe_code)]

use crate::path::GenericPath;
use crate::path::error::{FileNameError, DirectoryNameError, PathError};
use crate::terminal::error::TerminalError;


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),
  FileName(FileNameError),
  DirectoryName(DirectoryNameError),
  Path(PathError),
  PathEmpiricallyFile(GenericPath),
  Terminal(TerminalError),
}

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::FileName(e) => e.fmt(f),
      Error::DirectoryName(e) => e.fmt(f),
      Error::Path(e) => e.fmt(f),
      Error::PathEmpiricallyFile(path) =>
        f.write_fmt(format_args!(
            "There's a file at {}, not a directory.",
            path)),
      Error::Terminal(e) => e.fmt(f),
    }
  }
}

impl From<()> for Error {
  fn from(_: ()) -> Error {
    unreachable!()
  }
}

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))
  }
}

impl From<FileNameError> for Error {
  fn from(e: FileNameError) -> Error {
    Error::FileName(e)
  }
}

impl From<DirectoryNameError> for Error {
  fn from(e: DirectoryNameError) -> Error {
    Error::DirectoryName(e)
  }
}

impl From<PathError> for Error {
  fn from(e: PathError) -> Error {
    Error::Path(e)
  }
}

impl From<TerminalError> for Error {
  fn from(e: TerminalError) -> Error {
    Error::Terminal(e)
  }
}