Skip to main content

workyard_application/actions/create/
contract.rs

1use serde::Serialize;
2use trine::{Cancellation, NativePath};
3use workyard_core::{OperationKind, Problem, Task};
4
5use crate::contracts::ProblemOutcome;
6pub use crate::contracts::{OperationReport, ParticipantOutcome};
7
8pub(crate) struct ExistingTaskPreview {
9	pub workspace: Option<NativePath>,
10	pub cancellation: Cancellation,
11}
12
13/// Task placement, participant progress and the result of the creation attempt.
14#[derive(Serialize)]
15pub struct CreateOutcome {
16	#[serde(skip)]
17	pub(crate) explicit_workspace: Option<NativePath>,
18	#[serde(skip)]
19	pub(crate) checkout_hint: Option<String>,
20	#[serde(skip)]
21	pub(crate) starting_branch_example: Option<String>,
22	#[serde(skip)]
23	pub(crate) existing_task: Option<ExistingTaskPreview>,
24	/// Failure to record a project root after participant work succeeded.
25	#[serde(skip_serializing_if = "Option::is_none")]
26	pub registration_problem: Option<ProblemOutcome>,
27	/// Invocation history tracked separately from task lifecycle.
28	pub operation: OperationReport,
29	/// None when this invocation could not establish task membership.
30	pub lifecycle: Option<&'static str>,
31	/// Task selected for this operation.
32	pub task: String,
33	/// Directory containing the task workspaces.
34	pub tasks_root: Option<NativePath>,
35	/// Directory holding authoritative Workyard state.
36	pub state_dir: Option<NativePath>,
37	/// Location of the task record when available.
38	pub record: Option<NativePath>,
39	/// Members associated with this task or operation.
40	pub participants: Vec<ParticipantOutcome>,
41	/// Diagnostic describing a failure or an unresolved condition.
42	#[serde(skip_serializing_if = "Option::is_none")]
43	pub problem: Option<ProblemOutcome>,
44}
45
46impl CreateOutcome {
47	/// Build an invocation outcome for a request rejected before effects.
48	pub fn rejected(task: String, problem: Problem) -> Self {
49		Self {
50			checkout_hint: None,
51			starting_branch_example: None,
52			existing_task: None,
53			registration_problem: None,
54			operation: OperationReport::rejected(OperationKind::Create, &problem),
55			explicit_workspace: None,
56			lifecycle: None,
57			task,
58			tasks_root: None,
59			state_dir: None,
60			record: None,
61			participants: Vec::new(),
62			problem: Some(problem.into()),
63		}
64	}
65
66	/// Present the recorded task and its participants.
67	pub fn from_task(task: Task, record: std::path::PathBuf) -> Self {
68		Self {
69			checkout_hint: None,
70			starting_branch_example: None,
71			existing_task: None,
72			registration_problem: None,
73			operation: task.operation.into(),
74			explicit_workspace: None,
75			lifecycle: Some(task.lifecycle.as_str()),
76			task: task.name,
77			tasks_root: Some(NativePath::new(task.root).expect("validated root path")),
78			state_dir: Some(NativePath::new(task.state_dir).expect("validated state path")),
79			record: Some(NativePath::new(record).expect("validated filesystem path")),
80			participants: task.participants.into_iter().map(Into::into).collect(),
81			problem: task.problem.map(Into::into),
82		}
83	}
84}