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
//! # my-pretty-failure
//!
//! [![Build Status](https://travis-ci.org/AlbanMinassian/my-pretty-failure.svg?branch=master)](https://travis-ci.org/AlbanMinassian/my-pretty-failure)
//! [![codecov](https://codecov.io/gh/AlbanMinassian/my-pretty-failure/branch/master/graph/badge.svg)](https://codecov.io/gh/AlbanMinassian/my-pretty-failure)
//! [![License:MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
//! [![my-pretty-failure Latest Version](https://img.shields.io/crates/v/my-pretty-failure.svg)](https://crates.io/crates/my-pretty-failure)
//!
//! my-pretty-failure display [failure](https://github.com/rust-lang-nursery/failure) (and context) in an elegant way
//!
//! ## Example n°1
//!
//! With defaut option
//! ```rust,ignore
//! extern crate my_pretty_failure;
//! use my_pretty_failure::myprettyfailure;

//! fn main() {
//!     let err = err1().unwrap_err(); // your failure
//!     println!("{}", myprettyfailure(&err)); // or panic!
//! }
//! ```
//!  console output
//! ```console

//! 🔥 error
//! ---------------------------------------------------------
//! a long err1
//! ---------------------------------------------------------
//!  ▶ caused by: a very long err2
//!   ▶ caused by: an another deep err3
//! ---------------------------------------------------------
//! ```
//!
//! ## Example n°2
//!
//! With your options
//! ```rust,ignore
//! extern crate my_pretty_failure;
//! use my_pretty_failure::{myprettyfailure_option, MyPrettyFailurePrint};
//! extern crate yansi; // or ansi_term, colored ...

//! fn main() {
//!     let err = err1().unwrap_err(); // your failure
//!     println!("{}", myprettyfailure_option(MyPrettyFailurePrint {
//!         head: format!("🔔 my pretty app catch an {}", yansi::Paint::red("error")),
//!         separator: "- - - - - - - - - - - - - - - - - - -".to_string(),
//!         causedby: "context".to_string(),
//!     }, &err));
//! }
//! ```
//!  console output
//! ```console

//! 🔔 my pretty app catch an error
//! - - - - - - - - - - - - - - - - - - -
//! a long err1
//! - - - - - - - - - - - - - - - - - - -
//!  ▶ context: a very long err2
//!   ▶ context: an another deep err3
//! - - - - - - - - - - - - - - - - - - -
//! ```
//!

//! ## Links
//!
//! github: [https://github.com/AlbanMinassian/my-pretty-failure](https://github.com/AlbanMinassian/my-pretty-failure)

extern crate failure;
use failure::{Fail};

// -------------------------------------------------------------------
// struct
// -------------------------------------------------------------------
#[derive(Debug, Clone)]
pub struct MyPrettyFailurePrint {
  pub head: String,
  pub separator: String,
  pub causedby: String,
}

// -------------------------------------------------------------------
// print pretty failure message with your options
// -------------------------------------------------------------------
/// display [failure](https://github.com/rust-lang-nursery/failure) (and context) with *your* options in an elegant way
// -------------------------------------------------------------------
pub fn myprettyfailure_option(option: MyPrettyFailurePrint, fail: &Fail) -> String {
    let mut count_context = 0;
    let mut _indent = " ".to_string();
    let mut message: String = option.head;
    message.push_str("\n");
    message.push_str(&option.separator);
    message.push_str("\n");
    message.push_str(&fail.to_string());
    // message.push_str(format!("- {:?} - ", cause).as_str()); // <=== HOW DISPLAY string struct "Error<Xxxxx>" ? (is possible ?)
    // message.push_str(format!("- {:?} - ", cause).as_str()); // <=== HOW DISPLAY string enum "Error<Xxxxx>Kind" ? (is possible ?)
    message.push_str("\n");
    message.push_str(&option.separator);
    for cause in fail.iter_causes() {
        message.push_str("\n");
        message.push_str(&_indent); _indent.push_str(&" ".to_string());
        message.push_str("▶ ");
        // message.push_str(format!("- {:?} - ", cause).as_str()); // <=== HOW DISPLAY string struct "Error<Xxxxx>" ? (if exist ?, is possible ?)
        // message.push_str(format!("- {:?} - ", cause).as_str()); // <=== HOW DISPLAY string enum "Error<Xxxxx>Kind" ? (if not exist ?, is possible ?)
        message.push_str(&option.causedby);
        message.push_str(": ");
        message.push_str(&cause.to_string());
        count_context = count_context + 1;
    }
    if count_context != 0 {
      message.push_str("\n");
      message.push_str(&option.separator);
    }
    message
}

// -------------------------------------------------------------------
// print pretty failure message with default options
// -------------------------------------------------------------------
/// display [failure](https://github.com/rust-lang-nursery/failure) (and context) with *default* options in an elegant way
// -------------------------------------------------------------------
pub fn myprettyfailure(fail: &Fail) -> String {
    myprettyfailure_option(MyPrettyFailurePrint {
        head: "🔥 error".to_string(),
        separator: "---------------------------------------------------------".to_string(),
        causedby: "caused by".to_string()}, fail)
}