Skip to main content

easyofd_reader/
error_path_exception.rs

1//! 错误路径异常。
2//!
3//! 对应 Java: org.ofdrw.reader.ErrorPathException
4
5use thiserror::Error;
6
7/// 错误路径异常。
8///
9/// 当资源定位器尝试切换到不存在的目录路径时抛出。
10///
11/// 对应 Java: `org.ofdrw.reader.ErrorPathException`
12#[derive(Debug, Error)]
13#[non_exhaustive]
14pub enum ErrorPathException {
15    /// 路径不存在。
16    #[error("无法切换路径到 {path},目录不存在")]
17    PathNotFound {
18        /// 尝试访问的路径。
19        path: String,
20    },
21
22    /// 路径为空。
23    #[error("路径为空")]
24    EmptyPath,
25
26    /// 路径越界(超出根目录)。
27    #[error("路径越界: {path}")]
28    OutOfBounds {
29        /// 尝试访问的路径。
30        path: String,
31    },
32}
33
34impl ErrorPathException {
35    /// 创建路径不存在异常。
36    pub fn not_found(path: impl Into<String>) -> Self {
37        Self::PathNotFound { path: path.into() }
38    }
39
40    /// 创建路径为空异常。
41    pub fn empty() -> Self {
42        Self::EmptyPath
43    }
44
45    /// 创建路径越界异常。
46    pub fn out_of_bounds(path: impl Into<String>) -> Self {
47        Self::OutOfBounds { path: path.into() }
48    }
49}
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn test_path_not_found() {
57        let e = ErrorPathException::not_found("/Doc_0/Res");
58        assert!(e.to_string().contains("/Doc_0/Res"));
59        assert!(e.to_string().contains("不存在"));
60    }
61
62    #[test]
63    fn test_empty_path() {
64        let e = ErrorPathException::empty();
65        assert!(e.to_string().contains("为空"));
66    }
67
68    #[test]
69    fn test_out_of_bounds() {
70        let e = ErrorPathException::out_of_bounds("../../..");
71        assert!(e.to_string().contains("../../.."));
72    }
73}