tablo_core/schema/input.rs
1//! A typed value a form posts under keys no column binds: the [`FormInput`] trait.
2
3use std::{fmt::Debug, hash::Hash};
4
5use topcoat::context::Cx;
6
7use super::{Field, Schema};
8use crate::values::{FieldError, FormFields, FormValues};
9
10/// A typed value a form posts under its own keys, which no column binds: what an
11/// [`Action`](crate::Action) asks for before it runs, what a sign-up reads, one item of a
12/// repeater.
13///
14/// Derive it with [`FormInput`](derive@crate::FormInput) on a struct of form scalars, or name
15/// `()` for a value that asks for nothing.
16///
17/// An action with input asks for it before it runs: its button opens the input form in a dialog
18/// over the page, and its submit runs the action with the parsed value. A refused value renders
19/// the form as a page with the field's error, and writes nothing.
20///
21/// A repeater stores its items in a `#[document]` list, so an item is also a `toasty::Embed`
22/// struct. Each row posts the item's keys under its own prefix, and [`write`](Self::write) fills
23/// a stored item's row on the edit page.
24///
25/// Its [`FormFields`](crate::FormFields) are what
26/// [`Action::validate_input`](crate::Action::validate_input) refuses a value under, so an error
27/// always names a field the form renders: `errors.add(|f| f.reason, ..)`. The derive writes one
28/// variant per field, `ClosingField::Reason` for `Closing`'s `reason`, and the keys `ClosingKeys`;
29/// an input no hook refuses by field names [`Infallible`](std::convert::Infallible).
30pub trait FormInput:
31 FormFields<Field: Copy + Eq + Hash + Debug + Send + Sync + 'static> + Sized + Send + Sync + 'static
32{
33 /// The form key `field` posts under.
34 fn key(field: Self::Field) -> &'static str;
35
36 /// The value's controls, each posting its own key. An empty schema asks for nothing: an
37 /// action runs on its button.
38 fn schema() -> Schema;
39
40 /// Parse a submission of [`schema`](Self::schema)'s keys.
41 ///
42 /// # Errors
43 ///
44 /// Every key that failed, each once.
45 fn parse(cx: &Cx, values: &FormValues) -> Result<Self, Vec<FieldError>>;
46
47 /// Write the value's keys as its form posts them: a repeater fills a stored item's row from
48 /// them, and its detail-page column reads them. A password writes nothing, as it never renders
49 /// back into a form. Nothing writes an action's input or a sign-up, so a type used only there
50 /// writes nothing too.
51 fn write(&self, out: &mut FormValues);
52}
53
54/// No input: an action runs on its button.
55impl FormFields for () {
56 type Field = std::convert::Infallible;
57 type Keys = ();
58
59 fn keys() -> Self::Keys {}
60}
61
62impl FormInput for () {
63 fn key(field: Self::Field) -> &'static str {
64 match field {}
65 }
66
67 fn schema() -> Schema {
68 Schema::empty()
69 }
70
71 fn parse(_cx: &Cx, _values: &FormValues) -> Result<Self, Vec<FieldError>> {
72 Ok(())
73 }
74
75 fn write(&self, _out: &mut FormValues) {}
76}
77
78/// Renders `field`'s control required: the derive marks each field with no blank answer.
79#[doc(hidden)]
80pub fn required_input(field: impl Into<Field>) -> Field {
81 let mut field = field.into();
82 field.set_required(true);
83 field
84}