Skip to main content

workyard_application/actions/info/
action.rs

1use super::{DiscoverySource, InfoInput, InfoOutcome};
2use crate::{contracts::Progress, execution::blocking_action};
3use trine::{InvocationContext, NativePath};
4use workyard_core::{Control, Problem, TaskLifecycle};
5
6/// Inspect workspace placement and aggregate task state.
7pub async fn info(
8	input: InfoInput,
9	context: InvocationContext<Progress>,
10) -> Result<InfoOutcome, InfoOutcome> {
11	blocking_action(async move {
12		read(input, context).map_err(|problem| InfoOutcome {
13			problem: Some(problem.into()),
14			..Default::default()
15		})
16	})
17	.await
18}
19
20fn read(input: InfoInput, context: InvocationContext<Progress>) -> Result<InfoOutcome, Problem> {
21	let control = Control::new(move || context.cancellation().is_cancelled());
22	control.check()?;
23	let cwd =
24		std::env::current_dir().map_err(|error| Problem::io("Read current directory", error))?;
25	let workspace = input.workspace.as_ref().map(NativePath::as_path);
26	let Some(reader) = crate::workspace::reader(&cwd, workspace)? else {
27		return Ok(InfoOutcome {
28			guidance: Some(crate::guidance::workspace::missing_state(workspace)),
29			..Default::default()
30		});
31	};
32
33	let workspace_root = reader
34		.state()
35		.parent()
36		.expect("workspace marker has a parent");
37	let mut result = InfoOutcome {
38		workspace: Some(NativePath::new(workspace_root).expect("workspace path")),
39		tasks_root: Some(NativePath::new(reader.root()).expect("root path")),
40		state_dir: Some(NativePath::new(reader.state()).expect("state path")),
41		discovery: Some(if workspace.is_some() {
42			DiscoverySource::Explicit
43		} else {
44			DiscoverySource::Ancestor
45		}),
46		..Default::default()
47	};
48	for snapshot in reader.tasks(None)? {
49		control.check()?;
50		if snapshot.busy {
51			result.busy_tasks += 1;
52		}
53		if !snapshot.problems.is_empty() || snapshot.task.is_none() || snapshot.pending.is_some() {
54			result.unresolved_records += 1;
55		}
56		match snapshot.task.as_ref().map(|task| task.lifecycle) {
57			Some(TaskLifecycle::Active) => result.active_tasks += 1,
58			Some(TaskLifecycle::Retired) => result.retired_tasks += 1,
59			None => {}
60		}
61	}
62	Ok(result)
63}