use std::{
fmt,
path::{Path, PathBuf},
};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct SourceError {
path: PathBuf,
message: String,
}
impl SourceError {
pub fn new(path: impl Into<PathBuf>, message: impl Into<String>) -> Self {
Self {
path: path.into(),
message: message.into(),
}
}
pub fn path(&self) -> &Path {
&self.path
}
pub fn message(&self) -> &str {
&self.message
}
}
impl fmt::Display for SourceError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}: {}", self.path.display(), self.message)
}
}
impl std::error::Error for SourceError {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn source_error_accessors() {
let err = SourceError::new("test.orr", "something went wrong");
assert_eq!(err.path(), Path::new("test.orr"));
assert_eq!(err.message(), "something went wrong");
}
#[test]
fn source_error_display() {
let err = SourceError::new("shared/styles.orr", "file not found");
assert_eq!(err.to_string(), "shared/styles.orr: file not found");
}
}