Skip to content

Commit 633431a

Browse files
committed
🔨 Update definition of Error
The `Error` type was previously defined as a standalone error object that was convertible from any other object; however an easier definition is possible that requires even less work, and works seemlessly with the `?` operator: `Box<dyn Error>`. This updates the definition to be `Box<dyn Error>` instead, since this now has proper conversions from any other `Error` type, and allows for passing the object along.
1 parent b506f9b commit 633431a

File tree

1 file changed

+18
-37
lines changed

1 file changed

+18
-37
lines changed

‎neotest-common/src/result.rs

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,22 @@
1-
#[derive(Default, PartialEq, PartialOrd, Debug)]
2-
pub struct Error {
3-
message: String,
4-
}
1+
/// A generic error type that is returned from all tests.
2+
///
3+
/// This type can polymorphically take the form of any type that implements
4+
/// [`Error`], which allows any and all generic errors to be a valid return type
5+
/// from tests.
6+
///
7+
/// [`Error`]: std::error::Error
8+
pub type Error = Box<dyn std::error::Error>;
59

6-
impl std::fmt::Display for Error {
7-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
8-
self.message.fmt(f)
9-
}
10-
}
11-
12-
impl std::error::Error for Error {}
13-
14-
impl Error {
15-
pub fn from_error<T: std::error::Error>(e: T) -> Self {
16-
Self {
17-
message: e.to_string(),
18-
}
19-
}
20-
21-
pub fn new() -> Self {
22-
Self::default()
23-
}
24-
}
25-
26-
impl<T> From<T> for Error
27-
where
28-
String: From<T>,
29-
{
30-
fn from(value: T) -> Self {
31-
Self {
32-
message: String::from(value),
33-
}
34-
}
35-
}
36-
37-
/// A result type for unit tests.
10+
/// A [`Result`] object returned from utilities in this test framework.
11+
///
12+
/// This uses the generic [`Error`] implementation to ensure that it can return
13+
/// any and all error objects back from test-cases.
14+
///
15+
/// [`Result`]: std::result::Result
3816
pub type Result<T> = ::std::result::Result<T, Error>;
3917

40-
/// The result that is either implicitly or explicitly returned from tests.
18+
/// A [`Result`] object returned from all tests, either implicitly or explicitly.
19+
///
20+
/// All test results can only successfully return unit `()` objects, and so this
21+
/// `TestResult` type pins the return type.
4122
pub type TestResult = crate::Result<()>;

0 commit comments

Comments
 (0)