foundry_common/errors/
fs.rs
1use std::{
2 io,
3 path::{Path, PathBuf},
4};
5
6#[expect(unused_imports)]
7use std::fs::{self, File};
8
9#[derive(Debug, thiserror::Error)]
12#[expect(missing_docs)]
13pub enum FsPathError {
14 #[error("failed to write to {path:?}: {source}")]
16 Write { source: io::Error, path: PathBuf },
17 #[error("failed to read from {path:?}: {source}")]
19 Read { source: io::Error, path: PathBuf },
20 #[error("failed to copy from {from:?} to {to:?}: {source}")]
22 Copy { source: io::Error, from: PathBuf, to: PathBuf },
23 #[error("failed to read from {path:?}: {source}")]
25 ReadLink { source: io::Error, path: PathBuf },
26 #[error("failed to create file {path:?}: {source}")]
28 CreateFile { source: io::Error, path: PathBuf },
29 #[error("failed to remove file {path:?}: {source}")]
31 RemoveFile { source: io::Error, path: PathBuf },
32 #[error("failed to create dir {path:?}: {source}")]
34 CreateDir { source: io::Error, path: PathBuf },
35 #[error("failed to remove dir {path:?}: {source}")]
37 RemoveDir { source: io::Error, path: PathBuf },
38 #[error("failed to open file {path:?}: {source}")]
40 Open { source: io::Error, path: PathBuf },
41 #[error("failed to parse json file: {path:?}: {source}")]
43 ReadJson { source: serde_json::Error, path: PathBuf },
44 #[error("failed to write to json file: {path:?}: {source}")]
46 WriteJson { source: serde_json::Error, path: PathBuf },
47}
48
49impl FsPathError {
50 pub fn write(source: io::Error, path: impl Into<PathBuf>) -> Self {
52 Self::Write { source, path: path.into() }
53 }
54
55 pub fn read(source: io::Error, path: impl Into<PathBuf>) -> Self {
57 Self::Read { source, path: path.into() }
58 }
59
60 pub fn copy(source: io::Error, from: impl Into<PathBuf>, to: impl Into<PathBuf>) -> Self {
62 Self::Copy { source, from: from.into(), to: to.into() }
63 }
64
65 pub fn read_link(source: io::Error, path: impl Into<PathBuf>) -> Self {
67 Self::ReadLink { source, path: path.into() }
68 }
69
70 pub fn create_file(source: io::Error, path: impl Into<PathBuf>) -> Self {
72 Self::CreateFile { source, path: path.into() }
73 }
74
75 pub fn remove_file(source: io::Error, path: impl Into<PathBuf>) -> Self {
77 Self::RemoveFile { source, path: path.into() }
78 }
79
80 pub fn create_dir(source: io::Error, path: impl Into<PathBuf>) -> Self {
82 Self::CreateDir { source, path: path.into() }
83 }
84
85 pub fn remove_dir(source: io::Error, path: impl Into<PathBuf>) -> Self {
87 Self::RemoveDir { source, path: path.into() }
88 }
89
90 pub fn open(source: io::Error, path: impl Into<PathBuf>) -> Self {
92 Self::Open { source, path: path.into() }
93 }
94}
95
96impl AsRef<Path> for FsPathError {
97 fn as_ref(&self) -> &Path {
98 match self {
99 Self::Write { path, .. } |
100 Self::Read { path, .. } |
101 Self::ReadLink { path, .. } |
102 Self::Copy { from: path, .. } |
103 Self::CreateDir { path, .. } |
104 Self::RemoveDir { path, .. } |
105 Self::CreateFile { path, .. } |
106 Self::RemoveFile { path, .. } |
107 Self::Open { path, .. } |
108 Self::ReadJson { path, .. } |
109 Self::WriteJson { path, .. } => path,
110 }
111 }
112}
113
114impl From<FsPathError> for io::Error {
115 fn from(value: FsPathError) -> Self {
116 match value {
117 FsPathError::Write { source, .. } |
118 FsPathError::Read { source, .. } |
119 FsPathError::ReadLink { source, .. } |
120 FsPathError::Copy { source, .. } |
121 FsPathError::CreateDir { source, .. } |
122 FsPathError::RemoveDir { source, .. } |
123 FsPathError::CreateFile { source, .. } |
124 FsPathError::RemoveFile { source, .. } |
125 FsPathError::Open { source, .. } => source,
126
127 FsPathError::ReadJson { source, .. } | FsPathError::WriteJson { source, .. } => {
128 source.into()
129 }
130 }
131 }
132}