Skip to main content

fs_core/
error.rs

1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2pub enum FsError {
3    NotFound,
4    NotADirectory,
5    IsADirectory,
6    InvalidArgument,
7    BadFileDescriptor,
8    PermissionDenied,
9    AlreadyExists,
10    NotEmpty,
11}
12
13impl FsError {
14    /// Convert error to errno-style error code
15    pub fn to_errno(&self) -> i32 {
16        match self {
17            FsError::NotFound => -2,          // ENOENT
18            FsError::NotADirectory => -20,    // ENOTDIR
19            FsError::IsADirectory => -21,     // EISDIR
20            FsError::InvalidArgument => -22,  // EINVAL
21            FsError::BadFileDescriptor => -9, // EBADF
22            FsError::PermissionDenied => -13, // EACCES
23            FsError::AlreadyExists => -17,    // EEXIST
24            FsError::NotEmpty => -39,         // ENOTEMPTY
25        }
26    }
27
28    /// Convert error to WASI error-code (u8)
29    /// Reference: WASI Preview 2 filesystem error-code enum
30    /// https://github.com/WebAssembly/wasi-filesystem/blob/main/wit/types.wit
31    pub fn to_wasi_error_code(&self) -> u8 {
32        match self {
33            FsError::NotFound => 44,         // noent
34            FsError::NotADirectory => 54,    // notdir
35            FsError::IsADirectory => 31,     // isdir
36            FsError::InvalidArgument => 28,  // inval
37            FsError::BadFileDescriptor => 8, // badf
38            FsError::PermissionDenied => 2,  // access
39            FsError::AlreadyExists => 20,    // exist
40            FsError::NotEmpty => 55,         // notempty
41        }
42    }
43}