Skip to content

Commit dc77ae4

Browse files
committed
style: Rearrange the code slightly
1 parent 61825c2 commit dc77ae4

3 files changed

Lines changed: 65 additions & 62 deletions

File tree

src/error.rs

Lines changed: 22 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,5 @@
11
use std::{error, fmt};
2-
3-
/// The kind of error which occurred.
4-
///
5-
/// Distinguishes between errors which were the result of user actions
6-
/// and those which were the result of system failures. Conceptually
7-
/// similar to HTTP status codes in that 4xx errors are user-caused
8-
/// and 5xx errors are system-caused.
9-
#[derive(Debug, PartialEq, Eq)]
10-
pub enum Kind {
11-
/// An error which was the result of actions that the user took.
12-
///
13-
/// These errors are usually things which a user can easily resolve by
14-
/// changing how they interact with the system. Advice should be used
15-
/// to guide the user to the correct interaction paths and help them
16-
/// self-mitigate without needing to open support tickets.
17-
///
18-
/// These errors are usually generated with [`crate::user`], [`crate::user_with_cause`]
19-
/// and [`crate::user_with_internal`].
20-
User,
21-
22-
/// An error which was the result of the system failing rather than the user's actions.
23-
///
24-
/// These kinds of issues are usually the result of the system entering
25-
/// an unexpected state and/or violating an assumption on behalf of the
26-
/// developer. Often these issues cannot be resolved by the user directly,
27-
/// so the advice should guide them to the best way to raise a bug with you
28-
/// and provide you with information to help them fix the issue.
29-
///
30-
/// These errors are usually generated with [`crate::system`], [`crate::system_with_cause`]
31-
/// and [`crate::system_with_internal`].
32-
System,
33-
}
34-
35-
impl Kind {
36-
fn format_description(&self, description: &str) -> String {
37-
match self {
38-
Kind::User => format!("Oh no! {description}"),
39-
Kind::System => format!("Whoops! {description} (This isn't your fault)"),
40-
}
41-
}
42-
}
2+
use super::Kind;
433

444
/// The fundamental error type used by this library.
455
///
@@ -92,6 +52,27 @@ impl Error {
9252
}
9353
}
9454

55+
/// Checks if this error is of a specific kind.
56+
///
57+
/// Returns `true` if this error matches the provided [Kind],
58+
/// otherwise `false`.
59+
///
60+
/// # Examples
61+
/// ```
62+
/// use human_errors;
63+
///
64+
/// let err = human_errors::user(
65+
/// "We could not open the config file you provided.",
66+
/// &["Make sure that the file exists and is readable by the application."],
67+
/// );
68+
///
69+
/// // Prints "is_user?: true"
70+
/// println!("is_user?: {}", err.is(human_errors::Kind::User));
71+
/// ```
72+
pub fn is(&self, kind: Kind) -> bool {
73+
self.kind == kind
74+
}
75+
9576
/// Gets the description message from this error.
9677
///
9778
/// Gets the description which was provided as the first argument when constructing
@@ -212,27 +193,6 @@ impl Error {
212193

213194
advice
214195
}
215-
216-
/// Checks if this error is of a specific kind.
217-
///
218-
/// Returns `true` if this error matches the provided [Kind],
219-
/// otherwise `false`.
220-
///
221-
/// # Examples
222-
/// ```
223-
/// use human_errors;
224-
///
225-
/// let err = human_errors::user(
226-
/// "We could not open the config file you provided.",
227-
/// &["Make sure that the file exists and is readable by the application."],
228-
/// );
229-
///
230-
/// // Prints "is_user?: true"
231-
/// println!("is_user?: {}", err.is(human_errors::Kind::User));
232-
/// ```
233-
pub fn is(&self, kind: Kind) -> bool {
234-
self.kind == kind
235-
}
236196
}
237197

238198
impl error::Error for Error {

src/kind.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
/// The kind of error which occurred.
3+
///
4+
/// Distinguishes between errors which were the result of user actions
5+
/// and those which were the result of system failures. Conceptually
6+
/// similar to HTTP status codes in that 4xx errors are user-caused
7+
/// and 5xx errors are system-caused.
8+
#[derive(Debug, PartialEq, Eq)]
9+
pub enum Kind {
10+
/// An error which was the result of actions that the user took.
11+
///
12+
/// These errors are usually things which a user can easily resolve by
13+
/// changing how they interact with the system. Advice should be used
14+
/// to guide the user to the correct interaction paths and help them
15+
/// self-mitigate without needing to open support tickets.
16+
///
17+
/// These errors are usually generated with [`crate::user`], [`crate::user_with_cause`]
18+
/// and [`crate::user_with_internal`].
19+
User,
20+
21+
/// An error which was the result of the system failing rather than the user's actions.
22+
///
23+
/// These kinds of issues are usually the result of the system entering
24+
/// an unexpected state and/or violating an assumption on behalf of the
25+
/// developer. Often these issues cannot be resolved by the user directly,
26+
/// so the advice should guide them to the best way to raise a bug with you
27+
/// and provide you with information to help them fix the issue.
28+
///
29+
/// These errors are usually generated with [`crate::system`], [`crate::system_with_cause`]
30+
/// and [`crate::system_with_internal`].
31+
System,
32+
}
33+
34+
impl Kind {
35+
pub(crate) fn format_description(&self, description: &str) -> String {
36+
match self {
37+
Kind::User => format!("{description} (User error)"),
38+
Kind::System => format!("{description} (System failure)"),
39+
}
40+
}
41+
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
mod error;
99
mod from;
1010
mod helpers;
11+
mod kind;
1112
mod result;
1213
mod wrapper;
1314

1415
pub use error::*;
1516
pub use helpers::*;
17+
pub use kind::*;
1618
pub use result::ResultExt;
1719
pub use wrapper::*;

0 commit comments

Comments
 (0)