Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 0 additions & 65 deletions crates/libtest2-harness/src/case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,68 +48,3 @@ pub enum Source {
},
Path(std::path::PathBuf),
}

pub type RunResult = Result<(), RunError>;

#[derive(Debug)]
pub struct RunError {
status: notify::MessageKind,
cause: Option<Box<dyn std::error::Error + Send + Sync + 'static>>,
}

impl RunError {
pub fn with_cause(cause: impl std::error::Error + Send + Sync + 'static) -> Self {
Self {
status: notify::MessageKind::Error,
cause: Some(Box::new(cause)),
}
}

pub fn fail(cause: impl std::fmt::Display) -> Self {
Self::with_cause(Message(cause.to_string()))
}

/// Should not be called with `libtest_lexarg::RunIgnored::Yes`
pub fn ignore() -> Self {
Self {
status: notify::MessageKind::Ignored,
cause: None,
}
}

/// Should not be called with `libtest_lexarg::RunIgnored::Yes`
pub fn ignore_for(reason: String) -> Self {
Self {
status: notify::MessageKind::Ignored,
cause: Some(Box::new(Message(reason))),
}
}

pub(crate) fn status(&self) -> notify::MessageKind {
self.status
}

pub(crate) fn cause(&self) -> Option<&(dyn std::error::Error + Send + Sync)> {
self.cause.as_ref().map(|b| b.as_ref())
}
}

impl<E> From<E> for RunError
where
E: std::error::Error + Send + Sync + 'static,
{
fn from(error: E) -> Self {
Self::with_cause(error)
}
}

#[derive(Debug)]
struct Message(String);

impl std::fmt::Display for Message {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(formatter)
}
}

impl std::error::Error for Message {}
91 changes: 91 additions & 0 deletions crates/libtest2-harness/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
pub(crate) use crate::*;

pub type RunResult = Result<(), RunError>;

#[derive(Debug)]
pub struct RunError {
status: notify::MessageKind,
cause: Option<Box<dyn std::error::Error + Send + Sync + 'static>>,
}

impl RunError {
pub fn with_cause(cause: impl std::error::Error + Send + Sync + 'static) -> Self {
Self {
status: notify::MessageKind::Error,
cause: Some(Box::new(cause)),
}
}

pub fn fail(cause: impl std::fmt::Display) -> Self {
Self::with_cause(Message(cause.to_string()))
}

/// Should not be called with `libtest_lexarg::RunIgnored::Yes`
pub fn ignore() -> Self {
Self {
status: notify::MessageKind::Ignored,
cause: None,
}
}

/// Should not be called with `libtest_lexarg::RunIgnored::Yes`
pub fn ignore_for(reason: String) -> Self {
Self {
status: notify::MessageKind::Ignored,
cause: Some(Box::new(Message(reason))),
}
}

pub(crate) fn status(&self) -> notify::MessageKind {
self.status
}

pub(crate) fn cause(&self) -> Option<&(dyn std::error::Error + Send + Sync)> {
self.cause.as_ref().map(|b| b.as_ref())
}
}

impl<E> From<E> for RunError
where
E: std::error::Error + Send + Sync + 'static,
{
fn from(error: E) -> Self {
Self::with_cause(error)
}
}

#[derive(Debug)]
struct Message(String);

impl std::fmt::Display for Message {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(formatter)
}
}

impl std::error::Error for Message {}

pub trait IntoRunResult {
fn into_run_result(self) -> RunResult;
}

impl IntoRunResult for () {
fn into_run_result(self) -> RunResult {
Ok(())
}
}

impl IntoRunResult for RunResult {
fn into_run_result(self) -> RunResult {
self
}
}

impl<E> IntoRunResult for Result<(), E>
where
E: std::error::Error + Send + Sync + 'static,
{
fn into_run_result(self) -> RunResult {
self.map_err(RunError::with_cause)
}
}
2 changes: 2 additions & 0 deletions crates/libtest2-harness/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@

mod case;
mod context;
mod error;
mod harness;
mod notify;

pub mod cli;

pub use case::*;
pub use context::*;
pub use error::*;
pub use harness::*;
pub use notify::RunMode;

Expand Down
9 changes: 9 additions & 0 deletions crates/libtest2-mimic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,15 @@ impl RunError {
}
}

impl<E> From<E> for RunError
where
E: std::error::Error + Send + Sync + 'static,
{
fn from(error: E) -> Self {
Self::with_cause(error)
}
}

pub struct RunContext<'t> {
inner: &'t libtest2_harness::TestContext,
}
Expand Down
Loading