workyard_application/observation/
contract.rs1mod participant;
2pub use participant::{ParticipantReport, ParticipantState};
3
4use crate::contracts::ProblemOutcome;
5use serde::Serialize;
6use trine::NativePath;
7use workyard_core::{Task, TaskInspection, TaskSnapshot};
8
9#[derive(Clone, Copy, PartialEq, Eq, Serialize)]
11#[serde(rename_all = "snake_case")]
12pub enum Provenance {
13 Recorded,
15 Inspected,
17}
18
19#[derive(Serialize)]
21pub struct SavedTask {
22 pub backing_repositories: std::collections::BTreeMap<String, NativePath>,
24 #[serde(skip)]
25 pub(crate) repository_sources: std::collections::BTreeMap<String, NativePath>,
26 #[serde(skip)]
27 pub(crate) removed: std::collections::BTreeSet<String>,
28 pub lifecycle: &'static str,
30 pub operation: crate::contracts::OperationReport,
32 pub problem: Option<ProblemOutcome>,
34 pub participants: Vec<crate::contracts::ParticipantOutcome>,
36}
37
38impl From<Task> for SavedTask {
39 fn from(task: Task) -> Self {
40 let removed = task
41 .removal
42 .as_ref()
43 .into_iter()
44 .flat_map(|removal| task.participants.iter().zip(&removal.participants))
45 .filter(|(_, progress)| progress.phase == workyard_core::RemovalPhase::Removed)
46 .map(|(participant, _)| participant.name.clone())
47 .collect();
48 Self {
49 removed,
50 backing_repositories: task
51 .participants
52 .iter()
53 .map(|participant| {
54 (
55 participant.name.clone(),
56 NativePath::new(&participant.common_dir).expect("recorded backing path"),
57 )
58 })
59 .collect(),
60 repository_sources: task
61 .participants
62 .iter()
63 .map(|participant| {
64 (
65 participant.name.clone(),
66 NativePath::new(&participant.source).expect("recorded repository source"),
67 )
68 })
69 .collect(),
70 lifecycle: task.lifecycle.as_str(),
71 operation: task.operation.into(),
72 problem: task.problem.map(Into::into),
73 participants: task.participants.into_iter().map(Into::into).collect(),
74 }
75 }
76}
77
78#[derive(Serialize)]
80pub struct TaskReport {
81 pub name: Option<String>,
83 pub lifecycle: Option<&'static str>,
85 pub condition: super::Condition,
87 pub provenance: super::Provenance,
89 pub record: NativePath,
91 pub recorded: Option<SavedTask>,
93 pub evidence: Option<SavedTask>,
95 pub pending_participant: Option<usize>,
97 pub busy: bool,
99 pub problems: Vec<ProblemOutcome>,
101 pub current: Vec<ParticipantReport>,
103}
104
105impl From<TaskSnapshot> for TaskReport {
106 fn from(snapshot: TaskSnapshot) -> Self {
107 let mut report = Self {
108 lifecycle: snapshot
109 .task
110 .as_ref()
111 .or(snapshot.evidence.as_ref())
112 .map(|task| task.lifecycle.as_str()),
113 condition: super::Condition::Unknown,
114 provenance: super::Provenance::Recorded,
115 name: snapshot.name,
116 record: NativePath::new(snapshot.record).expect("record path"),
117 recorded: snapshot.task.map(Into::into),
118 evidence: snapshot.evidence.map(Into::into),
119 pending_participant: snapshot.pending,
120 busy: snapshot.busy,
121 problems: snapshot.problems.into_iter().map(Into::into).collect(),
122 current: Vec::new(),
123 };
124 report.classify();
125 report
126 }
127}
128
129impl From<TaskInspection> for TaskReport {
130 fn from(inspection: TaskInspection) -> Self {
131 let mut report = Self::from(inspection.snapshot);
132 report.provenance = super::Provenance::Inspected;
133 report.current = inspection
134 .participants
135 .into_iter()
136 .map(Into::into)
137 .collect();
138 report.classify();
139 report
140 }
141}
142
143#[derive(Serialize, Default)]
145pub struct Observation {
146 #[serde(skip)]
147 pub(crate) explicit_workspace: Option<NativePath>,
148 pub state_dir: Option<NativePath>,
150 pub tasks_root: Option<NativePath>,
152 pub tasks: Vec<TaskReport>,
154 pub selected_task: Option<String>,
156 pub guidance: Option<String>,
158 pub problem: Option<ProblemOutcome>,
160}