tablo_core/resource/action/submit.rs
1//! [`Submits`]: the buttons of an action's input dialog beside the action's own.
2
3use std::marker::PhantomData;
4
5use crate::{Text, schema::input::FormInput};
6
7/// The buttons of an action's input dialog and input page beside the action's own, each running
8/// the same action with one checkbox of the input checked.
9///
10/// [`Action::submits`](crate::Action::submits) and
11/// [`HeaderAction::submits`](crate::HeaderAction::submits) add them, each over a `bool` field of
12/// the input picked off its [`Keys`](crate::FormFields::Keys):
13///
14/// ```rust
15/// # use tablo_core::{FormInput, Submits};
16/// #[derive(FormInput)]
17/// struct Shipping {
18/// carrier: String,
19/// notify: bool,
20/// }
21///
22/// fn submits(submits: Submits<Shipping>) -> Submits<Shipping> {
23/// submits.add("Ship and notify", |f| f.notify)
24/// }
25/// ```
26///
27/// The dialog renders no control for a checkbox a submit picks: the action's own button posts it
28/// unchecked, `false`, and the submit posts it checked, `true`, so [`run`](crate::Action::run)
29/// reads which button ran it off the input. The buttons follow the action's own in the order they
30/// are added, so Enter in a field still runs the action's own. The POST goes through the action's
31/// route, its policy, `can_run` and input checks, whichever button sent it.
32///
33/// Mounting refuses a submit picking a field that does not render a checkbox.
34pub struct Submits<I> {
35 specs: Vec<SubmitSpec>,
36 input: PhantomData<fn() -> I>,
37}
38
39impl<I: FormInput> Submits<I> {
40 /// No button beside the action's own.
41 pub fn new() -> Self {
42 Self {
43 specs: Vec::new(),
44 input: PhantomData,
45 }
46 }
47
48 /// Adds a button labelled `label` that checks the field `pick` picks off the input's keys.
49 #[must_use]
50 pub fn add(mut self, label: impl Into<Text>, pick: impl FnOnce(I::Keys) -> I::Field) -> Self {
51 self.specs.push(SubmitSpec {
52 label: label.into(),
53 key: I::key(pick(I::keys())),
54 });
55 self
56 }
57
58 /// The buttons past the input's type.
59 pub(crate) fn into_specs(self) -> Vec<SubmitSpec> {
60 self.specs
61 }
62}
63
64impl<I: FormInput> Default for Submits<I> {
65 fn default() -> Self {
66 Self::new()
67 }
68}
69
70/// One of [`Submits`]' buttons past its input's type: its label and the key it posts `true`
71/// under.
72#[derive(Clone)]
73pub(crate) struct SubmitSpec {
74 pub(crate) label: Text,
75 pub(crate) key: &'static str,
76}