Skip to main content

workyard_application/observation/contract/
participant.rs

1use crate::contracts::ProblemOutcome;
2use serde::Serialize;
3use trine::NativePath;
4use workyard_core::ParticipantInspection;
5
6/// User-facing assessment of one participant worktree.
7#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
8#[serde(rename_all = "snake_case")]
9pub enum ParticipantState {
10	/// Git reports a clean worktree.
11	Clean,
12	/// The worktree contains local changes.
13	Modified,
14	/// Git reports unresolved conflicts.
15	Conflicted,
16	/// The participant worktree could not be located.
17	Missing,
18	/// Inspection could not establish current Git state.
19	Unavailable,
20}
21
22impl ParticipantState {
23	/// Return the human-readable status label.
24	pub fn label(self) -> &'static str {
25		match self {
26			Self::Clean => "clean",
27			Self::Modified => "modified",
28			Self::Conflicted => "conflicted",
29			Self::Missing => "missing",
30			Self::Unavailable => "unavailable",
31		}
32	}
33}
34
35/// Current Git facts alongside the participant's recorded placement.
36#[derive(Serialize)]
37pub struct ParticipantReport {
38	/// Name used to identify this entry.
39	pub name: String,
40	/// Location of the backing Git repository.
41	pub backing_repo: Option<NativePath>,
42	#[serde(skip)]
43	pub(crate) repository_source: Option<NativePath>,
44	/// Assessment of the current worktree from inspection.
45	pub state: ParticipantState,
46	/// Worktree location stored in task state.
47	pub recorded_path: NativePath,
48	/// Worktree location discovered during inspection.
49	pub current_path: Option<NativePath>,
50	/// Branch associated with the worktree, when known.
51	pub branch: Option<String>,
52	/// Whether the worktree has local changes.
53	pub dirty: Option<bool>,
54	/// Whether Git reports unresolved conflicts.
55	pub conflicted: Option<bool>,
56	/// Whether an unfinished repository operation was observed.
57	pub operation_in_progress: Option<bool>,
58	/// Diagnostic describing a failure or an unresolved condition.
59	pub problem: Option<ProblemOutcome>,
60}
61
62impl From<ParticipantInspection> for ParticipantReport {
63	fn from(inspection: ParticipantInspection) -> Self {
64		let mut participant = Self {
65			name: inspection.participant.name,
66			backing_repo: None,
67			repository_source: None,
68			state: ParticipantState::Unavailable,
69			recorded_path: NativePath::new(inspection.participant.destination)
70				.expect("participant path"),
71			current_path: None,
72			branch: None,
73			dirty: None,
74			conflicted: None,
75			operation_in_progress: None,
76			problem: None,
77		};
78		match inspection.current {
79			Ok(facts) => {
80				participant.backing_repo = facts
81					.common_dir
82					.map(|path| NativePath::new(path).expect("Git common path"));
83				participant.repository_source = facts
84					.repository_source
85					.map(|path| NativePath::new(path).expect("Git repository source"));
86				participant.state = if facts.path.is_none() {
87					ParticipantState::Missing
88				} else if facts.conflicted {
89					ParticipantState::Conflicted
90				} else if facts.dirty {
91					ParticipantState::Modified
92				} else {
93					ParticipantState::Clean
94				};
95				participant.problem = facts.problem.map(Into::into);
96				participant.current_path = facts
97					.path
98					.map(|path| NativePath::new(path).expect("Git path"));
99				participant.branch = facts.branch;
100				participant.dirty = participant.current_path.as_ref().map(|_| facts.dirty);
101				participant.conflicted =
102					participant.current_path.as_ref().map(|_| facts.conflicted);
103				participant.operation_in_progress = participant
104					.current_path
105					.as_ref()
106					.map(|_| facts.operation_in_progress);
107			}
108			Err(problem) => participant.problem = Some(problem.into()),
109		}
110		participant
111	}
112}