use std::collections::BTreeSet;
use std::sync::{Arc, Mutex, PoisonError};
use crate::host::files::LuaModule;
use crate::host::read::ReadSnapshot;
use crate::types::name::ToolName;
use crate::types::op::{ApiFunction, OpError, PlanInput, Proposals};
use crate::types::plan::Reads;
use crate::types::report::{Diagnostic, Diagnostics};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum Phase {
Load,
Values,
Plan,
}
#[derive(Debug)]
pub(super) struct Loan {
pub(super) input: PlanInput,
pub(super) proposals: Proposals,
pub(super) snapshot: ReadSnapshot,
pub(super) reads: Reads,
}
#[derive(Debug)]
pub(super) struct RunState {
tool: ToolName,
phase: Phase,
sticky: Diagnostics,
loan: Option<Loan>,
loading: Vec<LuaModule>,
loaded: BTreeSet<LuaModule>,
}
impl RunState {
const fn new(tool: ToolName) -> Self {
Self {
tool,
phase: Phase::Load,
sticky: Diagnostics::new(),
loan: None,
loading: Vec::new(),
loaded: BTreeSet::new(),
}
}
pub(super) const fn tool(&self) -> &ToolName {
&self.tool
}
#[cfg(test)]
pub(super) const fn phase(&self) -> Phase {
self.phase
}
pub(super) const fn set_phase(&mut self, phase: Phase) {
self.phase = phase;
}
pub(super) fn record(&mut self, refusal: Diagnostic) {
self.sticky.push(refusal);
}
pub(super) fn take_recorded(&mut self) -> Diagnostics {
std::mem::take(&mut self.sticky)
}
pub(super) fn begin_plan(&mut self, loan: Loan) {
self.loan = Some(loan);
self.phase = Phase::Plan;
}
pub(super) const fn end_plan(&mut self) -> Option<Loan> {
self.phase = Phase::Load;
self.loan.take()
}
pub(super) const fn plan_loan(&mut self, function: ApiFunction) -> Result<&mut Loan, OpError> {
match (self.phase, self.loan.as_mut()) {
(Phase::Plan, Some(loan)) => Ok(loan),
_ => Err(OpError::OutsidePlanHandler { function }),
}
}
pub(super) fn loading(&self) -> &[LuaModule] {
&self.loading
}
pub(super) fn start_loading(&mut self, module: LuaModule) {
self.loading.push(module);
}
pub(super) fn stop_loading(&mut self) {
self.loading.pop();
}
pub(super) fn is_loaded(&self, module: &LuaModule) -> bool {
self.loaded.contains(module)
}
pub(super) fn mark_loaded(&mut self, module: LuaModule) {
self.loaded.insert(module);
}
}
#[derive(Debug, Clone)]
pub(super) struct SharedState(Arc<Mutex<RunState>>);
impl SharedState {
pub(super) fn new(tool: ToolName) -> Self {
Self(Arc::new(Mutex::new(RunState::new(tool))))
}
pub(super) fn with<R>(&self, f: impl FnOnce(&mut RunState) -> R) -> R {
let mut state = self.0.lock().unwrap_or_else(PoisonError::into_inner);
f(&mut state)
}
}
#[cfg(test)]
mod tests {
use std::error::Error;
use super::*;
use crate::host::testing::plan_input;
use crate::types::report::{FieldError, Location};
type TestResult = Result<(), Box<dyn Error>>;
fn shared() -> Result<SharedState, Box<dyn Error>> {
Ok(SharedState::new(ToolName::new("claude")?))
}
#[test]
fn a_run_starts_in_the_load_phase_with_nothing_recorded() -> TestResult {
let state = shared()?;
assert_eq!(state.with(|run| run.phase()), Phase::Load);
assert_eq!(state.with(|run| run.tool().to_string()), "claude");
assert!(state.with(RunState::take_recorded).is_empty());
Ok(())
}
#[test]
fn a_phase_change_is_seen_through_every_clone() -> TestResult {
let state = shared()?;
let other = state.clone();
for phase in [Phase::Values, Phase::Plan, Phase::Load] {
state.with(|run| run.set_phase(phase));
assert_eq!(other.with(|run| run.phase()), phase);
}
Ok(())
}
#[test]
fn recorded_refusals_are_handed_back_once() -> TestResult {
let state = shared()?;
state.with(|run| {
run.record(Diagnostic::new(Location::file("a"), FieldError::Required));
});
assert_eq!(state.with(RunState::take_recorded).len(), 1);
assert!(state.with(RunState::take_recorded).is_empty());
Ok(())
}
fn loan() -> Result<Loan, Box<dyn Error>> {
Ok(Loan {
input: plan_input()?,
proposals: Proposals::new(),
snapshot: ReadSnapshot::default(),
reads: Reads::default(),
})
}
#[test]
fn the_loan_is_refused_outside_the_plan_phase() -> TestResult {
let state = shared()?;
let refused = state.with(|run| {
run.plan_loan(ApiFunction::Skip)
.err()
.map(|error| error.to_string())
});
assert_eq!(
refused.as_deref(),
Some("skip: only allowed inside the plan handler")
);
Ok(())
}
#[test]
fn a_plan_phase_lends_the_loan_and_takes_it_back() -> TestResult {
let state = shared()?;
let lent = loan()?;
state.with(|run| run.begin_plan(lent));
assert_eq!(state.with(|run| run.phase()), Phase::Plan);
assert!(state.with(|run| run.plan_loan(ApiFunction::Write).is_ok()));
assert!(state.with(RunState::end_plan).is_some());
assert_eq!(state.with(|run| run.phase()), Phase::Load);
assert!(state.with(|run| run.plan_loan(ApiFunction::Write).is_err()));
assert!(state.with(RunState::end_plan).is_none());
Ok(())
}
#[test]
fn modules_are_loading_until_stopped_and_loaded_once_marked() -> TestResult {
let state = shared()?;
let util = LuaModule::new("lib.util").ok_or("a module name")?;
let other = LuaModule::new("lib.other").ok_or("a module name")?;
state.with(|run| {
run.start_loading(util.clone());
run.start_loading(other.clone());
});
assert_eq!(
state.with(|run| run.loading().to_vec()),
[util.clone(), other.clone()]
);
state.with(RunState::stop_loading);
assert_eq!(
state.with(|run| run.loading().to_vec()),
std::slice::from_ref(&util)
);
assert!(!state.with(|run| run.is_loaded(&util)));
state.with(|run| run.mark_loaded(util.clone()));
assert!(state.with(|run| run.is_loaded(&util)));
assert!(!state.with(|run| run.is_loaded(&other)));
Ok(())
}
}