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
8#[derive(Debug, Error, Diagnostic)]
9#[non_exhaustive]
10pub enum FsError {
11    #[error("failed to read file '{}'", path.display())]
12    #[diagnostic(
13        code(lux_lib::fs::read),
14        help("ensure '{}' exists and is readable", path.display())
15    )]
16    Read {
17        path: std::path::PathBuf,
18        source: std::io::Error,
19    },
20    #[error("failed to read '{}' as UTF-8 string", path.display())]
21    #[diagnostic(
22        code(lux_lib::fs::read_to_string),
23        help("ensure '{}' exists, is readable, and contains valid UTF-8", path.display())
24    )]
25    ReadToString {
26        path: std::path::PathBuf,
27        source: std::io::Error,
28    },
29    #[error("failed to write to '{}'", path.display())]
30    #[diagnostic(
31        code(lux_lib::fs::write),
32        help("ensure the parent directory of '{}' exists and is writable", path.display())
33    )]
34    Write {
35        path: std::path::PathBuf,
36        source: std::io::Error,
37    },
38    #[error("failed to copy '{}' to '{}'", from.display(), to.display())]
39    #[diagnostic(
40        code(lux_lib::fs::copy),
41        help("ensure '{}' exists and the parent directory of '{}' is writable", from.display(), to.display())
42    )]
43    Copy {
44        from: std::path::PathBuf,
45        to: std::path::PathBuf,
46        source: std::io::Error,
47    },
48    #[error("failed to create directory '{}'", path.display())]
49    #[diagnostic(
50        code(lux_lib::fs::create_dir),
51        help("ensure the parent directory of '{}' exists and is writable", path.display())
52    )]
53    CreateDir {
54        path: std::path::PathBuf,
55        source: std::io::Error,
56    },
57    #[error("failed to create a temporaty directory")]
58    #[diagnostic(
59        code(lux_lib::fs::create_tempdir),
60        help("ensure the '{}' exists and is writable", std::env::temp_dir().display())
61    )]
62    CreateTempDir { source: std::io::Error },
63    #[error("failed to create directory tree '{}'", path.display())]
64    #[diagnostic(
65        code(lux_lib::fs::create_dir_all),
66        help("ensure the path to '{}' is accessible", path.display())
67    )]
68    CreateDirAll {
69        path: std::path::PathBuf,
70        source: std::io::Error,
71    },
72    #[error("failed to remove file '{}'", path.display())]
73    #[diagnostic(
74        code(lux_lib::fs::remove_file),
75        help("ensure '{}' exists and is writable", path.display())
76    )]
77    RemoveFile {
78        path: std::path::PathBuf,
79        source: std::io::Error,
80    },
81    #[error("failed to remove directory '{}'", path.display())]
82    #[diagnostic(
83        code(lux_lib::fs::remove_dir_all),
84        help("ensure '{}' exists and is writable", path.display())
85    )]
86    RemoveDirAll {
87        path: std::path::PathBuf,
88        source: std::io::Error,
89    },
90    #[error("failed to read directory '{}'", path.display())]
91    #[diagnostic(
92        code(lux_lib::fs::read_dir),
93        help("ensure '{}' exists and is a directory", path.display())
94    )]
95    ReadDir {
96        path: std::path::PathBuf,
97        source: std::io::Error,
98    },
99    #[error("failed to get metadata for '{}'", path.display())]
100    #[diagnostic(
101        code(lux_lib::fs::metadata),
102        help("ensure '{}' exists", path.display())
103    )]
104    Metadata {
105        path: std::path::PathBuf,
106        source: std::io::Error,
107    },
108    #[error("failed to rename '{}' to '{}'", from.display(), to.display())]
109    #[diagnostic(
110        code(lux_lib::fs::rename),
111        help("ensure '{}' exists and the parent directory of '{}' is writable", from.display(), to.display())
112    )]
113    Rename {
114        from: std::path::PathBuf,
115        to: std::path::PathBuf,
116        source: std::io::Error,
117    },
118    #[error("failed to set permissions on '{}'", path.display())]
119    #[diagnostic(
120        code(lux_lib::fs::set_permissions),
121        help("ensure '{}' exists and is writable", path.display())
122    )]
123    SetPermissions {
124        path: std::path::PathBuf,
125        source: std::io::Error,
126    },
127    #[error("failed to open file '{}'", path.display())]
128    #[diagnostic(
129        code(lux_lib::fs::file_open),
130        help("ensure '{}' exists and is readable", path.display())
131    )]
132    FileOpen {
133        path: std::path::PathBuf,
134        source: std::io::Error,
135    },
136    #[error("failed to create file '{}'", path.display())]
137    #[diagnostic(
138        code(lux_lib::fs::file_create),
139        help("ensure the parent directory of '{}' exists and is writable", path.display())
140    )]
141    FileCreate {
142        path: std::path::PathBuf,
143        source: std::io::Error,
144    },
145}