1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
//! [![github]](https://github.com/sonro/narrate) [![crates-io]](https://crates.io/crates/narrate) [![docs-rs]](https://docs.rs/narrate)
//!
//! [github]:
//!     https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github
//! [crates-io]:
//!     https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust
//! [docs-rs]:
//!     https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs
//!
//! This library provides Rust CLI applications with console reporting and
//! error-handling utilities. Console output is modeled after
//! [Cargo](https://github.com/rust-lang/cargo), and the [`Error`] type is
//! similar to [anyhow's](https://github.com/dtolnay/anyhow)
//! [`Error`](https://docs.rs/anyhow/1/anyhow/struct.Error.html), but with
//! optional help messages.
//!
//! Minimum supported Rust version: **1.61.1**
//!
//! # Features
//!
//! - Ergonomic [error-handling](#error-handling).
//! - A set of typical CLI application [errors](#cli-errors) (with exit codes).
//! - Errors and app status [reporting](report).
//!
//! #### Cargo Feature Flags
//!
//! All features are enabled by default, but they can be imported individually
//! using [Cargo feature
//! flags](https://doc.rust-lang.org/cargo/reference/features.html#dependency-features):
//!
//! - `error`: Enables error-handling with [`Error`], [`Result`] and
//!   [`ErrorWrap`].
//! - `cli-error`: Enables set of [`CliError`]s with their associated
//!   [`exit_code`](ExitCode).
//! - `report`: Enables reporting errors and statuses to the console with the
//!   [`report`] module.
//!
//! ##### Example `Cargo.toml`
//!
//! ```toml
//! ...
//! [dependencies]
//! narrate = { version = "0.4.0", default-features = false, features = ["report"] }
//! ...
//! ```
//!
//! ## Error Handling
//!
//! Use [`Result<T>`] as a return type for any fallible function. Within the
//! function, use `?` to propagate any error that implements the
//! [`std::error::Error`] trait. Same as
//! [`anyhow::Result<T>`](https://docs.rs/anyhow/1.0/anyhow/type.Result.html).
//!
//! ```
//! # struct User;
//! use narrate::Result;
//!
//! fn get_user() -> Result<User> {
//! # /*
//!     let json = std::fs::read_to_string("user.json")?;
//!     let user: User = serde_json::from_str(&json)?;
//!     Ok(user)
//! # */
//! # Ok(User)
//! }
//! ```
//!
//! #### Returning from `main()`
//!
//! [`Result<T>`] can be used to return from `main()`. If any errors occur it
//! prints the `Debug` implementation for [`Error`].
//!
//! ```no_run
//! use narrate::{bail, Result};
//!
//! fn main() -> Result<()> {
//!     inner_fn()?;
//!     Ok(())
//! }
//!
//! fn inner_fn() -> Result<()> {
//!     bail!("internal error")
//! }
//! ```
//!
//! Console output:
//!
//! ```console
//! Error: internal error
//! ```
//!
//! ### Error Wrap
//!
//! Wrap an error with more context by importing [`ErrorWrap`]. Similar to
//! [`anyhow::Context`](https://docs.rs/anyhow/1.0/anyhow/trait.Context.html).
//! Just add `.wrap(context)` after any function call that returns a `Result`.
//!
//! Context can be anything that implements [`Debug`](std::fmt::Debug),
//! [`Display`](std::fmt::Display), [`Sync`] and [`Send`] -- including [`&str`],
//! [`String`] and errors.
//!
//! ```rust
//! use narrate::{ErrorWrap, Result, CliError};
//!
//! fn run() -> Result<()> {
//! # /*
//!     ...
//!     // wrap with contextual &str
//!     acquire().wrap("unable to acquire data")?;
//!
//!     // or wrap with another error
//!     config.load().wrap(CliError::Config)?;
//!     ...
//! # */
//! # Ok(())
//! }
//!
//! # /*
//! fn acquire() -> Result<(), io::Error> {
//!     ...
//! }
//! # */
//! ```
//!
//! Console output:
//!
//! ```console
//! Error: unable to acquire data
//! Cause: oh no!
//! ```
//!
//! #### Lazy evaluation
//!
//! If your context requires some work to create/format, you should use
//! [`wrap_with`](ErrorWrap::wrap_with) instead.
//!
//! ```rust
//! use narrate::{ErrorWrap, Result, CliError};
//!
//! fn run() -> Result<()> {
//! # /*
//!     ...
//!     // wrap with a formatted string
//!     data.store(path).wrap_with(|| format!("can't save to: {path}"))?;
//!
//!     // wrap with a computed error
//!     data.store(path)
//!         .wrap_with(|| CliError::WriteFile(PathBuf::from(path)))?;
//!     ...
//! # */
//! # Ok(())
//! }
//! ```
//!
//! ### Help Message Wrap
//!
//! Add separate help text to an error. By importing [`ErrorWrap`] you also get
//! the `add_help` method and its lazy version `add_help_with`.
//!
//! ```rust
//! use narrate::{ErrorWrap, Result};
//!
//! fn run() -> Result<()> {
//! # /*
//!     Project::new(path).add_help("try using `project init`")?;
//!     ...
//! # */
//! # Ok(())
//! }
//! ```
//!
//! Console output:
//!
//! ```console
//! Error: directory already exists: '/home/dev/cool-project'
//!
//! try using `project init`
//! ```
//!
//! #### Combination
//!
//! Mix and match the `ErrorWrap` methods throughout your application to make
//! sure the user gets all the information they need.
//!
//! ```rust
//! use narrate::{ErrorWrap, Result};
//! # use std::path::Path;
//!
//! fn run() -> Result<()> {
//! # /*
//!     ...
//!     new_project(&path).wrap("cannot create project")?;
//!     ...
//! # */
//! # Ok(())
//! }
//!
//! fn new_project(path: &Path) -> Result<()> {
//! # /*
//!     ...
//!     create_dir(path)
//!         .wrap_with(|| format!(
//!             "unable to create directory: '{}'",
//!             path.display()
//!         ))
//!         .add_help(
//!             "try using `project init` inside your existing directory"
//!         )?;
//!     ...
//! # */
//! # Ok(())
//! }
//! ```
//!
//! Console output:
//!
//! ```console
//! Error: cannot create project
//! Cause: unable to create directory: '/home/dev/cool-project'
//! Cause: Is a directory (os error 20)
//!
//! try using `project init` inside your existing directory
//! ```
//!
//! ### Convenience Macros
//!
//! Use the [`error_from`] macro to create an ad-hoc [`Error`] from a string or
//! another error. Similar to [`anyhow!`](anyhow::anyhow).
//!
//! ```
//! # use std::collections::HashMap;
//! # use narrate::{error_from, Result};
//! # fn run(map: HashMap<&'static str, String>, key: &str) -> Result<()> {
//! let val = map.get(key).ok_or(error_from!("unknown key"))?;
//! # Ok(())
//! # }
//! ```
//!
//! Use [`bail`] to return early with an error. Equivalent to `return Err(error_from!(...))`.
//!
//!
//! ```
//! # use std::collections::HashMap;
//! # use narrate::{bail, Result};
//! # fn run(map: HashMap<&'static str, String>, key: &str) -> Result<()> {
//! if !map.contains_key(key) {
//!     bail!("unknown key");
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ## CLI Errors
//!
//! Use [`CliError`] for a set of common errors that can occur in a command-line
//! application. These can be used to avoid adding repetitive context for IO
//! errors.
//!
//! ```
//! use narrate::{bail, ErrorWrap, Result, CliError};
//!
//! fn run() -> Result<()> {
//! # /*
//!     ...
//!     match args.operation {
//!         Op::Get => fetch_data(res_name).wrap_with(|| CliError::ResourceNotFound(res_name))?,
//!         Op::Set(data) => set_data(res_name, data).wrap(CliError::InputData)?,
//!         _ => bail!(CliError::Protocol),
//!     }
//!     ...
//! # */
//! # Ok(())
//! }
//! ```
//!
//! ### Exit Codes
//!
//! As this selection of errors can often be fatal for an application, this
//! library provides access to a set of standard program exit codes via the
//! [`ExitCode`] trait. These adhere to
//! [sysexits.h](https://man.openbsd.org/sysexits).
//!
//! Both [`anyhow::Error`] and [`narrate::Error`](Error) implement this trait,
//! thus can provide exit codes. If no [`CliError`] is found as an underlying
//! error, the code will be `70` (for internal software error).
//!
//! Import the [`ExitCode`] trait to use the `exit_code` function, and use
//! [`std::process::exit`] to exit the program with the appropriate code.
//!
//! ```
//! # /*
//! use narrate::ExitCode;
//!
//! if let Err(err) = run() {
//!     std::process::exit(err.exit_code());
//! }
//! # */
//! ```
//!

