Skip to content

Commit 7aacbdf

Browse files
committed
Update docs
1 parent 5b1c9fa commit 7aacbdf

2 files changed

Lines changed: 50 additions & 46 deletions

File tree

README.md

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,13 @@ To use `simple-error`, first add this to your `Cargo.toml`:
1919
simple-error = "0.3"
2020
```
2121

22-
Then add this to your crate root:
22+
Then import what you use (Rust 2018/2021):
2323

2424
```rust
25-
#[macro_use]
26-
extern crate simple_error;
27-
28-
use simple_error::SimpleError;
25+
use simple_error::{SimpleError, try_with};
26+
// Add others as needed: require_with, ensure_with, bail, simple_error, map_err_with
2927
```
3028

31-
Or you can skip the `extern crate` and just import relevant items you use if you are on 2018 edition or beyond.
32-
3329
Now you can use `simple-error` in different ways:
3430

3531
You can use it simply as a string error type:
@@ -44,7 +40,7 @@ You can use it to replace all error types if you only care about a string descri
4440

4541
```rust
4642
fn do_bar() -> Result<(), SimpleError> {
47-
Err(SimpleError::from(std::io::Error(io::ErrorKind::Other, "oh no")))
43+
Err(SimpleError::from(std::io::Error::new(std::io::ErrorKind::Other, "oh no")))
4844
}
4945
```
5046

@@ -87,8 +83,19 @@ fn main() {
8783
You can also ensure a condition holds and early-return a `SimpleError` if it does not:
8884

8985
```rust
86+
use simple_error::{SimpleError, ensure_with};
87+
9088
fn check_config(is_valid: bool) -> Result<(), SimpleError> {
9189
ensure_with!(is_valid, "invalid config");
9290
Ok(())
9391
}
9492
```
93+
94+
## Macros
95+
96+
- `try_with!` — unwrap `Result`, else return `SimpleError::with`.
97+
- `require_with!` — unwrap `Option`, else return `SimpleError::new`.
98+
- `ensure_with!` — assert boolean, else return `SimpleError::new`.
99+
- `bail!` — return early with a `SimpleError` (supports format/expr).
100+
- `simple_error!` — construct a `SimpleError` (supports format/expr).
101+
- `map_err_with!` — map a `Result`’s error with extra context.

src/lib.rs

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
22
html_favicon_url = "https://www.rust-lang.org/favicon.ico",
3-
html_root_url = "https://wisagan.github.io/simple-error/simple_error/")]
3+
html_root_url = "https://docs.rs/simple-error")]
44
#![deny(missing_docs)]
55
//! A simple error type backed by a string.
66
//!
7-
//! This crate provides a `SimpleError` type, which implements `std::error::Error`. The underlying
8-
//! is a `String` as the error message.
7+
//! This crate provides a `SimpleError` type, which implements `std::error::Error`.
8+
//! The underlying type is a `String` used as the error message.
99
//!
1010
//! It should be used when all you care about is an error string.
1111
//!
@@ -29,7 +29,7 @@ impl SimpleError {
2929
/// # Examples
3030
///
3131
/// ```
32-
/// use self::simple_error::SimpleError;
32+
/// use simple_error::SimpleError;
3333
///
3434
/// // errors can be created from `str`
3535
/// let err = SimpleError::new("an error from str");
@@ -50,7 +50,7 @@ impl SimpleError {
5050
/// # Examples
5151
///
5252
/// ```
53-
/// use self::simple_error::SimpleError;
53+
/// use simple_error::SimpleError;
5454
/// use std::io;
5555
///
5656
/// // errors can be created from `io::Error`
@@ -72,7 +72,7 @@ impl SimpleError {
7272
/// # Examples
7373
///
7474
/// ```
75-
/// use self::simple_error::SimpleError;
75+
/// use simple_error::SimpleError;
7676
///
7777
/// let err = SimpleError::with("cannot turn on tv", SimpleError::new("remote not found"));
7878
/// assert_eq!("cannot turn on tv, remote not found", format!("{}", err));
@@ -87,7 +87,7 @@ impl SimpleError {
8787
/// # Examples
8888
///
8989
/// ```
90-
/// use self::simple_error::SimpleError;
90+
/// use simple_error::SimpleError;
9191
///
9292
/// let s = SimpleError::new("critical error");
9393
/// assert_eq!("critical error", s.as_str());
@@ -123,16 +123,16 @@ pub type SimpleResult<T> = Result<T, SimpleError>;
123123

124124
/// Helper macro for unwrapping `Result` values while returning early with a
125125
/// newly constructed `SimpleError` if the value of the expression is `Err`.
126-
/// Can only be used in functions that return `Result<_, SimpleError>`.
126+
/// Can be used in functions that return `Result<_, E>` where `E: From<SimpleError>`
127+
/// (e.g. `SimpleError` or `Box<dyn Error>`).
127128
///
128129
///
129130
/// # Examples
130131
///
131-
/// ```
132-
/// # #[macro_use] extern crate simple_error;
133-
/// # fn main() {
134-
/// use self::simple_error::SimpleError;
135-
/// use std::error::Error;
132+
/// ```
133+
/// # fn main() {
134+
/// use simple_error::{SimpleError, try_with};
135+
/// use std::error::Error;
136136
///
137137
/// fn try_block(result: Result<(), SimpleError>, s: &str) -> Result<(), SimpleError> {
138138
/// Ok(try_with!(result, s))
@@ -186,16 +186,16 @@ macro_rules! try_with {
186186

187187
/// Helper macro for unwrapping `Option` values while returning early with a
188188
/// newly constructed `SimpleError` if the value of the expression is `None`.
189-
/// Can only be used in functions that return `Result<_, SimpleError>`.
189+
/// Can be used in functions that return `Result<_, E>` where `E: From<SimpleError>`
190+
/// (e.g. `SimpleError` or `Box<dyn Error>`).
190191
///
191192
///
192193
/// # Examples
193194
///
194-
/// ```
195-
/// # #[macro_use] extern crate simple_error;
196-
/// # fn main() {
197-
/// use self::simple_error::SimpleError;
198-
/// use std::error::Error;
195+
/// ```
196+
/// # fn main() {
197+
/// use simple_error::{SimpleError, require_with};
198+
/// use std::error::Error;
199199
///
200200
/// fn require_block(maybe: Option<()>, s: &str) -> Result<(), SimpleError> {
201201
/// Ok(require_with!(maybe, s))
@@ -249,15 +249,15 @@ macro_rules! require_with {
249249

250250
/// Helper macro for ensuring a boolean condition while returning early with a
251251
/// newly constructed `SimpleError` if the condition is `false`.
252-
/// Can only be used in functions that return `Result<_, SimpleError>`.
252+
/// Can be used in functions that return `Result<_, E>` where `E: From<SimpleError>`
253+
/// (e.g. `SimpleError` or `Box<dyn Error>`).
253254
///
254255
/// # Examples
255256
///
256-
/// ```
257-
/// # #[macro_use] extern crate simple_error;
258-
/// # fn main() {
259-
/// use self::simple_error::SimpleError;
260-
/// use std::error::Error;
257+
/// ```
258+
/// # fn main() {
259+
/// use simple_error::{SimpleError, ensure_with};
260+
/// use std::error::Error;
261261
///
262262
/// fn ensure_block(cond: bool, s: &str) -> Result<(), SimpleError> {
263263
/// ensure_with!(cond, s);
@@ -310,11 +310,10 @@ macro_rules! ensure_with {
310310
///
311311
/// # Examples
312312
///
313-
/// ```
314-
/// # #[macro_use] extern crate simple_error;
315-
/// # fn main() {
316-
/// use self::simple_error::SimpleError;
317-
/// use std::error::Error;
313+
/// ```
314+
/// # fn main() {
315+
/// use simple_error::{SimpleError, bail};
316+
/// use std::error::Error;
318317
/// // Use with a `Into<SimpleError>`
319318
///
320319
/// struct ErrorSeed;
@@ -364,10 +363,9 @@ macro_rules! bail {
364363
///
365364
/// # Example
366365
///
367-
/// ```
368-
/// # #[macro_use] extern crate simple_error;
369-
/// # fn main() {
370-
/// use self::simple_error::SimpleResult;
366+
/// ```
367+
/// # fn main() {
368+
/// use simple_error::{SimpleResult, simple_error};
371369
///
372370
/// fn add_reason(r: Result<(), ()>) -> SimpleResult<()> {
373371
/// // Use with a string slice
@@ -399,10 +397,9 @@ macro_rules! simple_error {
399397
///
400398
/// # Example
401399
///
402-
/// ```
403-
/// # #[macro_use] extern crate simple_error;
404-
/// # fn main() {
405-
/// use self::simple_error::SimpleResult;
400+
/// ```
401+
/// # fn main() {
402+
/// use simple_error::{SimpleResult, map_err_with};
406403
///
407404
/// fn map_err_with_reason(r: Result<(), std::io::Error>) -> SimpleResult<()> {
408405
/// // Use with a string slice

0 commit comments

Comments
 (0)