1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*!
This crates provides a state machine implementation that emits states as a `std::futures::Stream`,
groups sources of external state into a single `Context`, and handles automatic conversion between states
(both forwards and backwards) through the `State` trait.
*/
#![deny(missing_docs)]
use async_trait::async_trait;
use futures::{stream, Stream};
use std::{fmt, sync::Arc};

/// Streamlines represent the streams of states configured for a particular Context, Error type,
/// and `State`-implementing type
#[derive(Debug)]
pub struct Streamline<C, E, S>
where
    S: State<Context = C, Error = E>,
{
    context: Option<C>,
    current: Progress<S, E, C>,
}

impl<C, E, S> Streamline<C, E, S>
where
    S: State<Context = C, Error = E>,
{
    /// Create a `Streamline` from an initial state
    pub fn build(state: S) -> Self {
        Self {
            context: None,
            current: Progress::from(state),
        }
    }

    /// Add an (optional) context to an existing `Streamline`
    pub fn context(mut self, context: C) -> Self {
        self.context = Some(context);

        self
    }

    /// Generate a Stream of states, consuming the `Streamline`
    pub fn run(self) -> impl Stream<Item = Progress<S, E, C>> {
        stream::unfold(Some(self), Self::reduce)
    }

    async fn reduce(state_machine: Option<Self>) -> Option<(Progress<S, E, C>, Option<Self>)> {
        if let Some(mut state_machine) = state_machine {
            let context = state_machine.context.as_ref();
            let next_state = match &state_machine.current {
                Progress::Ok(inner) => match inner.next(context).await {
                    Ok(None) => None,
                    Ok(Some(next)) => Some(Progress::Ok(next)),
                    Err(source) => Some(Progress::Revert(RevertProgress::Reverting {
                        step: inner.clone(),
                        source: Arc::new(source),
                    })),
                },
                Progress::Revert(RevertProgress::Reverting { step, source }) => {
                    match step.revert(context).await {
                        Ok(None) => Some(Progress::Revert(RevertProgress::Reverted {
                            source: source.clone(),
                        })),
                        Ok(Some(next)) => Some(Progress::Revert(RevertProgress::Reverting {
                            step: next,
                            source: source.clone(),
                        })),
                        Err(error) => Some(Progress::Revert(RevertProgress::Failure {
                            source: source.clone(),
                            error,
                        })),
                    }
                }
                _ => None,
            };

            if let Some(next_state) = next_state {
                let current = std::mem::replace(&mut state_machine.current, next_state);

                Some((current, Some(state_machine)))
            } else {
                let current = std::mem::take(&mut state_machine.current);

                Some((current, None))
            }
        } else {
            None
        }
    }
}

/// The `State` trait defines the way that a `Streamline` progresses to (or from) the next state.
#[async_trait(?Send)]
pub trait State: Clone + fmt::Debug + Default + PartialEq {
    /// Global state shared between all `Streamline` states.
    type Context;
    /// The Error shared between all states progressions.
    type Error;

    /// Derives the next state when progressing through a `Streamline` that has not encountered any
    /// errors. There are no limits to how many times a state can be visited, but all mappings
    /// between user states must be explicit. If `Err(Self::Error)` is returned from this method,
    /// the reversion process is triggered. If `Ok(None)` is returned, the `Streamline` ends. If
    /// `Ok(Some(Self))` is returned, the stream continues to the next iteration of `next`
    async fn next(&self, context: Option<&Self::Context>) -> Result<Option<Self>, Self::Error>;

    /// Handles the mapping between a state and its previous state in the case of reversion on
    /// `Err` from `next()`. By default, `revert` simply ends the `Streamline`
    async fn revert(&self, _context: Option<&Self::Context>) -> Result<Option<Self>, Self::Error> {
        Ok(None)
    }
}

/// An internal state machine that represents the process of reverting previous progress.
#[derive(Debug, PartialEq)]
pub enum RevertProgress<S, E, C>
where
    S: State<Context = C, Error = E>,
{
    /// An in-flight `State` reversion
    Reverting {
        /// the state variant in the process of being reverted
        step: S,
        /// the original error that triggered the reversion process
        source: Arc<E>,
    },
    /// The final state of a successful reversion
    Reverted {
        /// the original error that triggered the reversion process
        source: Arc<E>,
    },
    /// The final state of a failed reversion
    Failure {
        /// the original error that triggered the reversion process
        source: Arc<E>,
        /// the error that caused the reversion process to fail
        error: E,
    },
}

/// The state emitted by a `Streamline`
#[derive(Debug, PartialEq)]
pub enum Progress<S, E, C>
where
    S: State<Context = C, Error = E>,
{
    /// All user-provided states run as part of `Progress::Ok` until they trigger a reversion
    Ok(S),
    /// Once a reversion has been triggered, `Progress` tracks the state of the reversion through
    /// a `RevertProgress` `enum`
    Revert(RevertProgress<S, E, C>),
}

impl<S, E, C> Default for Progress<S, E, C>
where
    S: State<Context = C, Error = E>,
{
    fn default() -> Self {
        Self::Ok(S::default())
    }
}

impl<S, E, C> From<S> for Progress<S, E, C>
where
    S: State<Context = C, Error = E>,
{
    fn from(state: S) -> Self {
        Self::Ok(state)
    }
}