#[cfg(feature = "error")]
use std::fmt::Display;
#[cfg(feature = "cli-error")]
use std::path::PathBuf;

#[cfg(feature = "error")]
use error::HelpMsg;

#[cfg(feature = "cli-error")]
mod cli_error;
#[cfg(feature = "error")]
mod error;
#[cfg(feature = "cli-error")]
mod exit_code;

#[cfg(feature = "report")]
pub mod report;

#[cfg(feature = "anyhow")]
pub use anyhow;
#[cfg(feature = "report")]
pub use colored;

#[cfg(feature = "report")]
pub use colored::Color;

/// Wrapper around a dynamic error type with an optional help message.
///
/// `Error` works a lot like `Box<dyn std::error::Error>`, but with these
/// differences:
///
/// - `Error` requires that the error is `Send`, `Sync`, and `'static`.
/// - `Error` is represented as a narrow pointer &mdash; exactly one word in
///   size instead of two.
/// - `Error` may contain a help message in order to suggest further actions a
///   user might take.
#[cfg(feature = "error")]
pub struct Error {
    inner: anyhow::Error,
    help: Option<HelpMsg>,
}

/// Iterator of a chain of source errors.
///
/// This type is the iterator returned by [`Error::chain`].
///
/// # Example
///
/// ```
/// use narrate::Error;
/// use std::io;
///
/// pub fn underlying_io_error_kind(error: &Error) -> Option<io::ErrorKind> {
///     for cause in error.chain() {
///         if let Some(io_error) = cause.downcast_ref::<io::Error>() {
///             return Some(io_error.kind());
///         }
///     }
///     None
/// }
/// ```
#[derive(Clone, Default)]
#[repr(transparent)]
#[cfg(feature = "error")]
pub struct Chain<'a> {
    inner: anyhow::Chain<'a>,
}

