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
/* Allows for the conversion of String, &str Error types to be coerced into a
 * UserError.
*/

use super::UserError;

/// Convert an Err(String) into a UserError
///
/// # Example
/// ```
/// use user_error::UserError;
///
/// fn string_error(e: &str) -> Result<(), String> {
///		Err(String::from(e))
///	}
///
///	fn caller() -> Result<(), UserError> {
///		string_error("broken")?;
///		Ok(())
///	}
///
///	match caller() {
///		Err(e) => eprintln!("{}", e),
///		Ok(_) => ()
///	}
/// ```
/// This results in the following being printed to stderr:
/// ```bash
/// Error: broken
/// ```
impl From<String> for UserError {
    fn from(error: String) -> Self {
        UserError {
        	summary: error,
        	reasons: None,
        	subtleties: None,
        	original_errors: None,
        }
    }
}

/// Convert an Err(&'static str) into a UserError
///
/// # Example
/// ```
/// use user_error::UserError;
///
/// fn str_error(path: &str) -> Result<(), &'static str> {
///		Err("Failed!")
///	}
///
///	fn caller() -> Result<(), UserError> {
///		str_error("does_not_exist.txt")?;
///		Ok(())
///	}
///
///	match caller() {
///		Err(e) => eprintln!("{}", e),
///		Ok(_) => ()
///	}
/// ```
/// This results in the following being printed to stderr:
/// ```bash
/// Error: Failed!
/// ```
impl From<&str> for UserError {
    fn from(error: &str) -> Self {
        UserError {
        	summary: String::from(error),
        	reasons: None,
        	subtleties: None,
        	original_errors: None,
        }
    }
}