| 1 |
<!-- |
| 2 |
This readme is created with https://github.com/livioribeiro/cargo-readme |
| 3 |
|
| 4 |
Edit `src/lib.rs` and use `cargo readme > README.md` to update it. |
| 5 |
--> |
| 6 |
|
| 7 |
# fs-err |
| 8 |
|
| 9 |
[](https://crates.io/crates/fs-err) |
| 10 |
[](https://github.com/andrewhickman/fs-err/actions?query=workflow%3ACI) |
| 11 |
|
| 12 |
fs-err is a drop-in replacement for [`std::fs`][std::fs] that provides more |
| 13 |
helpful messages on errors. Extra information includes which operations was |
| 14 |
attempted and any involved paths. |
| 15 |
|
| 16 |
## Error Messages |
| 17 |
|
| 18 |
Using [`std::fs`][std::fs], if this code fails: |
| 19 |
|
| 20 |
```rust |
| 21 |
let file = File::open("does not exist.txt")?; |
| 22 |
``` |
| 23 |
|
| 24 |
The error message that Rust gives you isn't very useful: |
| 25 |
|
| 26 |
```txt |
| 27 |
The system cannot find the file specified. (os error 2) |
| 28 |
``` |
| 29 |
|
| 30 |
...but if we use fs-err instead, our error contains more actionable information: |
| 31 |
|
| 32 |
```txt |
| 33 |
failed to open file `does not exist.txt` |
| 34 |
caused by: The system cannot find the file specified. (os error 2) |
| 35 |
``` |
| 36 |
|
| 37 |
## Usage |
| 38 |
|
| 39 |
fs-err's API is the same as [`std::fs`][std::fs], so migrating code to use it is easy. |
| 40 |
|
| 41 |
```rust |
| 42 |
// use std::fs; |
| 43 |
use fs_err as fs; |
| 44 |
|
| 45 |
let contents = fs::read_to_string("foo.txt")?; |
| 46 |
|
| 47 |
println!("Read foo.txt: {}", contents); |
| 48 |
|
| 49 |
``` |
| 50 |
|
| 51 |
fs-err uses [`std::io::Error`][std::io::Error] for all errors. This helps fs-err |
| 52 |
compose well with traits from the standard library like |
| 53 |
[`std::io::Read`][std::io::Read] and crates that use them like |
| 54 |
[`serde_json`][serde_json]: |
| 55 |
|
| 56 |
```rust |
| 57 |
use fs_err::File; |
| 58 |
|
| 59 |
let file = File::open("my-config.json")?; |
| 60 |
|
| 61 |
// If an I/O error occurs inside serde_json, the error will include a file path |
| 62 |
// as well as what operation was being performed. |
| 63 |
let decoded: Vec<String> = serde_json::from_reader(file)?; |
| 64 |
|
| 65 |
println!("Program config: {:?}", decoded); |
| 66 |
|
| 67 |
``` |
| 68 |
|
| 69 |
[std::fs]: https://doc.rust-lang.org/stable/std/fs/ |
| 70 |
[std::io::Error]: https://doc.rust-lang.org/stable/std/io/struct.Error.html |
| 71 |
[std::io::Read]: https://doc.rust-lang.org/stable/std/io/trait.Read.html |
| 72 |
[serde_json]: https://crates.io/crates/serde_json |
| 73 |
|
| 74 |
## License |
| 75 |
|
| 76 |
Licensed under either of |
| 77 |
|
| 78 |
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or https://www.apache.org/licenses/LICENSE-2.0) |
| 79 |
* MIT license ([LICENSE-MIT](LICENSE-MIT) or https://opensource.org/licenses/MIT) |
| 80 |
|
| 81 |
at your option. |
| 82 |
|
| 83 |
### Contribution |
| 84 |
|
| 85 |
Unless you explicitly state otherwise, any contribution intentionally |
| 86 |
submitted for inclusion in the work by you, as defined in the Apache-2.0 |
| 87 |
license, shall be dual licensed as above, without any additional terms or |
| 88 |
conditions. |