/// `Result<T, Error>`
///
/// This is a reasonable return type to use throughout your application.
///
/// `narrate::Result` may be used with one *or* two type parameters. Therefore
/// you can import it and not worry about which `Result` you are using. Using it
/// with two types is functionally the same as rust's standard
/// [`Result`](core::result::Result) type.
///
/// ```
/// use narrate::Result;
///
/// # /*
/// fn demo1() -> Result<T> {...}
///            // ^ equivalent to std::result::Result<T, narrate::Error>
///
/// fn demo2() -> Result<T, OtherError> {...}
///            // ^ equivalent to std::result::Result<T, OtherError>
/// */
/// ```
///
/// # Example
///
/// ```
/// # pub trait Deserialize {}
/// #
/// # mod serde_json {
/// #     use super::Deserialize;
/// #     use std::io;
/// #
/// #     pub fn from_str<T: Deserialize>(json: &str) -> io::Result<T> {
/// #         unimplemented!()
/// #     }
/// # }
/// #
/// # #[derive(Debug)]
/// # struct ClusterMap;
/// #
/// # impl Deserialize for ClusterMap {}
/// #
/// # fn main() {
/// # run();
/// # }
/// #
/// use narrate::Result;
///
/// fn run() -> Result<()> {
///     # return Ok(());
///     let config = std::fs::read_to_string("cluster.json")?;
///     let map: ClusterMap = serde_json::from_str(&config)?;
///     println!("cluster info: {:#?}", map);
///     Ok(())
/// }
///
/// ```
#[cfg(feature = "error")]
pub type Result<T, E = Error> = core::result::Result<T, E>;

