workyard-application 0.1.3

Task actions and interface contracts for Workyard
Documentation
use serde::Serialize;
use trine::NativePath;
use workyard_core::{OperationKind, Problem, Task};

pub use crate::contracts::{OperationReport, ParticipantOutcome};
use crate::contracts::{ProblemOutcome, removal};

/// Retirement outcome, observed participants and any outstanding problems.
#[derive(Serialize)]
pub struct RemoveOutcome {
	#[serde(skip)]
	pub(crate) explicit_workspace: Option<NativePath>,
	/// User-facing health classification for the observed task.
	#[serde(skip_serializing_if = "Option::is_none")]
	pub condition: Option<crate::contracts::Condition>,
	/// Whether the invocation entered the mutation phase.
	#[serde(skip_serializing_if = "Option::is_none")]
	pub mutation_started: Option<bool>,
	/// Removal or restoration evidence associated with this task.
	#[serde(flatten)]
	pub removal: removal::RemovalReport,
	/// Invocation history tracked separately from task lifecycle.
	pub operation: OperationReport,
	/// None when this invocation could not establish task membership.
	pub lifecycle: Option<&'static str>,
	/// Task selected for this operation.
	pub task: String,
	/// Directory containing the task workspaces.
	pub tasks_root: Option<NativePath>,
	/// Directory holding authoritative Workyard state.
	pub state_dir: Option<NativePath>,
	/// Location of the task record when available.
	pub record: Option<NativePath>,
	/// Members associated with this task or operation.
	pub participants: Vec<ParticipantOutcome>,
	/// Diagnostic describing a failure or an unresolved condition.
	#[serde(skip_serializing_if = "Option::is_none")]
	pub problem: Option<ProblemOutcome>,
}

impl RemoveOutcome {
	/// Present a removal or restoration result with its participant evidence.
	pub fn from_removal(result: workyard_core::RemovalResult) -> Self {
		let condition = removal::condition(&result);
		let mut outcome = Self::from_task(result.task, result.record);
		outcome.operation = result.operation.into();
		outcome.problem = result.problem.map(Into::into);
		outcome.condition = Some(condition);
		outcome.mutation_started = Some(result.mutation_started);

		if !result.mutation_started && result.inspected {
			removal::mark_present_after_inspection(&mut outcome.participants);
		}

		outcome
	}

	/// Build an invocation outcome for a request rejected before effects.
	pub fn rejected(task: String, problem: Problem) -> Self {
		Self {
			condition: None,
			mutation_started: None,
			removal: Default::default(),
			operation: OperationReport::rejected(OperationKind::Remove, &problem),
			explicit_workspace: None,
			lifecycle: None,
			task,
			tasks_root: None,
			state_dir: None,
			record: None,
			participants: Vec::new(),
			problem: Some(problem.into()),
		}
	}

	/// Present the recorded task and its participants.
	pub fn from_task(task: Task, record: std::path::PathBuf) -> Self {
		let participants = removal::participants(task.participants, task.removal.as_ref());

		Self {
			condition: None,
			mutation_started: None,
			removal: Default::default(),
			operation: task.operation.into(),
			explicit_workspace: None,
			lifecycle: Some(task.lifecycle.as_str()),
			task: task.name,
			tasks_root: Some(NativePath::new(task.root).expect("validated root path")),
			state_dir: Some(NativePath::new(task.state_dir).expect("validated state path")),
			record: Some(NativePath::new(record).expect("validated filesystem path")),
			participants,
			problem: task.problem.map(Into::into),
		}
	}
}