|
| 1 | +pub(crate) use crate::*; |
| 2 | + |
| 3 | +pub type RunResult = Result<(), RunError>; |
| 4 | + |
| 5 | +#[derive(Debug)] |
| 6 | +pub struct RunError { |
| 7 | + status: notify::MessageKind, |
| 8 | + cause: Option<Box<dyn std::error::Error + Send + Sync + 'static>>, |
| 9 | +} |
| 10 | + |
| 11 | +impl RunError { |
| 12 | + pub fn with_cause(cause: impl std::error::Error + Send + Sync + 'static) -> Self { |
| 13 | + Self { |
| 14 | + status: notify::MessageKind::Error, |
| 15 | + cause: Some(Box::new(cause)), |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + pub fn fail(cause: impl std::fmt::Display) -> Self { |
| 20 | + Self::with_cause(Message(cause.to_string())) |
| 21 | + } |
| 22 | + |
| 23 | + /// Should not be called with `libtest_lexarg::RunIgnored::Yes` |
| 24 | + pub fn ignore() -> Self { |
| 25 | + Self { |
| 26 | + status: notify::MessageKind::Ignored, |
| 27 | + cause: None, |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + /// Should not be called with `libtest_lexarg::RunIgnored::Yes` |
| 32 | + pub fn ignore_for(reason: String) -> Self { |
| 33 | + Self { |
| 34 | + status: notify::MessageKind::Ignored, |
| 35 | + cause: Some(Box::new(Message(reason))), |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + pub(crate) fn status(&self) -> notify::MessageKind { |
| 40 | + self.status |
| 41 | + } |
| 42 | + |
| 43 | + pub(crate) fn cause(&self) -> Option<&(dyn std::error::Error + Send + Sync)> { |
| 44 | + self.cause.as_ref().map(|b| b.as_ref()) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +impl<E> From<E> for RunError |
| 49 | +where |
| 50 | + E: std::error::Error + Send + Sync + 'static, |
| 51 | +{ |
| 52 | + fn from(error: E) -> Self { |
| 53 | + Self::with_cause(error) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +#[derive(Debug)] |
| 58 | +struct Message(String); |
| 59 | + |
| 60 | +impl std::fmt::Display for Message { |
| 61 | + fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 62 | + self.0.fmt(formatter) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +impl std::error::Error for Message {} |
| 67 | + |
| 68 | +pub trait IntoRunResult { |
| 69 | + fn into_run_result(self) -> RunResult; |
| 70 | +} |
| 71 | + |
| 72 | +impl IntoRunResult for () { |
| 73 | + fn into_run_result(self) -> RunResult { |
| 74 | + Ok(()) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +impl IntoRunResult for RunResult { |
| 79 | + fn into_run_result(self) -> RunResult { |
| 80 | + self |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +impl<E> IntoRunResult for Result<(), E> |
| 85 | +where |
| 86 | + E: std::error::Error + Send + Sync + 'static, |
| 87 | +{ |
| 88 | + fn into_run_result(self) -> RunResult { |
| 89 | + self.map_err(RunError::with_cause) |
| 90 | + } |
| 91 | +} |
0 commit comments