Skip to main content

tablo_core/declaration/
related_form.rs

1//! The declaration mistakes of a `has_one` relation's related form, and their wording.
2
3use std::fmt;
4
5/// Why a [`Relation::has_one`](crate::Relation::has_one) cannot render its related resource's
6/// form inside the owner's:
7/// [`DeclarationErrorKind::RelatedForm`](super::DeclarationErrorKind::RelatedForm).
8#[derive(Debug, Clone, PartialEq, Eq)]
9#[non_exhaustive]
10pub enum RelatedFormFault {
11    /// The owner has no form for the related form to render in.
12    ///
13    /// Give the owner a form, or declare a `has_many` relation, which renders a table.
14    NoOwnerForm,
15    /// The related resource has no form.
16    ///
17    /// Give it a form.
18    NoForm,
19    /// The related resource's form binds no control for the foreign key, which the owner's key
20    /// fills.
21    ///
22    /// Bind the foreign key in its form.
23    UnboundForeignKey {
24        /// The foreign key.
25        column: String,
26    },
27    /// The related resource soft-deletes: a trashed child would stay the record's while the form
28    /// created another.
29    ///
30    /// Declare a `has_many` relation, whose table offers the trashed records.
31    SoftDeletes,
32    /// The related form holds a control that posts keys besides its own or fetches from its
33    /// resource's routes: a file field, a repeater, an embedded value, a searchable or dependent
34    /// choice.
35    ///
36    /// Leave the field out of the related resource's form, or declare a `has_many` relation.
37    OwnKeys {
38        /// The field.
39        field: String,
40    },
41    /// An owner form key is the related resource's slug, or starts with the related form's
42    /// prefix, the slug and a dot, under which the related form's controls post.
43    ///
44    /// Rename the field, or give the related resource another slug.
45    ShadowedKey {
46        /// The owner's form key.
47        field: String,
48    },
49}
50
51impl RelatedFormFault {
52    /// The sentence a mount error says.
53    pub(super) fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        match self {
55            Self::NoOwnerForm => f.write_str(
56                "a `has_one` relation renders its related form inside the owner's form, and the \
57                 owner has none: give it a form, or declare a `has_many` relation",
58            ),
59            Self::NoForm => f.write_str(
60                "a `has_one` relation renders the related resource's form, and it has none: \
61                 give it a form",
62            ),
63            Self::UnboundForeignKey { column } => write!(
64                f,
65                "the related resource's form binds no control for `{column}`, which the owner's \
66                 key fills: bind it in that form"
67            ),
68            Self::SoftDeletes => f.write_str(
69                "the related resource soft-deletes, so a trashed child would stay the record's \
70                 while its form created another: declare a `has_many` relation",
71            ),
72            Self::OwnKeys { field } => write!(
73                f,
74                "the related form's field `{field}` posts keys besides its own or fetches from \
75                 its resource's routes, which a form inside another cannot: leave it out of \
76                 the related resource's form, or declare a `has_many` relation"
77            ),
78            Self::ShadowedKey { field } => write!(
79                f,
80                "the owner's form key `{field}` shadows the related form's prefix, the related \
81                 resource's slug and a dot: rename it, or give the related resource another slug"
82            ),
83        }
84    }
85}