Skip to main content

secure_exec_vfs_core/engine/
error.rs

1use std::error::Error;
2use std::fmt;
3
4pub type VfsResult<T> = Result<T, VfsError>;
5
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct VfsError {
8    code: &'static str,
9    message: String,
10}
11
12impl VfsError {
13    pub fn new(code: &'static str, message: impl Into<String>) -> Self {
14        Self {
15            code,
16            message: message.into(),
17        }
18    }
19
20    pub fn code(&self) -> &'static str {
21        self.code
22    }
23
24    pub fn message(&self) -> &str {
25        &self.message
26    }
27
28    pub fn enoent(path: impl AsRef<str>) -> Self {
29        Self::new(
30            "ENOENT",
31            format!("no such file or directory: {}", path.as_ref()),
32        )
33    }
34
35    pub fn eexist(path: impl AsRef<str>) -> Self {
36        Self::new("EEXIST", format!("file exists: {}", path.as_ref()))
37    }
38
39    pub fn enotdir(path: impl AsRef<str>) -> Self {
40        Self::new("ENOTDIR", format!("not a directory: {}", path.as_ref()))
41    }
42
43    pub fn eisdir(path: impl AsRef<str>) -> Self {
44        Self::new("EISDIR", format!("is a directory: {}", path.as_ref()))
45    }
46
47    pub fn eloop(path: impl AsRef<str>) -> Self {
48        Self::new(
49            "ELOOP",
50            format!("too many symbolic links: {}", path.as_ref()),
51        )
52    }
53
54    pub fn enametoolong(path: impl AsRef<str>) -> Self {
55        Self::new("ENAMETOOLONG", format!("path too long: {}", path.as_ref()))
56    }
57
58    pub fn enotempty(path: impl AsRef<str>) -> Self {
59        Self::new(
60            "ENOTEMPTY",
61            format!("directory not empty: {}", path.as_ref()),
62        )
63    }
64
65    pub fn eopnotsupp(message: impl Into<String>) -> Self {
66        Self::new("EOPNOTSUPP", message)
67    }
68
69    pub fn erofs(message: impl Into<String>) -> Self {
70        Self::new("EROFS", message)
71    }
72
73    pub fn einval(message: impl Into<String>) -> Self {
74        Self::new("EINVAL", message)
75    }
76
77    pub fn eio(message: impl Into<String>) -> Self {
78        Self::new("EIO", message)
79    }
80}
81
82impl fmt::Display for VfsError {
83    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84        write!(f, "{}: {}", self.code, self.message)
85    }
86}
87
88impl Error for VfsError {}