#[cfg(not(feature = "std"))]
use alloc::string::String;
#[cfg(feature = "std")]
use std::string::String;
use crate::{Escalation, FailureHandling, LogLine, LogLineError, Outcome, Payload, Status, Verb};
#[derive(Default)]
pub struct LogLineBuilder {
who: Option<String>,
did: Option<Verb>,
this: Option<Payload>,
when: Option<u64>,
confirmed_by: Option<String>,
if_ok: Option<Outcome>,
if_doubt: Option<Escalation>,
if_not: Option<FailureHandling>,
}
impl LogLineBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn who(mut self, v: impl Into<String>) -> Self {
self.who = Some(v.into());
self
}
pub fn did(mut self, v: Verb) -> Self {
self.did = Some(v);
self
}
pub fn this(mut self, v: Payload) -> Self {
self.this = Some(v);
self
}
pub fn when(mut self, v: u64) -> Self {
self.when = Some(v);
self
}
pub fn confirmed_by(mut self, v: impl Into<String>) -> Self {
self.confirmed_by = Some(v.into());
self
}
pub fn if_ok(mut self, v: Outcome) -> Self {
self.if_ok = Some(v);
self
}
pub fn if_doubt(mut self, v: Escalation) -> Self {
self.if_doubt = Some(v);
self
}
pub fn if_not(mut self, v: FailureHandling) -> Self {
self.if_not = Some(v);
self
}
pub fn build_draft(self) -> Result<LogLine, LogLineError> {
let line = LogLine {
who: self.who.ok_or(LogLineError::MissingField("who"))?,
did: self.did.ok_or(LogLineError::MissingField("did"))?,
this: self.this.unwrap_or(Payload::None),
when: self.when.ok_or(LogLineError::MissingField("when"))?,
confirmed_by: self.confirmed_by,
if_ok: self.if_ok.ok_or(LogLineError::MissingInvariant("if_ok"))?,
if_doubt: self
.if_doubt
.ok_or(LogLineError::MissingInvariant("if_doubt"))?,
if_not: self
.if_not
.ok_or(LogLineError::MissingInvariant("if_not"))?,
status: Status::Draft,
};
line.verify_invariants()?;
Ok(line)
}
}