Skip to main content

tablo_core/declaration/
action_input.rs

1//! The declaration mistakes of an action's input, and their wording.
2
3use std::fmt;
4
5/// Why an action's input cannot be served:
6/// [`DeclarationErrorKind::ActionInput`](super::DeclarationErrorKind::ActionInput).
7#[derive(Debug, Clone, PartialEq, Eq)]
8#[non_exhaustive]
9pub enum ActionInputFault {
10    /// A field named like a key the action's POST carries itself: `csrf_token`, `confirm` or
11    /// `ids`.
12    ///
13    /// Rename the field.
14    ReservedField(String),
15    /// A file field, whose upload an action's POST does not read.
16    ///
17    /// Take the file in a record form.
18    FileField(String),
19    /// No field, but a parse that refuses an empty submission, so the action could never run.
20    ///
21    /// Declare the fields it parses, or use `()`.
22    RefusesEmpty,
23    /// A dependent choice, whose options no route serves an action's input.
24    ///
25    /// Drop `depends_on`.
26    DependentChoice(String),
27}
28
29/// [`DeclarationErrorKind::ActionInput`](super::DeclarationErrorKind::ActionInput)'s sentence.
30pub(super) fn fmt_action_input(
31    f: &mut fmt::Formatter<'_>,
32    action: &str,
33    fault: &ActionInputFault,
34) -> fmt::Result {
35    match fault {
36        ActionInputFault::ReservedField(field) => write!(
37            f,
38            "action '{action}' declares an input field named '{field}', which its POST \
39             carries itself: rename the field"
40        ),
41        ActionInputFault::FileField(field) => write!(
42            f,
43            "action '{action}' declares a file field '{field}' in its input, which an \
44             action's POST does not upload: take the file in a record form"
45        ),
46        ActionInputFault::RefusesEmpty => write!(
47            f,
48            "action '{action}' declares no input field, but its input refuses an empty \
49             submission: declare the fields it parses, or name `()`"
50        ),
51        ActionInputFault::DependentChoice(field) => write!(
52            f,
53            "action '{action}' declares a dependent choice '{field}' in its input, whose \
54             options only a resource's form refreshes: drop `depends_on`"
55        ),
56    }
57}