|
| 1 | +use camino::{Utf8Path, Utf8PathBuf}; |
1 | 2 | use snafu::prelude::*; |
2 | | -use std::path::{Path, PathBuf}; |
3 | 3 |
|
4 | 4 | #[derive(Snafu, Debug)] |
5 | | -#[snafu(display("failed to create directory {} and parents", path.display()))] |
| 5 | +#[snafu(display("failed to create directory {path} and parents"))] |
6 | 6 | pub struct CreateDirAllError { |
7 | | - pub path: PathBuf, |
| 7 | + pub path: Utf8PathBuf, |
8 | 8 | pub source: std::io::Error, |
9 | 9 | } |
10 | 10 |
|
11 | | -pub fn create_dir_all(path: &Path) -> Result<(), CreateDirAllError> { |
| 11 | +pub fn create_dir_all(path: impl AsRef<Utf8Path>) -> Result<(), CreateDirAllError> { |
| 12 | + let path = path.as_ref(); |
12 | 13 | std::fs::create_dir_all(path).context(CreateDirAllSnafu { path }) |
13 | 14 | } |
14 | 15 |
|
15 | 16 | #[derive(Snafu, Debug)] |
16 | | -#[snafu(display("failed to read {}", path.display()))] |
| 17 | +#[snafu(display("failed to read {path}"))] |
17 | 18 | pub struct ReadFileError { |
18 | | - pub path: PathBuf, |
| 19 | + pub path: Utf8PathBuf, |
19 | 20 | pub source: std::io::Error, |
20 | 21 | } |
21 | 22 |
|
22 | | -pub fn read<P: AsRef<Path>>(path: P) -> Result<Vec<u8>, ReadFileError> { |
| 23 | +pub fn read<P: AsRef<Utf8Path>>(path: P) -> Result<Vec<u8>, ReadFileError> { |
23 | 24 | let path = path.as_ref(); |
24 | 25 | std::fs::read(path).context(ReadFileSnafu { path }) |
25 | 26 | } |
26 | 27 |
|
27 | 28 | #[derive(Snafu, Debug)] |
28 | | -#[snafu(display("failed to remove directory {} and contents", path.display()))] |
| 29 | +#[snafu(display("failed to remove directory {path} and contents"))] |
29 | 30 | pub struct RemoveDirAllError { |
30 | | - pub path: PathBuf, |
| 31 | + pub path: Utf8PathBuf, |
31 | 32 | pub source: std::io::Error, |
32 | 33 | } |
33 | 34 |
|
34 | | -pub fn remove_dir_all(path: &Path) -> Result<(), RemoveDirAllError> { |
| 35 | +pub fn remove_dir_all(path: impl AsRef<Utf8Path>) -> Result<(), RemoveDirAllError> { |
| 36 | + let path = path.as_ref(); |
35 | 37 | std::fs::remove_dir_all(path).context(RemoveDirAllSnafu { path }) |
36 | 38 | } |
37 | 39 |
|
38 | 40 | #[derive(Snafu, Debug)] |
39 | | -#[snafu(display("failed to remove file {}", path.display()))] |
| 41 | +#[snafu(display("failed to remove file {path}"))] |
40 | 42 | pub struct RemoveFileError { |
41 | | - pub path: PathBuf, |
| 43 | + pub path: Utf8PathBuf, |
42 | 44 | pub source: std::io::Error, |
43 | 45 | } |
44 | 46 |
|
45 | | -pub fn remove_file<P: AsRef<Path>>(path: P) -> Result<(), RemoveFileError> { |
| 47 | +pub fn remove_file<P: AsRef<Utf8Path>>(path: P) -> Result<(), RemoveFileError> { |
46 | 48 | let path = path.as_ref(); |
47 | 49 | std::fs::remove_file(path).context(RemoveFileSnafu { path }) |
48 | 50 | } |
49 | 51 |
|
50 | 52 | #[derive(Snafu, Debug)] |
51 | | -#[snafu(display("failed to write {}", path.display()))] |
| 53 | +#[snafu(display("failed to write {path}"))] |
52 | 54 | pub struct WriteFileError { |
53 | | - pub path: PathBuf, |
| 55 | + pub path: Utf8PathBuf, |
54 | 56 | pub source: std::io::Error, |
55 | 57 | } |
56 | 58 |
|
57 | | -pub fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> Result<(), WriteFileError> { |
| 59 | +pub fn write<P: AsRef<Utf8Path>, C: AsRef<[u8]>>( |
| 60 | + path: P, |
| 61 | + contents: C, |
| 62 | +) -> Result<(), WriteFileError> { |
58 | 63 | let path = path.as_ref(); |
59 | 64 | std::fs::write(path, contents).context(WriteFileSnafu { path }) |
60 | 65 | } |
0 commit comments