ignite-core 0.6.5

Core types for Ignite, the admin panel for Rocket.rs: resources, fields, forms and runtime resources.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
//! Fields that show only when a condition holds, as Filament's `visible()`, `hidden()`,
//! `visibleOn()`, `hiddenOn()` and `live()`.
//!
//! A condition is a closure the server runs each time it builds or saves a form, given a
//! [`FormContext`]: the operation, the record being edited (none on create), the signed-in
//! user and the form's current values, as a Filament closure is given `$operation`, `$record`,
//! `auth()->user()` and `$get`. A field whose condition fails is hidden: the panel does not
//! render it, does not check it, and does not save it, so a request that sends it anyway
//! cannot change the stored value.
//!
//! A field marked [`live`](FieldConditions::live) re-renders the form when its value changes in
//! the browser: the panel's script sends the form to the server, which runs the conditions on
//! the new values and returns the fields to show.
//!
//! ```ignore
//! RuntimeResource::new("posts", "Posts", "Post", PostData)
//!     .field(kind_field)
//!     .field(company_field)
//!     .live("kind")
//!     .visible("company", |form| form.get("kind") == Some("business"))
//!     .hidden_on("slug", &[Operation::Create])
//!     .visible("featured", |form| form.user().can("posts.feature"));
//! ```

use std::collections::{BTreeSet, HashMap};
use std::fmt;
use std::sync::Arc;

use serde_json::Value;

use crate::form::FormInput;
use crate::policy::ActingUser;
use crate::runtime_resource::{FieldDef, Row};

/// What the form is for, as Filament's `$operation`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Operation {
    /// The new-record form and its save.
    Create,
    /// The edit form of a record and its save.
    Edit,
}

impl Operation {
    /// The operation's name as Filament spells it: `"create"` or `"edit"`.
    pub fn as_str(self) -> &'static str {
        match self {
            Operation::Create => "create",
            Operation::Edit => "edit",
        }
    }
}

/// What a condition is given: the operation, the record, the signed-in user and the form's
/// current values.
#[derive(Debug, Clone, Copy)]
pub struct FormContext<'a> {
    operation: Operation,
    record: Option<&'a Row>,
    user: &'a ActingUser,
    values: &'a FormInput,
}

impl<'a> FormContext<'a> {
    /// The context of a form for `operation`, editing `record` (none on create), for `user`,
    /// whose fields hold `values`. The panel builds it; an app builds one to test a condition.
    pub fn new(
        operation: Operation,
        record: Option<&'a Row>,
        user: &'a ActingUser,
        values: &'a FormInput,
    ) -> Self {
        FormContext {
            operation,
            record,
            user,
            values,
        }
    }

    /// Whether the form creates a record or edits one, as Filament's `$operation`.
    pub fn operation(&self) -> Operation {
        self.operation
    }

    /// The record being edited, as stored, as Filament's `$record`: a runtime resource's
    /// [`Row`], or a static resource's record serialized to JSON. `None` on create.
    pub fn record(&self) -> Option<&'a Row> {
        self.record
    }

    /// The signed-in user, as `auth()->user()` in a Filament closure.
    pub fn user(&self) -> &'a ActingUser {
        self.user
    }

    /// The current value of the field `name`, as Filament's `$get('name')`: the value the
    /// user has typed or chosen, or `None` when it is blank. When the form is first shown it
    /// is the stored value. A field that is itself hidden gives its stored value, never one a
    /// request sent for it. The value is not checked.
    pub fn get(&self, name: &str) -> Option<&'a str> {
        self.values.get(name)
    }

    /// Whether the checkbox `name` is ticked. See [`FormInput::checkbox`].
    pub fn checked(&self, name: &str) -> bool {
        self.values.checkbox(name)
    }
}

