Skip to main content

lux_lib/fs/
mod.rs

1use miette::Diagnostic;
2use thiserror::Error;
3
4pub mod sync;
5pub mod tempfile;
6pub mod tokio;
7#[cfg(unix)]
8pub mod unix;
9#[cfg(windows)]
10pub mod windows;
11
12pub type Result<T> = core::result::Result<T, FsError>;
13
14#[derive(Debug, Error, Diagnostic)]
15#[non_exhaustive]
16pub enum FsError {
17    #[error("failed to read file '{}'", path.display())]
18    #[diagnostic(
19        code(lux_lib::fs::read),
20        help("ensure '{}' exists and is readable", path.display())
21    )]
22    Read {
23        path: std::path::PathBuf,
24        source: std::io::Error,
25    },
26    #[error("failed to read '{}' as UTF-8 string", path.display())]
27    #[diagnostic(
28        code(lux_lib::fs::read_to_string),
29        help("ensure '{}' exists, is readable, and contains valid UTF-8", path.display())
30    )]
31    ReadToString {
32        path: std::path::PathBuf,
33        source: std::io::Error,
34    },
35    #[error("failed to write to '{}'", path.display())]
36    #[diagnostic(
37        code(lux_lib::fs::write),
38        help("ensure the parent directory of '{}' exists and is writable", path.display())
39    )]
40    Write {
41        path: std::path::PathBuf,
42        source: std::io::Error,
43    },
44    #[error("failed to copy '{}' to '{}'", from.display(), to.display())]
45    #[diagnostic(
46        code(lux_lib::fs::copy),
47        help("ensure '{}' exists and the parent directory of '{}' is writable", from.display(), to.display())
48    )]
49    Copy {
50        from: std::path::PathBuf,
51        to: std::path::PathBuf,
52        source: std::io::Error,
53    },
54    #[error("failed to create directory '{}'", path.display())]
55    #[diagnostic(
56        code(lux_lib::fs::create_dir),
57        help("ensure the parent directory of '{}' exists and is writable", path.display())
58    )]
59    CreateDir {
60        path: std::path::PathBuf,
61        source: std::io::Error,
62    },
63    #[error("failed to create a temporaty directory")]
64    #[diagnostic(
65        code(lux_lib::fs::create_tempdir),
66        help("ensure the '{}' exists and is writable", std::env::temp_dir().display())
67    )]
68    CreateTempDir { source: std::io::Error },
69    #[error("failed to create directory tree '{}'", path.display())]
70    #[diagnostic(
71        code(lux_lib::fs::create_dir_all),
72        help("ensure the path to '{}' is accessible", path.display())
73    )]
74    CreateDirAll {
75        path: std::path::PathBuf,
76        source: std::io::Error,
77    },
78    #[error("failed to remove file '{}'", path.display())]
79    #[diagnostic(
80        code(lux_lib::fs::remove_file),
81        help("ensure '{}' exists and is writable", path.display())
82    )]
83    RemoveFile {
84        path: std::path::PathBuf,
85        source: std::io::Error,
86    },
87    #[error("failed to remove directory '{}'", path.display())]
88    #[diagnostic(
89        code(lux_lib::fs::remove_dir_all),
90        help("ensure '{}' exists and is writable", path.display())
91    )]
92    RemoveDirAll {
93        path: std::path::PathBuf,
94        source: std::io::Error,
95    },
96    #[error("failed to read directory '{}'", path.display())]
97    #[diagnostic(
98        code(lux_lib::fs::read_dir),
99        help("ensure '{}' exists and is a directory", path.display())
100    )]
101    ReadDir {
102        path: std::path::PathBuf,
103        source: std::io::Error,
104    },
105    #[error("failed to get metadata for '{}'", path.display())]
106    #[diagnostic(
107        code(lux_lib::fs::metadata),
108        help("ensure '{}' exists", path.display())
109    )]
110    Metadata {
111        path: std::path::PathBuf,
112        source: std::io::Error,
113    },
114    #[error("failed to rename '{}' to '{}'", from.display(), to.display())]
115    #[diagnostic(
116        code(lux_lib::fs::rename),
117        help("ensure '{}' exists and the parent directory of '{}' is writable", from.display(), to.display())
118    )]
119    Rename {
120        from: std::path::PathBuf,
121        to: std::path::PathBuf,
122        source: std::io::Error,
123    },
124    #[error("failed to set permissions on '{}'", path.display())]
125    #[diagnostic(
126        code(lux_lib::fs::set_permissions),
127        help("ensure '{}' exists and is writable", path.display())
128    )]
129    SetPermissions {
130        path: std::path::PathBuf,
131        source: std::io::Error,
132    },
133    #[error("failed to open file '{}'", path.display())]
134    #[diagnostic(
135        code(lux_lib::fs::file_open),
136        help("ensure '{}' exists and is readable", path.display())
137    )]
138    FileOpen {
139        path: std::path::PathBuf,
140        source: std::io::Error,
141    },
142    #[error("failed to create file '{}'", path.display())]
143    #[diagnostic(
144        code(lux_lib::fs::file_create),
145        help("ensure the parent directory of '{}' exists and is writable", path.display())
146    )]
147    FileCreate {
148        path: std::path::PathBuf,
149        source: std::io::Error,
150    },
151    #[error("failed to get the absolute path for '{}'", path.display())]
152    #[diagnostic(code(lux_lib::fs::get_absolute_path))]
153    GetAbsolutePath {
154        path: std::path::PathBuf,
155        source: std::io::Error,
156    },
157    #[error("failed to create symlink '{}' pointing to '{}'", link.display(), target.display())]
158    #[diagnostic(
159        code(lux_lib::fs::symlink),
160        help("ensure the parent directory of '{}' exists and is writable", link.display())
161    )]
162    Symlink {
163        target: std::path::PathBuf,
164        link: std::path::PathBuf,
165        source: std::io::Error,
166    },
167    #[error("{0}")]
168    Other(String),
169}