Skip to main content

xtap_core_lib/
error.rs

1use thiserror::Error;
2
3#[derive(Error, Debug)]
4pub enum CoreError {
5    #[error("IO 错误: {0}")]
6    Io(#[from] std::io::Error),
7
8    #[error("路径不存在: {0}")]
9    PathNotFound(String),
10
11    #[error("权限不足: {0}")]
12    PermissionDenied(String),
13
14    #[error("系统错误: {0}")]
15    SystemError(String),
16
17    #[error("未知错误")]
18    Unknown,
19}
20
21pub type Result<T> = std::result::Result<T, anyhow::Error>;
22
23#[cfg(test)]
24mod tests {
25    use super::*;
26
27    #[test]
28    fn test_display_path_not_found() {
29        let e = CoreError::PathNotFound("/tmp/missing".into());
30        assert_eq!(e.to_string(), "路径不存在: /tmp/missing");
31    }
32
33    #[test]
34    fn test_display_unknown() {
35        assert_eq!(CoreError::Unknown.to_string(), "未知错误");
36    }
37
38    #[test]
39    fn test_io_from_conversion() {
40        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file gone");
41        let e: CoreError = io_err.into();
42        assert!(matches!(e, CoreError::Io(_)));
43        assert!(e.to_string().starts_with("IO 错误"));
44    }
45}