workyard_application/observation/contract/
participant.rs1use crate::contracts::ProblemOutcome;
2use serde::Serialize;
3use trine::NativePath;
4use workyard_core::ParticipantInspection;
5
6#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
8#[serde(rename_all = "snake_case")]
9pub enum ParticipantState {
10 Clean,
12 Modified,
14 Conflicted,
16 Missing,
18 Unavailable,
20}
21
22impl ParticipantState {
23 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#[derive(Serialize)]
37pub struct ParticipantReport {
38 pub name: String,
40 pub backing_repo: Option<NativePath>,
42 #[serde(skip)]
43 pub(crate) repository_source: Option<NativePath>,
44 pub state: ParticipantState,
46 pub recorded_path: NativePath,
48 pub current_path: Option<NativePath>,
50 pub branch: Option<String>,
52 pub dirty: Option<bool>,
54 pub conflicted: Option<bool>,
56 pub operation_in_progress: Option<bool>,
58 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}