diff --git a/crates/thing-core/src/command/mod.rs b/crates/thing-core/src/command/mod.rs index ef664a8..a1bcdb4 100644 --- a/crates/thing-core/src/command/mod.rs +++ b/crates/thing-core/src/command/mod.rs @@ -277,14 +277,7 @@ impl Commands { Ok(prepared) => prepared, Err(diagnostic) => return vec![diagnostic], }; - match &decl.confirm { - Some(text) => { - self.confirming = - Some(Confirming { surface, node: act.node, command: decl.id.clone(), text: text.clone(), prepared }); - } - None => self.mint(prepared, now, ids, port, ledger), - } - Vec::new() + self.begin(prepared, act.node, decl.confirm.as_ref(), now, ids, port, ledger) } /// Answer the command waiting on confirmation. Accepting mints with the captures taken at the @@ -322,10 +315,7 @@ impl Commands { let scope = tree.locate_source(&resume.source, &resume.items).map(|located| located.prefixes).unwrap_or_default(); let origin = Origin { issuing: None, source: resume.source.clone(), scope, items: resume.items.clone() }; match self.prepare(tree, drafts, decl, origin) { - Ok(prepared) => { - self.mint(prepared, now, ids, port, ledger); - Vec::new() - } + Ok(prepared) => self.begin(prepared, resume.source.clone(), decl.confirm.as_ref(), now, ids, port, ledger), Err(diagnostic) => vec![diagnostic], } } @@ -393,6 +383,30 @@ impl Commands { }) } + /// Where every way of acting on a command meets its confirmation: a command that asks waits for the + /// user's yes, holding the captures it was prepared with; any other mints. One command waits at a time. + #[allow(clippy::too_many_arguments)] + fn begin( + &mut self, + prepared: Prepared, + node: NodeId, + confirm: Option<&String>, + now: Timestamp, + ids: &mut dyn IdSource, + port: &mut dyn StatePort, + ledger: &mut Ledger, + ) -> Vec { + let Some(text) = confirm else { + self.mint(prepared, now, ids, port, ledger); + return Vec::new(); + }; + if self.confirming.is_some() { + return vec![refused(&prepared.surface, &node, "another command is waiting for confirmation")]; + } + self.confirming = Some(Confirming { surface: prepared.surface.clone(), node, command: prepared.command.clone(), text: text.clone(), prepared }); + Vec::new() + } + /// The one mint: draw one id, hold the interest in its results before the host can send, and /// queue the invocation. fn mint(&mut self, prepared: Prepared, now: Timestamp, ids: &mut dyn IdSource, port: &mut dyn StatePort, ledger: &mut Ledger) { diff --git a/crates/thing-core/tests/command.rs b/crates/thing-core/tests/command.rs index aa7b698..72955ad 100644 --- a/crates/thing-core/tests/command.rs +++ b/crates/thing-core/tests/command.rs @@ -1266,3 +1266,25 @@ fn a_select_change_naming_no_declared_option_is_refused() { assert_eq!(rig.draft(&nid("pick")), Some(choice_value("a"))); } +// ----- a resume is an activation of a declared command ----- + +#[test] +fn a_resumed_command_that_asks_for_confirmation_waits_for_it() { + let mut document = go_surface(); + document.commands[1] = decl("after", &[], Some("really?")); + let mut rig = Rig::new(MemState::new(), &document); + let mut ids = SeqIds::new(); + let resume = Resume { surface_key: rig.surface.clone(), command: CommandId::new("after"), source: nid("after"), items: vec![] }; + let tree = rig.binder.tree(&rig.surface.clone()).unwrap(); + let said = rig.commands.resume(&resume, tree, &rig.drafts, PINNED_NOW, &mut ids, &mut rig.state, &mut rig.ledger); + assert!(said.is_empty()); + assert!(rig.commands.take_outbound().is_empty()); + assert_eq!(rig.commands.confirming().map(|waiting| waiting.text.as_str()), Some("really?")); + // One command waits at a time: a second resume that asks is refused, and the first still waits. + let tree = rig.binder.tree(&rig.surface.clone()).unwrap(); + let blocked = rig.commands.resume(&resume, tree, &rig.drafts, at(1), &mut ids, &mut rig.state, &mut rig.ledger); + assert_eq!(codes(&blocked), vec![Code::ActivationRefused]); + rig.commands.confirm(true, at(1), &mut ids, &mut rig.state, &mut rig.ledger); + assert_eq!(rig.commands.take_outbound()[0].command, CommandId::new("after")); +} +