use thiserror::Error;
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum ErrorPathException {
#[error("无法切换路径到 {path},目录不存在")]
PathNotFound {
path: String,
},
#[error("路径为空")]
EmptyPath,
#[error("路径越界: {path}")]
OutOfBounds {
path: String,
},
}
impl ErrorPathException {
pub fn not_found(path: impl Into<String>) -> Self {
Self::PathNotFound { path: path.into() }
}
pub fn empty() -> Self {
Self::EmptyPath
}
pub fn out_of_bounds(path: impl Into<String>) -> Self {
Self::OutOfBounds { path: path.into() }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_path_not_found() {
let e = ErrorPathException::not_found("/Doc_0/Res");
assert!(e.to_string().contains("/Doc_0/Res"));
assert!(e.to_string().contains("不存在"));
}
#[test]
fn test_empty_path() {
let e = ErrorPathException::empty();
assert!(e.to_string().contains("为空"));
}
#[test]
fn test_out_of_bounds() {
let e = ErrorPathException::out_of_bounds("../../..");
assert!(e.to_string().contains("../../.."));
}
}