Thing 2.0 cut 9b-0: Code::ActivationBlocked splits user-state refusals from caller bugs #29
4 changed files with 124 additions and 33 deletions
|
|
@ -178,11 +178,19 @@ fn fault(surface: &str, node: Option<&NodeId>, code: Code, detail: impl Into<Str
|
|||
Diagnostic { code, surface: surface.to_string(), node: node.cloned(), detail: detail.into() }
|
||||
}
|
||||
|
||||
/// An activation core did not act on; nothing was minted and no draft was written.
|
||||
/// An activation core did not act on because the caller asked for something invalid; nothing was
|
||||
/// minted and no draft was written. A host treats it as a bug in itself.
|
||||
fn refused(surface: &str, node: &NodeId, detail: &str) -> Diagnostic {
|
||||
fault(surface, Some(node), Code::ActivationRefused, detail)
|
||||
}
|
||||
|
||||
/// An otherwise valid activation a user state stopped (a command waits for confirmation, or the control
|
||||
/// is disabled or waits for its result); nothing was minted and no draft was written. A host may show
|
||||
/// or drop it and never treats it as a bug.
|
||||
fn blocked(surface: &str, node: &NodeId, detail: &str) -> Diagnostic {
|
||||
fault(surface, Some(node), Code::ActivationBlocked, detail)
|
||||
}
|
||||
|
||||
/// A v4 UUID from 16 bytes: the version and variant bits are core's, so a minted id is always
|
||||
/// UUID-shaped and dot-free (a result key splits at its last dot).
|
||||
fn uuid(ids: &mut dyn IdSource) -> String {
|
||||
|
|
@ -236,9 +244,10 @@ impl Commands {
|
|||
|
||||
/// The one commit primitive: the user acted on `act.node` of `tree`. Every path calls it. It
|
||||
/// mints one invocation, or nothing. A refusal (a node that is not a command source for this kind, a
|
||||
/// node nobody laid, a change the input does not take, a command waiting on confirmation) answers with
|
||||
/// a `Code::ActivationRefused` diagnostic. A disabled or busy button is not a refusal: the user sees
|
||||
/// it, so it stays silent.
|
||||
/// node nobody laid, a change the input does not take) answers with a `Code::ActivationRefused`
|
||||
/// diagnostic, whatever the user is doing; a well-formed activation a user state stops (a command
|
||||
/// waiting on confirmation, a change to a busy control) answers with `Code::ActivationBlocked`. A press
|
||||
/// on a disabled or busy button is neither: the user sees it, so it stays silent.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn activate(
|
||||
&mut self,
|
||||
|
|
@ -251,9 +260,6 @@ impl Commands {
|
|||
ledger: &mut Ledger,
|
||||
) -> Vec<Diagnostic> {
|
||||
let surface = tree.resolved().document().record_key();
|
||||
if let Some(waiting) = self.confirming_refusal(&surface, &act.node) {
|
||||
return vec![waiting];
|
||||
}
|
||||
let Some(located) = tree.locate(&act.node) else {
|
||||
return vec![refused(&surface, &act.node, "the node is not in the surface")];
|
||||
};
|
||||
|
|
@ -261,15 +267,21 @@ impl Commands {
|
|||
let Some(CommandRef::Id(command)) = command_of(located.node, &act.kind) else {
|
||||
return vec![refused(&surface, &act.node, "the node fires no command for this activation")];
|
||||
};
|
||||
if let ActivationKind::Change(value) = &act.kind {
|
||||
// A control that takes no change holds no value the user did not send: the gate is before the draft.
|
||||
if let Some(held) = self.refuses_change(tree, &act.node) {
|
||||
return vec![held];
|
||||
}
|
||||
match capture::change(tree, &act.node, located.node, value) {
|
||||
Ok(taken) => drafts.set(&surface, &act.node, taken),
|
||||
// Caller bugs are told before user states: a malformed activation is a bug whatever the user is doing.
|
||||
let taken = match &act.kind {
|
||||
ActivationKind::Change(value) => match capture::change(tree, &act.node, located.node, value) {
|
||||
Ok(taken) => Some(taken),
|
||||
Err(detail) => return vec![refused(&surface, &act.node, detail)],
|
||||
}
|
||||
},
|
||||
_ => None,
|
||||
};
|
||||
// A control that takes no change holds no value the user did not send: the gate is before the draft.
|
||||
let stopped = if taken.is_some() { self.refuses_change(tree, &act.node) } else { self.confirming_refusal(&surface, &act.node) };
|
||||
if let Some(stopped) = stopped {
|
||||
return vec![stopped];
|
||||
}
|
||||
if let Some(taken) = taken {
|
||||
drafts.set(&surface, &act.node, taken);
|
||||
}
|
||||
if self.holds(tree, &act.node) {
|
||||
return Vec::new();
|
||||
|
|
@ -290,20 +302,20 @@ impl Commands {
|
|||
self.begin(prepared, act.node, decl.confirm.as_ref(), now, ids, port, ledger)
|
||||
}
|
||||
|
||||
/// Why `node` takes no change now, said as a refusal: a command waits for confirmation, or the
|
||||
/// control is disabled, or its own invocation waits for a result. A change that is refused writes
|
||||
/// no draft, so a control that looks disabled never holds a value that was not sent. A press on
|
||||
/// such a control stays silent (`activate`); a change is an edit the user made and is told.
|
||||
/// Why `node` takes no change now, said as `Code::ActivationBlocked`: a command waits for confirmation,
|
||||
/// or its own invocation waits for a result. A change that is blocked writes no draft, so a busy
|
||||
/// control never holds a value that was not sent. A press on a disabled or busy button stays silent
|
||||
/// (`activate`); a change is an edit the user made and is told.
|
||||
pub(crate) fn refuses_change(&self, tree: &BoundTree, node: &NodeId) -> Option<Diagnostic> {
|
||||
let surface = tree.resolved().document().record_key();
|
||||
if let Some(waiting) = self.confirming_refusal(&surface, node) {
|
||||
return Some(waiting);
|
||||
}
|
||||
self.holds(tree, node).then(|| refused(&surface, node, "the control is disabled or waiting for a result"))
|
||||
self.holds(tree, node).then(|| blocked(&surface, node, "the control is waiting for a result"))
|
||||
}
|
||||
|
||||
fn confirming_refusal(&self, surface: &str, node: &NodeId) -> Option<Diagnostic> {
|
||||
self.confirming.is_some().then(|| refused(surface, node, "a command is waiting for confirmation"))
|
||||
self.confirming.is_some().then(|| blocked(surface, node, "a command is waiting for confirmation"))
|
||||
}
|
||||
|
||||
/// Whether `node` is a disabled Button or has an invocation waiting for its result.
|
||||
|
|
@ -433,7 +445,7 @@ impl Commands {
|
|||
return Vec::new();
|
||||
};
|
||||
if self.confirming.is_some() {
|
||||
return vec![refused(&prepared.surface, &node, "another command is waiting for confirmation")];
|
||||
return vec![blocked(&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()
|
||||
|
|
|
|||
|
|
@ -59,10 +59,16 @@ pub enum Code {
|
|||
CaptureUnavailable,
|
||||
/// A command capture naming no node, or a node that holds no input value.
|
||||
CaptureUndeclared,
|
||||
/// An activation core refused to act on: a kind the node does not take, a node nobody laid, a change
|
||||
/// value outside what the input allows, a command blocked by a confirmation, or a resume that can no
|
||||
/// longer run. Nothing was minted; the detail says which.
|
||||
/// An activation core refused to act on because the caller was wrong: a kind the node does not take, a
|
||||
/// node nobody laid, a change value outside what the input allows, no surface shown, or a resume that
|
||||
/// can no longer run. Nothing was minted; the detail says which. A host logs it as a caller bug, as
|
||||
/// it logs the other caller bugs of the activation path: `UndeclaredCommand` and `CaptureUnavailable`.
|
||||
ActivationRefused,
|
||||
/// A well-formed activation a user state stopped: a command waits for confirmation (this one or
|
||||
/// another), or a change reached a control that waits for its own result. Nothing was minted and no
|
||||
/// draft was written; a host may show or drop it, and it is never a bug. A press on a disabled or busy
|
||||
/// button says nothing at all.
|
||||
ActivationBlocked,
|
||||
}
|
||||
|
||||
impl Code {
|
||||
|
|
|
|||
|
|
@ -669,7 +669,7 @@ fn a_toggle_flips_its_draft_and_fires_on_change_once_per_press() {
|
|||
// A second press is a change to a control with an invocation waiting: it is refused and says so, it
|
||||
// fires nothing more, and it leaves the draft that was sent (a busy control holds no unsent value).
|
||||
let said = rig.click(flag);
|
||||
assert_eq!(said.iter().map(|d| (d.code, d.node.as_ref().map(NodeId::as_str))).collect::<Vec<_>>(), vec![(Code::ActivationRefused, Some("flag"))], "{mode:?}");
|
||||
assert_eq!(said.iter().map(|d| (d.code, d.node.as_ref().map(NodeId::as_str))).collect::<Vec<_>>(), vec![(Code::ActivationBlocked, Some("flag"))], "{mode:?}");
|
||||
assert_eq!(rig.minted(), vec![], "{mode:?}");
|
||||
assert_eq!(rig.draft("flag"), Some(InputValue::Bool(BoolValue { value: true })), "{mode:?}");
|
||||
}
|
||||
|
|
@ -750,7 +750,7 @@ fn a_click_while_a_command_waits_for_confirmation_is_a_refusal_the_event_returns
|
|||
assert_eq!(rig.click(inside(rig.rect("a"))), vec![]);
|
||||
assert!(rig.commands.confirming().is_some() && rig.minted().is_empty(), "{mode:?}");
|
||||
let said = rig.click(inside(rig.rect("b")));
|
||||
assert_eq!(said.iter().map(|d| d.code).collect::<Vec<_>>(), vec![Code::ActivationRefused], "{mode:?}");
|
||||
assert_eq!(said.iter().map(|d| d.code).collect::<Vec<_>>(), vec![Code::ActivationBlocked], "{mode:?}");
|
||||
assert_eq!(said[0].node.as_ref().map(NodeId::as_str), Some("b"));
|
||||
}
|
||||
}
|
||||
|
|
@ -779,7 +779,7 @@ fn a_slider_that_waits_for_its_result_takes_no_press_or_drag_and_a_completion_le
|
|||
let sent = rig.minted().remove(0);
|
||||
assert_eq!(rig.draft("level"), value(30.0), "{mode:?}");
|
||||
// The slider waits: a second press and its drag are refused, say so, and write nothing.
|
||||
assert_eq!(refusals(&rig.send(down(along(0.8)))), vec![(Code::ActivationRefused, Some("level"))], "{mode:?}");
|
||||
assert_eq!(refusals(&rig.send(down(along(0.8)))), vec![(Code::ActivationBlocked, Some("level"))], "{mode:?}");
|
||||
assert_eq!(rig.draft("level"), value(30.0), "{mode:?}: the press wrote nothing");
|
||||
rig.send(mv(along(0.9)));
|
||||
assert_eq!(rig.draft("level"), value(30.0), "{mode:?}: a refused press starts no drag");
|
||||
|
|
@ -799,7 +799,7 @@ fn a_toggle_pressed_while_it_waits_is_refused_and_a_completion_leaves_the_provid
|
|||
let flag = inside(rig.rect("flag"));
|
||||
rig.click(flag);
|
||||
let sent = rig.minted().remove(0);
|
||||
assert_eq!(refusals(&rig.click(flag)), vec![(Code::ActivationRefused, Some("flag"))], "{mode:?}");
|
||||
assert_eq!(refusals(&rig.click(flag)), vec![(Code::ActivationBlocked, Some("flag"))], "{mode:?}");
|
||||
let tree = &rig.tree;
|
||||
rig.commands.apply(&result_change(&sent, true), Timestamp(5), &|_| Some(tree), &mut rig.drafts, &mut rig.state, &mut rig.ledger);
|
||||
assert_eq!(rig.draft("flag"), None, "{mode:?}: nothing survives to show a checked box the provider does not hold");
|
||||
|
|
@ -816,7 +816,7 @@ fn a_slider_press_while_a_command_waits_for_confirmation_writes_no_draft() {
|
|||
assert!(rig.commands.confirming().is_some(), "{mode:?}");
|
||||
let track = rig.rect("level");
|
||||
let said = rig.send(down((track.x + 0.5 * track.w, track.y + 0.5)));
|
||||
assert_eq!(refusals(&said), vec![(Code::ActivationRefused, Some("level"))], "{mode:?}");
|
||||
assert_eq!(refusals(&said), vec![(Code::ActivationBlocked, Some("level"))], "{mode:?}");
|
||||
assert_eq!((rig.draft("level"), rig.drafts.is_empty()), (None, true), "{mode:?}");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1205,7 +1205,7 @@ fn a_change_to_a_node_that_waits_is_refused_writes_no_draft_and_the_completion_c
|
|||
let first = rig.commands.take_outbound().pop().expect("minted");
|
||||
// The node waits for its result: this edit is refused and mints nothing, and it is not a draft.
|
||||
let said = rig.act(&nid("t"), ActivationKind::Change(bool_value(false)), at(10), &mut ids);
|
||||
assert_eq!(said.iter().map(|d| d.code).collect::<Vec<_>>(), vec![Code::ActivationRefused]);
|
||||
assert_eq!(said.iter().map(|d| d.code).collect::<Vec<_>>(), vec![Code::ActivationBlocked]);
|
||||
assert!(rig.commands.take_outbound().is_empty());
|
||||
assert_eq!(rig.draft(&nid("t")), Some(bool_value(true)));
|
||||
rig.feed(&completed(&first, 1, true, None), at(20));
|
||||
|
|
@ -1283,7 +1283,7 @@ fn every_refusal_is_a_diagnostic_naming_the_surface_and_node() {
|
|||
// Waiting on a confirmation blocks every other activation, and says so.
|
||||
assert!(rig.press("after", PINNED_NOW, &mut ids).is_empty());
|
||||
let blocked = rig.press("go", at(1), &mut ids);
|
||||
assert_eq!(codes(&blocked), vec![Code::ActivationRefused]);
|
||||
assert_eq!(codes(&blocked), vec![Code::ActivationBlocked]);
|
||||
assert!(rig.commands.take_outbound().is_empty());
|
||||
// A busy button stays silent: it is a state the user sees, not a fault.
|
||||
let mut rig = Rig::new(MemState::new(), &go_surface());
|
||||
|
|
@ -1291,6 +1291,79 @@ fn every_refusal_is_a_diagnostic_naming_the_surface_and_node() {
|
|||
assert!(rig.press("go", at(1), &mut ids).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn each_refusal_site_says_its_code_a_user_state_is_blocked_a_caller_bug_is_refused() {
|
||||
// Caller bugs: a node nobody laid, a kind the node does not take, a change value the input rejects.
|
||||
let mut rig = Rig::new(MemState::new(), &range_surface());
|
||||
let mut ids = SeqIds::new();
|
||||
for (node, kind, site) in [
|
||||
("no-such-node", ActivationKind::Press, "not in the surface"),
|
||||
("vol", ActivationKind::Submit, "no command for this kind"),
|
||||
("vol", ActivationKind::Change(number_value(f64::NAN)), "bad change value"),
|
||||
] {
|
||||
assert_eq!(codes(&rig.act(&nid(node), kind, PINNED_NOW, &mut ids)), vec![Code::ActivationRefused], "{site}");
|
||||
}
|
||||
|
||||
// User states: a control that waits for its own result.
|
||||
let mut rig = Rig::new(MemState::new(), &tg_surface());
|
||||
rig.act(&nid("t"), ActivationKind::Change(bool_value(true)), PINNED_NOW, &mut ids);
|
||||
assert_eq!(codes(&rig.act(&nid("t"), ActivationKind::Change(bool_value(false)), at(1), &mut ids)), vec![Code::ActivationBlocked], "busy control");
|
||||
|
||||
// User states: a command waits for confirmation, and another one that would ask waits behind it.
|
||||
let mut document = go_surface();
|
||||
document.commands[1] = decl("after", &[], Some("sure?"));
|
||||
let mut rig = Rig::new(MemState::new(), &document);
|
||||
assert!(rig.press("after", PINNED_NOW, &mut ids).is_empty());
|
||||
assert_eq!(codes(&rig.press("go", at(1), &mut ids)), vec![Code::ActivationBlocked], "confirmation waiting");
|
||||
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 behind = rig.commands.resume(&resume, tree, &rig.drafts, at(2), &mut ids, &mut rig.state, &mut rig.ledger);
|
||||
assert_eq!(codes(&behind), vec![Code::ActivationBlocked], "another command waiting");
|
||||
|
||||
// Caller bugs at the host: no surface shown, a held resume dropped.
|
||||
let provider = ProviderId::new(PROVIDER);
|
||||
let document = go_surface();
|
||||
let mut state = MemState::new();
|
||||
state.put(&provider, &document);
|
||||
let mut core = core();
|
||||
core.activate(press("go"), PINNED_NOW, Ports { state: &mut state, ids: &mut SeqIds::new() });
|
||||
assert_eq!(codes(&frame_at(&mut core, &mut state, at(1)).diagnostics), vec![Code::ActivationRefused], "no surface shown");
|
||||
core.show(provider, RecordKey::new(document.record_key()));
|
||||
let held = Resume { surface_key: document.record_key(), command: CommandId::new("after"), source: nid("go"), items: vec![] };
|
||||
core.resume(&held, at(2), Ports { state: &mut state, ids: &mut SeqIds::new() });
|
||||
core.show(ProviderId::new(PROVIDER), RecordKey::new("thing:surface:provider.cmd.elsewhere"));
|
||||
assert_eq!(codes(&frame_at(&mut core, &mut state, at(3)).diagnostics), vec![Code::ActivationRefused], "dropped resume");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_caller_bug_is_refused_even_while_a_command_waits_for_confirmation_or_for_a_result() {
|
||||
let bugs = || {
|
||||
[
|
||||
("no-such-node", ActivationKind::Press, "not in the surface"),
|
||||
("go", ActivationKind::Submit, "no command for this kind"),
|
||||
("name", ActivationKind::Change(number_value(1.0)), "bad change value"),
|
||||
]
|
||||
};
|
||||
let mut ids = SeqIds::new();
|
||||
|
||||
// Another command waits for confirmation.
|
||||
let mut document = go_surface();
|
||||
document.commands[1] = decl("after", &[], Some("sure?"));
|
||||
let mut rig = Rig::new(MemState::new(), &document);
|
||||
assert!(rig.press("after", PINNED_NOW, &mut ids).is_empty());
|
||||
for (node, kind, site) in bugs() {
|
||||
assert_eq!(codes(&rig.act(&nid(node), kind, at(1), &mut ids)), vec![Code::ActivationRefused], "confirming: {site}");
|
||||
}
|
||||
assert_eq!(rig.draft(&nid("name")), None);
|
||||
|
||||
// A command waits for its result.
|
||||
let mut rig = Rig::new(MemState::new(), &go_surface());
|
||||
rig.press("go", PINNED_NOW, &mut ids);
|
||||
for (node, kind, site) in bugs() {
|
||||
assert_eq!(codes(&rig.act(&nid(node), kind, at(1), &mut ids)), vec![Code::ActivationRefused], "waiting: {site}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_command_param_outside_a_composition_leaves_no_tree_to_activate() {
|
||||
let mut document = go_surface();
|
||||
|
|
@ -1339,7 +1412,7 @@ fn a_resumed_command_that_asks_for_confirmation_waits_for_it() {
|
|||
// 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]);
|
||||
assert_eq!(codes(&blocked), vec![Code::ActivationBlocked]);
|
||||
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"));
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue