Skip to main content

workyard_application/contracts/
problem.rs

1use serde::Serialize;
2use trine::NativePath;
3use workyard_core::{Problem, ProjectRemedy};
4
5#[derive(Serialize)]
6pub struct SuggestedOverride {
7	pub path: NativePath,
8	pub name: String,
9}
10
11#[derive(Serialize)]
12pub struct WorktreeLocations {
13	pub registered: NativePath,
14	pub candidate: NativePath,
15}
16
17/// Diagnostic representation shared by the public interfaces.
18#[derive(Serialize)]
19pub struct ProblemOutcome {
20	/// Paths associated with a worktree relationship problem.
21	#[serde(skip_serializing_if = "Option::is_none")]
22	pub worktree_locations: Option<Box<WorktreeLocations>>,
23	/// Stable machine-readable diagnostic classification.
24	pub code: String,
25	/// Human-readable explanation for the caller.
26	pub message: String,
27	/// Suggested action for resolving the problem.
28	pub remedy: String,
29	/// Matching project references the caller may select.
30	#[serde(skip_serializing_if = "Vec::is_empty")]
31	pub candidates: Vec<NativePath>,
32	/// An explicit name or selector that can resolve the conflict.
33	#[serde(skip_serializing_if = "Option::is_none")]
34	pub suggested_override: Option<SuggestedOverride>,
35	#[serde(skip)]
36	pub(crate) repositories: Vec<std::path::PathBuf>,
37	#[serde(skip)]
38	pub(crate) detail: Option<Box<workyard_core::ProblemDetail>>,
39	#[serde(skip)]
40	pub(crate) explicit_workspace: Option<NativePath>,
41}
42
43impl From<Problem> for ProblemOutcome {
44	fn from(problem: Problem) -> Self {
45		let mut outcome = Self {
46			worktree_locations: problem.worktree_locations.map(|paths| {
47				Box::new(WorktreeLocations {
48					registered: NativePath::new(paths.registered).expect("Git path"),
49					candidate: NativePath::new(paths.candidate).expect("Git path"),
50				})
51			}),
52			code: problem.code.as_str().into(),
53			message: problem.message,
54			remedy: problem.remedy,
55			candidates: Vec::new(),
56			suggested_override: None,
57			repositories: problem.repositories,
58			detail: problem.detail,
59			explicit_workspace: None,
60		};
61		let Some(detail) = problem.project_remedy else {
62			return outcome;
63		};
64		match *detail {
65			ProjectRemedy::Choose(paths) => {
66				outcome.candidates = paths
67					.into_iter()
68					.map(|p| NativePath::new(p).expect("filesystem path"))
69					.collect();
70			}
71			ProjectRemedy::Rename { source, name } => {
72				let path = NativePath::new(source).expect("filesystem path");
73				if let Some(fragment) = override_arguments(&path, &name) {
74					outcome.remedy =
75						format!("Use this override in the original command: {fragment}");
76				}
77				outcome.suggested_override = Some(SuggestedOverride { path, name });
78			}
79		}
80		outcome
81	}
82}
83
84fn override_arguments(path: &NativePath, name: &str) -> Option<String> {
85	use crate::guidance::commands::argument;
86	let path = argument(path.as_path().to_str()?)?;
87	let name = argument(name)?;
88	Some(format!("--repo {path} --repo-name {name}"))
89}