/// A condition on a field.
type Rule = Arc<dyn Fn(&FormContext<'_>) -> bool + Send + Sync>;

/// The conditions of a form's fields, by field name, and the fields that are live. Each
/// method takes the field's name and returns the conditions, so they chain.
///
/// As in Filament, a field has one `hidden` condition and one `visible` condition, and shows
/// only when its `hidden` condition fails and its `visible` condition holds. A later
/// [`hidden`](Self::hidden) or [`hidden_on`](Self::hidden_on) for the same field replaces the
/// earlier one, and so does a later [`visible`](Self::visible) or
/// [`visible_on`](Self::visible_on). A field with no condition always shows.
#[derive(Clone, Default)]
pub struct FieldConditions {
    hidden: HashMap<String, Rule>,
    visible: HashMap<String, Rule>,
    live: BTreeSet<String>,
    /// The field each slug field is filled from, by slug field name.
    slugs: HashMap<String, String>,
    /// When each slug field stops following its source, by slug field name.
    slug_locks: HashMap<String, Rule>,
}

impl fmt::Debug for FieldConditions {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let names = |rules: &HashMap<String, Rule>| rules.keys().cloned().collect::<BTreeSet<_>>();
        f.debug_struct("FieldConditions")
            .field("hidden", &names(&self.hidden))
            .field("visible", &names(&self.visible))
            .field("live", &self.live)
            .field("slugs", &self.slugs)
            .field("slug_locks", &names(&self.slug_locks))
            .finish()
    }
}

impl FieldConditions {
    /// No conditions: every field shows, and none is live.
    pub fn new() -> Self {
        FieldConditions::default()
    }

    /// Shows `field` only when `rule` holds, as Filament's `->visible(fn () => ...)`.
    pub fn visible(
        mut self,
        field: impl Into<String>,
        rule: impl Fn(&FormContext<'_>) -> bool + Send + Sync + 'static,
    ) -> Self {
        self.visible.insert(field.into(), Arc::new(rule));
        self
    }

    /// Hides `field` when `rule` holds, as Filament's `->hidden(fn () => ...)`.
    pub fn hidden(
        mut self,
        field: impl Into<String>,
        rule: impl Fn(&FormContext<'_>) -> bool + Send + Sync + 'static,
    ) -> Self {
        self.hidden.insert(field.into(), Arc::new(rule));
        self
    }

    /// Shows `field` only on the forms of `operations`, as Filament's `->visibleOn('edit')`.
    pub fn visible_on(self, field: impl Into<String>, operations: &[Operation]) -> Self {
        let operations = operations.to_vec();
        self.visible(field, move |form| operations.contains(&form.operation()))
    }

    /// Hides `field` on the forms of `operations`, as Filament's `->hiddenOn('edit')`.
    pub fn hidden_on(self, field: impl Into<String>, operations: &[Operation]) -> Self {
        let operations = operations.to_vec();
        self.hidden(field, move |form| operations.contains(&form.operation()))
    }

    /// Makes `field` live, as Filament's `->live()`: when its value changes in the browser,
    /// the form's fields are shown again for the new value, so a field whose condition reads
    /// it with [`FormContext::get`] appears or disappears. The browser sends the change when
    /// the input fires `change`: at once for a select, a checkbox or a radio, and when a text
    /// input loses focus.
    pub fn live(mut self, field: impl Into<String>) -> Self {
        self.live.insert(field.into());
        self
    }

    /// Fills the field `field` with the slug of the field `source` as the user types, until the
    /// user edits `field` by hand, as Filament's documented slug pattern:
    /// `->afterStateUpdated(...)` on the title sets `slug` to `Str::slug($state)` unless the slug
    /// no longer matches `Str::slug($old)`. Filament writes that with a closure on the server;
    /// the panel's script does it in the browser, so it needs no round trip. The slug is the
    /// text in lowercase ASCII, accents removed, with each run of other characters turned into
    /// one `-`, as `Str::slug` gives it. The value is not checked: `validate` still checks it.
    ///
    /// On the edit form, a slug that is not the slug of the stored title counts as edited by
    /// hand, so it is not replaced. [`slug_locked_when`](Self::slug_locked_when) stops it from
    /// following the title at all, such as once the record is published.
    pub fn slug_from(mut self, field: impl Into<String>, source: impl Into<String>) -> Self {
        self.slugs.insert(field.into(), source.into());
        self
    }

    /// Stops the slug field `field` from following its source on the edit form of a record
    /// whose stored slug is not blank, when `rule` holds, such as when the stored record is
    /// published: the slug of a published post's URL then stays as it is when the title
    /// changes. On the create form, and while `rule` fails (such as for a draft), the slug
    /// follows the title as [`slug_from`](Self::slug_from) says. The user can still edit the
    /// slug by hand. The input is marked `data-ignite-slug-locked`, which the panel's script
    /// reads.
    ///
    /// Filament's slug example has no such rule: it stops when the user has edited the slug.
    /// Ignite keeps that, and adds this one.
    ///
    /// ```ignore
    /// conditions
    ///     .slug_from("slug", "title")
    ///     .slug_locked_when("slug", |form| {
    ///         form.record().and_then(|r| r.get("status")).and_then(|s| s.as_str()) == Some("publish")
    ///     })
    /// ```
    pub fn slug_locked_when(
        mut self,
        field: impl Into<String>,
        rule: impl Fn(&FormContext<'_>) -> bool + Send + Sync + 'static,
    ) -> Self {
        self.slug_locks.insert(field.into(), Arc::new(rule));
        self
    }

    /// Whether the slug field `field` no longer follows its source on `form`: the form edits a
    /// record whose stored `field` is not blank, and the rule of
    /// [`slug_locked_when`](Self::slug_locked_when) holds.
    pub fn is_slug_locked(&self, field: &str, form: &FormContext<'_>) -> bool {
        let Some(rule) = self.slug_locks.get(field) else {
            return false;
        };
        let stored = form
            .record()
            .and_then(|record| record.get(field))
            .is_some_and(|value| match value {
                Value::Null => false,
                Value::String(text) => !text.trim().is_empty(),
                _ => true,
            });
        form.operation() == Operation::Edit && stored && rule(form)
    }

    /// The field `field` is filled from by [`slug_from`](Self::slug_from), if any.
    pub fn slug_source(&self, field: &str) -> Option<&str> {
        self.slugs.get(field).map(String::as_str)
    }

    /// Whether `field` is live.
    pub fn is_live(&self, field: &str) -> bool {
        self.live.contains(field)
    }

    /// Whether no field has a condition or is live.
    pub fn is_empty(&self) -> bool {
        self.hidden.is_empty() && self.visible.is_empty() && self.live.is_empty()
    }

    /// Whether `field` shows in `form`: its `hidden` condition, if any, fails and its
    /// `visible` condition, if any, holds.
    pub fn is_shown(&self, field: &str, form: &FormContext<'_>) -> bool {
        let hidden = self.hidden.get(field).is_some_and(|rule| rule(form));
        let visible = self.visible.get(field).is_none_or(|rule| rule(form));
        !hidden && visible
    }

    /// The names of the `fields` that are hidden on the form for `operation`, editing
    /// `record`, for `user`, whose fields hold `values`.
    ///
    /// A condition that reads a hidden field gets the field's stored value, not the one in
    /// `values`, so a request cannot show a field by sending a value for a field it may not
    /// see. The conditions run again until the hidden fields settle.
    pub fn hidden_fields(
        &self,
        fields: &[FieldDef],
        operation: Operation,
        record: Option<&Row>,
        user: &ActingUser,
        values: &FormInput,
    ) -> BTreeSet<String> {
        let mut hidden = BTreeSet::new();
        if self.hidden.is_empty() && self.visible.is_empty() {
            return hidden;
        }
        for _ in 0..=fields.len() {
            let state = stored_when_hidden(values, record, &hidden);
            let form = FormContext::new(operation, record, user, &state);
            let next: BTreeSet<String> = fields
                .iter()
                .filter(|field| !self.is_shown(&field.name, &form))
                .map(|field| field.name.clone())
                .collect();
            if next == hidden {
                break;
            }
            hidden = next;
        }
        hidden
    }
}

/// `values`, with each field of `hidden` holding its stored value in `record` instead, or no
/// value when there is none.
fn stored_when_hidden(
    values: &FormInput,
    record: Option<&Row>,
    hidden: &BTreeSet<String>,
) -> FormInput {
    let mut state = values.without(hidden.iter().map(String::as_str));
    for name in hidden {
        let stored = match record.and_then(|record| record.get(name)) {
            None | Some(Value::Null) | Some(Value::Bool(false)) => continue,
            Some(Value::Bool(true)) => "on".to_string(),
            Some(Value::String(text)) => text.clone(),
            Some(other) => other.to_string(),
        };
        state.insert(name.clone(), stored);
    }
    state
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::runtime_resource::FieldKindDef;

    fn field(name: &str) -> FieldDef {
        FieldDef {
            name: name.into(),
            label: name.into(),
            kind: FieldKindDef::Text,
            required: true,
        }
    }

    fn form(pairs: &[(&str, &str)]) -> FormInput {
        FormInput::new(
            pairs
                .iter()
                .map(|(k, v)| (k.to_string(), v.to_string()))
                .collect(),
        )
    }

    fn user() -> ActingUser {
        ActingUser::new(1, "Ada", "ada@example.test")
    }

    #[test]
    fn a_field_with_no_condition_shows() {
        let conditions = FieldConditions::new();
        let user = user();
        let values = form(&[]);
        let context = FormContext::new(Operation::Create, None, &user, &values);
        assert!(conditions.is_shown("title", &context));
        assert!(conditions.is_empty());
    }

    #[test]
    fn hidden_on_and_visible_on_follow_the_operation() {
        let conditions = FieldConditions::new()
            .hidden_on("slug", &[Operation::Create])
            .visible_on("published_at", &[Operation::Edit]);
        let fields = [field("slug"), field("published_at"), field("title")];
        let user = user();
        let values = form(&[]);
        let on_create = conditions.hidden_fields(&fields, Operation::Create, None, &user, &values);
        assert_eq!(
            on_create,
            BTreeSet::from(["published_at".into(), "slug".into()])
        );
        let record = Row::new();
        let on_edit =
            conditions.hidden_fields(&fields, Operation::Edit, Some(&record), &user, &values);
        assert!(on_edit.is_empty());
    }

    #[test]
    fn a_later_hidden_replaces_the_earlier_one_and_both_slots_must_agree() {
        let conditions = FieldConditions::new()
            .hidden("note", |_| true)
            .hidden_on("note", &[Operation::Create])
            .visible("note", |form| form.user().can("notes.edit"));
        let user = user();
        let editor = ActingUser::new(2, "Bo", "bo@example.test").with_ability("notes.edit");
        let values = form(&[]);
        let record = Row::new();
        let edit = |user| FormContext::new(Operation::Edit, Some(&record), user, &values);
        assert!(!conditions.is_shown("note", &edit(&user)), "not visible");
        assert!(conditions.is_shown("note", &edit(&editor)));
        let create = FormContext::new(Operation::Create, None, &editor, &values);
        assert!(!conditions.is_shown("note", &create), "hidden on create");
    }

    #[test]
    fn a_condition_reads_the_record_and_the_current_values() {
        let conditions = FieldConditions::new()
            .live("kind")
            .visible("company", |form| form.get("kind") == Some("business"))
            .visible("archived_note", |form| {
                form.record()
                    .and_then(|record| record.get("status"))
                    .and_then(Value::as_str)
                    == Some("archived")
            });
        assert!(conditions.is_live("kind"));
        assert!(!conditions.is_live("company"));
        let fields = [field("kind"), field("company"), field("archived_note")];
        let user = user();
        let mut record = Row::new();
        record.insert("status".into(), Value::from("archived"));
        let hidden = conditions.hidden_fields(
            &fields,
            Operation::Edit,
            Some(&record),
            &user,
            &form(&[("kind", "business")]),
        );
        assert!(hidden.is_empty(), "{hidden:?}");
        let hidden = conditions.hidden_fields(
            &fields,
            Operation::Create,
            None,
            &user,
            &form(&[("kind", "person")]),
        );
        assert_eq!(
            hidden,
            BTreeSet::from(["archived_note".into(), "company".into()])
        );
    }

    // `secret` is hidden from this user, and `extra` shows when `secret` is "yes". A request
    // that sends `secret=yes` must not show `extra`: the condition reads the stored value.
    #[test]
    fn a_condition_reads_a_hidden_field_as_stored_not_as_sent() {
        let conditions = FieldConditions::new()
            .visible("secret", |form| form.user().can("secret.edit"))
            .visible("extra", |form| form.get("secret") == Some("yes"));
        let fields = [field("secret"), field("extra")];
        let user = user();
        let sent = form(&[("secret", "yes"), ("extra", "changed")]);

        let mut record = Row::new();
        record.insert("secret".into(), Value::from("no"));
        let hidden =
            conditions.hidden_fields(&fields, Operation::Edit, Some(&record), &user, &sent);
        assert_eq!(hidden, BTreeSet::from(["extra".into(), "secret".into()]));

        record.insert("secret".into(), Value::from("yes"));
        let hidden =
            conditions.hidden_fields(&fields, Operation::Edit, Some(&record), &user, &sent);
        assert_eq!(hidden, BTreeSet::from(["secret".into()]));
    }
}