|
18 | 18 | use std::any::Any;
|
19 | 19 | use std::collections::HashMap;
|
20 | 20 | use std::ffi::CStr;
|
| 21 | +use std::fmt; |
21 | 22 | use std::io;
|
22 | 23 | use std::io::{Error, ErrorKind, Result};
|
23 | 24 | use std::ops::Deref;
|
@@ -89,6 +90,23 @@ pub enum VfsError {
|
89 | 90 | Initialize(String),
|
90 | 91 | }
|
91 | 92 |
|
| 93 | +impl fmt::Display for VfsError { |
| 94 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 95 | + use self::VfsError::*; |
| 96 | + match self { |
| 97 | + Unsupported => write!(f, "Vfs operation not supported"), |
| 98 | + Mount(e) => write!(f, "Mount backend filesystem: {e}"), |
| 99 | + InodeIndex(s) => write!(f, "Illegal inode index: {s}"), |
| 100 | + FsIndex(e) => write!(f, "Filesystem index error: {e}"), |
| 101 | + PathWalk(e) => write!(f, "Walking path error: {e}"), |
| 102 | + NotFound(s) => write!(f, "Entry can't be found: {s}"), |
| 103 | + Initialize(s) => write!(f, "File system can't be initialized: {s}"), |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +impl std::error::Error for VfsError {} |
| 109 | + |
92 | 110 | /// Vfs result
|
93 | 111 | pub type VfsResult<T> = std::result::Result<T, VfsError>;
|
94 | 112 |
|
@@ -556,6 +574,7 @@ mod tests {
|
556 | 574 | use super::*;
|
557 | 575 | use crate::api::Vfs;
|
558 | 576 | use std::ffi::CString;
|
| 577 | + use std::io::{Error, ErrorKind}; |
559 | 578 |
|
560 | 579 | pub(crate) struct FakeFileSystemOne {}
|
561 | 580 | impl FileSystem for FakeFileSystemOne {
|
@@ -1152,4 +1171,45 @@ mod tests {
|
1152 | 1171 | vfs.allocate_fs_idx().unwrap_err();
|
1153 | 1172 | }
|
1154 | 1173 | }
|
| 1174 | + |
| 1175 | + #[test] |
| 1176 | + fn test_fmt_vfs_error() { |
| 1177 | + assert_eq!( |
| 1178 | + format!("{}", VfsError::Unsupported), |
| 1179 | + "Vfs operation not supported".to_string() |
| 1180 | + ); |
| 1181 | + assert_eq!( |
| 1182 | + format!( |
| 1183 | + "{}", |
| 1184 | + VfsError::Mount(Error::new(ErrorKind::Other, "mount".to_string()),) |
| 1185 | + ), |
| 1186 | + "Mount backend filesystem: mount".to_string() |
| 1187 | + ); |
| 1188 | + assert_eq!( |
| 1189 | + format!("{}", VfsError::InodeIndex("inode index".to_string())), |
| 1190 | + "Illegal inode index: inode index".to_string() |
| 1191 | + ); |
| 1192 | + assert_eq!( |
| 1193 | + format!( |
| 1194 | + "{}", |
| 1195 | + VfsError::FsIndex(Error::new(ErrorKind::Other, "fs index".to_string()),) |
| 1196 | + ), |
| 1197 | + "Filesystem index error: fs index".to_string() |
| 1198 | + ); |
| 1199 | + assert_eq!( |
| 1200 | + format!( |
| 1201 | + "{}", |
| 1202 | + VfsError::PathWalk(Error::new(ErrorKind::Other, "path walk".to_string()),) |
| 1203 | + ), |
| 1204 | + "Walking path error: path walk".to_string() |
| 1205 | + ); |
| 1206 | + assert_eq!( |
| 1207 | + format!("{}", VfsError::NotFound("not found".to_string())), |
| 1208 | + "Entry can't be found: not found".to_string() |
| 1209 | + ); |
| 1210 | + assert_eq!( |
| 1211 | + format!("{}", VfsError::Initialize("initialize".to_string())), |
| 1212 | + "File system can't be initialized: initialize".to_string() |
| 1213 | + ); |
| 1214 | + } |
1155 | 1215 | }
|
0 commit comments