pub mod command;
pub mod context;
pub mod error;
pub mod result;
mod state_or_command;
pub mod types;
#[cfg(test)]
mod tests;
pub use command::*;
pub use context::*;
pub use error::*;
pub use result::*;
pub use state_or_command::*;
pub use types::*;
pub async fn interrupt(
value: impl Into<serde_json::Value>,
) -> Result<serde_json::Value, InterruptError> {
use context::INTERRUPT_CONTEXT;
let value = value.into();
let resume_value = INTERRUPT_CONTEXT.with(|ctx| {
let ctx = ctx.borrow();
if let Some(ref c) = *ctx {
if c.current_index < c.resume_values.len() {
Some((c.resume_values[c.current_index].clone(), c.current_index))
} else {
None
}
} else {
None
}
});
if let Some((resume, index)) = resume_value {
INTERRUPT_CONTEXT.with(|ctx| {
let mut ctx = ctx.borrow_mut();
if let Some(ref mut c) = *ctx {
c.current_index = index + 1;
}
});
Ok(resume)
} else {
INTERRUPT_CONTEXT.with(|ctx| {
let mut ctx = ctx.borrow_mut();
if let Some(ref mut c) = *ctx {
c.interrupt_value = Some(value.clone());
} else {
*ctx = Some(InterruptContext {
interrupt_value: Some(value.clone()),
resume_values: Vec::new(),
current_index: 0,
});
}
});
Err(InterruptError::new(value))
}
}