use crate::{AccessSpec, Context};
pub enum Flow<Out> {
Continue,
Done(Out),
}
#[derive(Debug, thiserror::Error)]
#[error("{source}")]
pub struct StepError {
source: Box<dyn std::error::Error + Send + Sync>,
}
impl StepError {
pub fn new(source: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> Self {
Self {
source: source.into(),
}
}
pub(crate) fn downcast<E: std::error::Error + Send + Sync + 'static>(self) -> Result<E, Self> {
match self.source.downcast::<E>() {
Ok(boxed) => Ok(*boxed),
Err(source) => Err(Self { source }),
}
}
}
#[async_trait::async_trait]
pub trait Step<Out>: Send + Sync {
fn name(&self) -> &str;
fn access(&self) -> AccessSpec;
async fn run(&self, ctx: &mut Context) -> Result<Flow<Out>, StepError>;
}
#[cfg(test)]
mod test {
use std::error::Error;
use std::fmt::Display;
use super::*;
use crate::test_support::block_on;
#[derive(Debug)]
struct DummyError(String);
impl Display for DummyError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.0)
}
}
impl Error for DummyError {}
#[test]
fn new_wraps_source_error_accessible_via_source_downcast() {
let err = StepError::new(DummyError("boom".to_string()));
let source = err.source().expect("source should be present");
let dummy = source
.downcast_ref::<DummyError>()
.expect("source should downcast to DummyError");
assert_eq!(dummy.0, "boom");
}
#[test]
fn display_delegates_to_source_display() {
let err = StepError::new(DummyError("boom".to_string()));
assert_eq!(err.to_string(), "boom");
}
#[test]
fn new_accepts_str_via_into() {
let err = StepError::new("boom");
assert_eq!(err.to_string(), "boom");
}
#[test]
fn new_accepts_string_via_into() {
let err = StepError::new(String::from("boom"));
assert_eq!(err.to_string(), "boom");
}
#[test]
fn implements_std_error_trait_with_some_source() {
let err = StepError::new("boom");
let as_dyn: &dyn Error = &err;
assert!(as_dyn.source().is_some());
}
#[test]
fn new_accepts_empty_string_message() {
let err = StepError::new("");
assert_eq!(err.to_string(), "");
}
struct Marker(i32);
enum Outcome {
Continue,
Done(i32),
Err(&'static str),
InsertMarker(i32),
}
struct TestStep {
name: &'static str,
access: AccessSpec,
outcome: Outcome,
}
#[async_trait::async_trait]
impl Step<i32> for TestStep {
fn name(&self) -> &str {
self.name
}
fn access(&self) -> AccessSpec {
self.access.clone()
}
async fn run(&self, ctx: &mut Context) -> Result<Flow<i32>, StepError> {
match &self.outcome {
Outcome::Continue => Ok(Flow::Continue),
Outcome::Done(v) => Ok(Flow::Done(*v)),
Outcome::Err(msg) => Err(StepError::new(*msg)),
Outcome::InsertMarker(v) => {
ctx.insert(Marker(*v));
Ok(Flow::Continue)
}
}
}
}
#[test]
fn step_name_returns_configured_name() {
let step = TestStep {
name: "load",
access: AccessSpec::new(),
outcome: Outcome::Continue,
};
assert_eq!(step.name(), "load");
}
#[test]
fn step_access_returns_configured_spec() {
let step = TestStep {
name: "load",
access: AccessSpec::new().reads::<Marker>(),
outcome: Outcome::Continue,
};
assert!(step.access().can_read(std::any::TypeId::of::<Marker>()));
}
#[test]
fn step_run_returns_continue_variant() {
let step = TestStep {
name: "s",
access: AccessSpec::new(),
outcome: Outcome::Continue,
};
let mut ctx = Context::new();
let result = block_on(step.run(&mut ctx));
assert!(matches!(result, Ok(Flow::Continue)));
}
#[test]
fn step_run_returns_done_variant_with_value() {
let step = TestStep {
name: "s",
access: AccessSpec::new(),
outcome: Outcome::Done(42),
};
let mut ctx = Context::new();
let result = block_on(step.run(&mut ctx));
assert!(matches!(result, Ok(Flow::Done(42))));
}
#[test]
fn step_run_propagates_err_variant() {
let step = TestStep {
name: "s",
access: AccessSpec::new(),
outcome: Outcome::Err("boom"),
};
let mut ctx = Context::new();
match block_on(step.run(&mut ctx)) {
Err(err) => assert_eq!(err.to_string(), "boom"),
Ok(_) => panic!("expected Err"),
}
}
#[test]
fn step_run_receives_context_and_can_read_write_it() {
let step = TestStep {
name: "s",
access: AccessSpec::new(),
outcome: Outcome::InsertMarker(7),
};
let mut ctx = Context::new();
block_on(step.run(&mut ctx)).unwrap();
assert_eq!(ctx.get::<Marker>().map(|m| m.0), Some(7));
}
#[test]
fn step_trait_object_is_usable_as_dyn_step() {
let step: Box<dyn Step<i32>> = Box::new(TestStep {
name: "boxed",
access: AccessSpec::new(),
outcome: Outcome::Done(1),
});
assert_eq!(step.name(), "boxed");
let mut ctx = Context::new();
let result = block_on(step.run(&mut ctx));
assert!(matches!(result, Ok(Flow::Done(1))));
}
}