use crate::store::JournalOutcome;
use chrono::{DateTime, Utc};
use std::future::Future;
use tokio_util::sync::CancellationToken;
#[derive(Debug, Clone)]
pub struct JournalEntry {
run_no: u32,
recorded_at: DateTime<Utc>,
outcome: JournalOutcome,
note: Option<String>,
attachment: Option<serde_json::Value>,
}
impl JournalEntry {
pub(crate) fn from_record(record: crate::store::JournalRecord) -> JournalEntry {
JournalEntry {
run_no: record.run_no,
recorded_at: record.recorded_at,
outcome: record.outcome,
note: record.note,
attachment: record.attachment,
}
}
pub fn run_no(&self) -> u32 {
self.run_no
}
pub fn recorded_at(&self) -> DateTime<Utc> {
self.recorded_at
}
pub fn outcome(&self) -> JournalOutcome {
self.outcome
}
pub fn note(&self) -> Option<&str> {
self.note.as_deref()
}
pub fn attachment(&self) -> Option<&serde_json::Value> {
self.attachment.as_ref()
}
pub fn is_failure(&self) -> bool {
self.outcome.is_failure()
}
}
pub struct Context<Carry> {
run_count: u32,
history: Vec<JournalEntry>,
carry: Carry,
attachment: Option<serde_json::Value>,
cancel: CancellationToken,
}
impl<Carry> Context<Carry> {
pub(crate) fn new(
run_count: u32,
history: Vec<JournalEntry>,
carry: Carry,
cancel: CancellationToken,
) -> Context<Carry> {
Context {
run_count,
history,
carry,
attachment: None,
cancel,
}
}
pub fn run_count(&self) -> u32 {
self.run_count
}
pub fn history(&self) -> &[JournalEntry] {
&self.history
}
pub fn carry(&self) -> &Carry {
&self.carry
}
pub fn carry_mut(&mut self) -> &mut Carry {
&mut self.carry
}
pub fn set_attachment(&mut self, value: serde_json::Value) {
self.attachment = Some(value);
}
pub fn is_cancelled(&self) -> bool {
self.cancel.is_cancelled()
}
pub fn cancelled(&self) -> impl Future<Output = ()> + '_ {
self.cancel.cancelled()
}
pub(crate) fn into_parts(self) -> (Carry, Option<serde_json::Value>) {
(self.carry, self.attachment)
}
}