workyard_application/contracts/
problem.rs1use 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#[derive(Serialize)]
19pub struct ProblemOutcome {
20 #[serde(skip_serializing_if = "Option::is_none")]
22 pub worktree_locations: Option<Box<WorktreeLocations>>,
23 pub code: String,
25 pub message: String,
27 pub remedy: String,
29 #[serde(skip_serializing_if = "Vec::is_empty")]
31 pub candidates: Vec<NativePath>,
32 #[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}