/// Provides `wrap` and `add_help` methods for [`Result`](core::result::Result).
///
/// This trait is sealed and cannot be implemented for types outside of
/// `narrate`.
///
/// Useful for wrapping a potential error with additional context and/or help
/// message.
///
/// ## Lazy evaluation
///
/// Use `wrap_with` and `add_help_with` methods for lazily evaluation of the
/// added context.
///
/// # Example
///
/// ```
/// use narrate::{ErrorWrap, Result};
/// use std::fs;
/// use std::path::PathBuf;
///
/// pub struct ImportantThing {
///     path: PathBuf,
/// }
///
/// impl ImportantThing {
///     # /**
///     pub fn detach(&mut self) -> Result<()> {...}
///     # */
///     # fn detach(&mut self) -> Result<()> {
///     #     unimplemented!()
///     # }
/// }
///
/// pub fn do_it(mut it: ImportantThing) -> Result<Vec<u8>> {
///     it.detach().wrap("Failed to detach the important thing")?;
///
///     let path = &it.path;
///     let content = fs::read(path)
///         .wrap_with(|| format!("Failed to read instrs from {}", path.display()))
///         .add_help("list of instr in README.md")?;
///
///     Ok(content)
/// }
/// ```
#[cfg(feature = "error")]
pub trait ErrorWrap<T, E>: error::wrap::private::Sealed
where
    E: Send + Sync + 'static,
{
    /// Wrap an error value with additional context.
    fn wrap<C>(self, context: C) -> Result<T, Error>
    where
        C: Display + Send + Sync + 'static;

    /// Wrap an error value with lazily evaluated context.
    fn wrap_with<C, F>(self, f: F) -> Result<T, Error>
    where
        C: Display + Send + Sync + 'static,
        F: FnOnce() -> C;

    /// Add a help message to an error value.
    fn add_help(self, help: &'static str) -> Result<T, Error>;

    /// Add a lazily evaluated help message to an error value.
    fn add_help_with<C, F>(self, f: F) -> Result<T, Error>
    where
        C: Display + Send + Sync + 'static,
        F: FnOnce() -> C;
}

/// Provide `exit_code` method for [CliError]. Intended to be passed to
/// [`std::process::exit`].
#[cfg(feature = "cli-error")]
pub trait ExitCode: exit_code::private::Sealed {
    /// CLI application exit code
    fn exit_code(&self) -> i32 {
        exitcode::SOFTWARE
    }
}

/// Standard command line application error
#[derive(Debug, PartialEq, Eq, Hash)]
#[cfg(feature = "cli-error")]
#[non_exhaustive]
pub enum CliError {
    /// Invalid configuration
    Config,

    /// Cannot create file
    CreateFile(PathBuf),

    /// Invalid input data
    InputData,

    /// Supplied file not found
    InputFileNotFound(PathBuf),

    /// User not found
    NoUser(String),

    /// Host not found
    NoHost(String),

    /// No permission to perform operation
    OperationPermission(String),

    /// Operating system error
    OsErr,

    /// System file not found
    OsFileNotFound(PathBuf),

    /// Cannot read file
    ReadFile(PathBuf),

    /// Resource not found
    ResourceNotFound(String),

    /// Protocol not possible
    Protocol,

    /// Temporary/non fatal error
    Temporary,

    /// Incorrect usage
    Usage,

    /// Cannot write to file
    WriteFile(PathBuf),
}