Skip to main content

workyard_core/
problem.rs

1use std::path::PathBuf;
2
3/// Structured guidance for an ambiguous or conflicting project selection.
4#[derive(Clone, Debug)]
5pub enum ProjectRemedy {
6	/// Select one of the matching project references.
7	Choose(Vec<PathBuf>),
8	/// Supply a unique name for the participant.
9	Rename {
10		/// Backing repository whose participant needs a distinct name.
11		source: PathBuf,
12		/// Proposed replacement participant name.
13		name: String,
14	},
15}
16
17/// Locations to inspect when Git and recorded placement disagree.
18#[derive(Clone, Debug)]
19pub struct WorktreeLocations {
20	/// Location reported by Git registration.
21	pub registered: PathBuf,
22	/// Possible current location requiring verification.
23	pub candidate: PathBuf,
24}
25
26/// Additional facts that a presenter can use without parsing diagnostic prose.
27#[derive(Clone, Debug)]
28pub enum ProblemDetail {
29	/// Captured Git diagnostics, separate from the command invocation.
30	GitDiagnostic(String),
31	/// User-supplied starting point that a repository could not resolve.
32	StartingPoint(String),
33	/// A previously admitted worktree could not be located.
34	MissingWorktree {
35		/// Name by which the task identifies this worktree.
36		participant: String,
37		/// Worktree location currently expected by Workyard.
38		expected: PathBuf,
39		/// Whether the expected path is occupied by an unverified entry.
40		occupied: bool,
41	},
42	/// An initialized workspace is already configured to use a different task directory.
43	TaskDirectoryConflict {
44		/// Workspace containing the configured tasks directory.
45		workspace: PathBuf,
46		/// Configured tasks directory.
47		current: PathBuf,
48	},
49}
50
51/// Classified failure with diagnostic text and optional recovery guidance.
52#[derive(Clone, Debug)]
53pub struct Problem {
54	/// Paths associated with a worktree relationship problem.
55	pub worktree_locations: Option<Box<WorktreeLocations>>,
56	/// Stable machine-readable diagnostic classification.
57	pub code: crate::ProblemCode,
58	/// Human-readable explanation for the caller.
59	pub message: String,
60	/// Suggested action for resolving the problem.
61	pub remedy: String,
62	/// Structured project selection or naming guidance.
63	pub project_remedy: Option<Box<ProjectRemedy>>,
64	/// Repositories affected by this diagnostic, for presentation by native path.
65	pub repositories: Vec<PathBuf>,
66	/// Additional presentation facts available for this failure.
67	pub detail: Option<Box<ProblemDetail>>,
68}
69
70impl Problem {
71	/// Create a classified diagnostic with the supplied message.
72	pub fn new(code: crate::ProblemCode, message: impl Into<String>) -> Self {
73		Self {
74			code,
75			message: message.into(),
76			remedy: String::new(),
77			project_remedy: None,
78			worktree_locations: None,
79			repositories: Vec::new(),
80			detail: None,
81		}
82	}
83
84	/// Attach a suggested next step for the caller.
85	pub fn remedy(mut self, remedy: impl Into<String>) -> Self {
86		self.remedy = remedy.into();
87		self
88	}
89
90	/// Identify the repository associated with a diagnostic.
91	pub fn in_repository(mut self, path: &std::path::Path) -> Self {
92		self.repositories.push(path.into());
93		self
94	}
95
96	/// A later invocation cannot infer process exit from an available Workyard lock.
97	pub(crate) fn unconfirmed_git_exit(context: impl std::fmt::Display) -> Self {
98		let message = format!("Workyard cannot confirm Git finished {context}.");
99		let remedy = "You can inspect files. Leave the worktree in place while checking Git and its hooks. \
100			If the original Workyard process has exited, retrying cannot clear this interruption.";
101
102		Self::new(crate::ProblemCode::ResourceBusy, message).remedy(remedy)
103	}
104
105	/// Build a diagnostic from a filesystem error.
106	pub fn io(context: impl std::fmt::Display, error: std::io::Error) -> Self {
107		Self::new(crate::ProblemCode::IoError, format!("{context}: {error}"))
108	}
109}
110
111impl std::fmt::Display for Problem {
112	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
113		write!(f, "{}: {}", self.code, self.message)
114	}
115}
116impl std::error::Error for Problem {}