Skip to main content

workyard_core/
create.rs

1mod execution;
2mod preflight;
3
4use execution::Creation;
5
6use crate::{Control, CreateRequest, Git, Observer, OperationOutcome, Problem, State};
7use preflight::{preflight, validate_plans};
8
9/// Establish the task and its participant worktrees.
10pub async fn create(
11	request: CreateRequest,
12	runner: &impl Git,
13	control: &Control,
14	context: &impl Observer,
15	store: &impl State,
16) -> Result<crate::CreateResult, Problem> {
17	control.check()?;
18	let guard = store.admit(&request.task)?;
19	let (mut task, repositories) = preflight(&request, runner, control).await?;
20	store.ensure_new(&guard, &request.task)?;
21	task.generation = store.next_generation(&guard)?;
22
23	let record_path = store.record_path(&guard);
24	let destination = request.tasks_root.join(&request.task);
25	store.validate_destination(&destination)?;
26	for participant in &task.participants {
27		store.validate_destination(&participant.destination)?;
28	}
29	validate_plans(&task, &repositories, runner, control).await?;
30
31	control.check()?;
32	store.begin(&guard, &task)?;
33
34	let creation = Creation {
35		runner,
36		control,
37		observer: context,
38		store,
39		guard: &guard,
40	};
41	let operation = creation.run(&mut task, &repositories).await;
42
43	if let Err(mut problem) = operation {
44		task.operation.outcome = if problem.code == crate::ProblemCode::Cancelled {
45			OperationOutcome::Cancelled
46		} else {
47			OperationOutcome::Failed
48		};
49		problem.remedy.push_str(&format!(
50			" Inspect {record_path:?} and Git worktree/status output for each listed path. \
51			Successful work is preserved; do not rerun create on this task."
52		));
53		task.problem = Some(problem);
54	} else {
55		task.operation.outcome = OperationOutcome::Succeeded;
56	}
57
58	let mut invocation = task.operation;
59	let mut invocation_problem = task.problem.clone();
60	task.preserve_settled_cancellation();
61
62	if let Err(problem) = store.save(&guard, &task) {
63		task.operation.outcome = OperationOutcome::Failed;
64		task.problem = Some(problem.clone());
65		invocation.outcome = OperationOutcome::Failed;
66		invocation_problem = Some(problem);
67	}
68
69	Ok(crate::CreateResult {
70		operation: invocation,
71		task,
72		record: record_path,
73		problem: invocation_problem,
74	})
75}