Skip to main content

sofie_model/
error.rs

1use std::fmt;
2use std::path::{Path, PathBuf};
3
4#[derive(Debug, PartialEq, Eq)]
5pub struct ModelError {
6    pub file: PathBuf,
7    pub line: usize,
8    pub message: String,
9}
10
11impl ModelError {
12    pub fn new(file: &Path, line: usize, message: impl Into<String>) -> Self {
13        Self {
14            file: file.to_path_buf(),
15            line,
16            message: message.into(),
17        }
18    }
19
20    pub fn in_file(file: &Path, message: impl Into<String>) -> Self {
21        Self::new(file, 1, message)
22    }
23}
24
25impl fmt::Display for ModelError {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        write!(f, "{}:{}: {}", self.file.display(), self.line, self.message)
28    }
29}
30
31impl std::error::Error for ModelError {}