summary refs log tree commit diff
path: root/src/path/error.rs
blob: aee6e9f690b6b51aacc189c3e912d8f052c22fd3 (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
#![forbid(unsafe_code)]

use crate::path::GenericPath;

pub type Result<T> = std::result::Result<T, PathError>;


#[derive(Clone,Debug,Eq,Hash,Ord,PartialEq,PartialOrd)]
pub enum FileNameError {
  ContainsSlash(String),
}

#[derive(Clone,Debug,Eq,Hash,Ord,PartialEq,PartialOrd)]
pub enum DirectoryNameError {
  ContainsSlash(String),
}

#[derive(Clone,Debug,Eq,Hash,Ord,PartialEq,PartialOrd)]
pub enum PathError {
  Parse(String),
  PathLexicallyDirectory(GenericPath),
  PathLexicallyRelative(GenericPath),
  PathLexicallyInvalid(GenericPath),
  PathListHasEmptyComponents(String),
}


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

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

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

impl std::fmt::Display for FileNameError {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match self {
      FileNameError::ContainsSlash(s) =>
        f.write_fmt(format_args!(
            "File names cannot contain slashes, but {:?} does.", s)),
    }
  }
}

impl std::fmt::Display for DirectoryNameError {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match self {
      DirectoryNameError::ContainsSlash(s) =>
        f.write_fmt(format_args!(
            "File names cannot contain slashes, but {:?} does.", s)),
    }
  }
}

impl std::fmt::Display for PathError {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match self {
      PathError::Parse(s) =>
        f.write_fmt(format_args!("Syntax error in path: {}", s)),
      PathError::PathLexicallyDirectory(path) =>
        f.write_fmt(format_args!(
            "The path {} ends in a slash, but is supposed to refer to a file, \
             not a directory.",
            path)),
      PathError::PathLexicallyRelative(path) =>
        f.write_fmt(format_args!(
            "The path {} is relative, not absolute.",
            path)),
      PathError::PathLexicallyInvalid(path) =>
        f.write_fmt(format_args!(
            "This isn't a valid path. {}",
            path)),
      PathError::PathListHasEmptyComponents(path_list) =>
        f.write_fmt(format_args!(
            "Path list has empty components: {}",
            path_list)),
    }
  }
}