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
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use std::fmt;
use std::path::{Path, PathBuf};

use thiserror::Error;

#[derive(Debug, Error)]
#[error("Expected {expected} {location} but observed: {observed}")]
pub struct ParseError {
    expected: &'static str,
    observed: String,
    location: Location,
}

#[derive(Debug)]
pub enum Location {
    Unknown,
    File { path: PathBuf, line: usize },
    Item { type_: &'static str, index: usize },
}

impl fmt::Display for Location {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Location::Unknown => write!(f, "at unknown location"),
            Location::File { path, line } => {
                write!(f, "in file {} on line {}", path.as_path().display(), line)
            }
            Location::Item { type_, index } => {
                write!(f, "for item of type {} at index {}", type_, index)
            }
        }
    }
}

impl ParseError {
    pub fn somewhere(expected: &'static str, observed: String) -> Self {
        Self {
            expected,
            observed,
            location: Location::Unknown,
        }
    }

    pub fn file(path: PathBuf, line: usize, expected: &'static str, observed: String) -> Self {
        let location = Location::File { path, line };
        Self {
            observed,
            expected,
            location,
        }
    }

    pub fn item(
        type_: &'static str,
        index: usize,
        expected: &'static str,
        observed: String,
    ) -> Self {
        let location = Location::Item { type_, index };
        Self {
            observed,
            expected,
            location,
        }
    }
}

#[derive(Debug, Error)]
pub struct FileError {
    path: Option<PathBuf>,
    #[source]
    source: FileErrorSource,
}

impl FileError {
    pub fn io<P: AsRef<Path>>(path: Option<P>, error: std::io::Error) -> Self {
        let path = match path {
            Some(p) => Some(p.as_ref().to_path_buf()),
            None => None,
        };
        Self {
            path,
            source: error.into(),
        }
    }

    pub fn parse<P: AsRef<Path>>(path: Option<P>, error: ParseError) -> Self {
        let path = match path {
            Some(p) => Some(p.as_ref().to_path_buf()),
            None => None,
        };
        Self {
            path,
            source: error.into(),
        }
    }
}

impl fmt::Display for FileError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.path {
            Some(path) => write!(f, "Failed to work with file {}", path.display()),
            None => write!(f, "Failed to work fith anonymous file"),
        }
    }
}

#[derive(Debug, Error)]
pub enum FileErrorSource {
    #[error("Failed to parse file")]
    Parse {
        #[from]
        source: ParseError,
    },
    #[error("Failed to read/write to file")]
    IO {
        #[from]
        source: std::io::Error,
    },
}

#[derive(Debug, Error)]
pub struct SequenceError {
    sequence_name: String,
    message: &'static str,
    #[source]
    source: Option<Box<dyn std::error::Error>>,
}

impl SequenceError {
    pub fn new(
        sequence_name: String,
        message: &'static str,
        source: Option<Box<dyn std::error::Error>>,
    ) -> Self {
        Self {
            sequence_name,
            message,
            source,
        }
    }
}

impl fmt::Display for SequenceError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Error in Sequence {}: {}",
            self.sequence_name, self.message
        )
    }
}

/// Catch-all error for top-level API
#[derive(Debug, Error)]
pub enum MutexpectError {
    #[error(transparent)]
    ParseError(#[from] ParseError),
    #[error(transparent)]
    FileError(#[from] FileError),
    #[error(transparent)]
    SequenceError(#[from] SequenceError),
}