Skip to content

Commit 9f9b50c

Browse files
eryugeybergwolf
authored andcommitted
api/vfs: implement Display trait for VfsError
So we could print VfsError. Signed-off-by: Eryu Guan <[email protected]>
1 parent d8031bf commit 9f9b50c

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

src/api/vfs/mod.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use std::any::Any;
1919
use std::collections::HashMap;
2020
use std::ffi::CStr;
21+
use std::fmt;
2122
use std::io;
2223
use std::io::{Error, ErrorKind, Result};
2324
use std::ops::Deref;
@@ -89,6 +90,23 @@ pub enum VfsError {
8990
Initialize(String),
9091
}
9192

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+
92110
/// Vfs result
93111
pub type VfsResult<T> = std::result::Result<T, VfsError>;
94112

@@ -556,6 +574,7 @@ mod tests {
556574
use super::*;
557575
use crate::api::Vfs;
558576
use std::ffi::CString;
577+
use std::io::{Error, ErrorKind};
559578

560579
pub(crate) struct FakeFileSystemOne {}
561580
impl FileSystem for FakeFileSystemOne {
@@ -1152,4 +1171,45 @@ mod tests {
11521171
vfs.allocate_fs_idx().unwrap_err();
11531172
}
11541173
}
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+
}
11551215
}

0 commit comments

Comments
 (0)