renox-admin 1.2.0

An admin panel generated from a Renox app's models: lists, forms, filters, bulk actions and exports.
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
//! What an app declares for each model in the panel: [`AdminResource`],
//! with its [`AdminAction`]s and [`Filter`]s.

use std::fmt;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;

use renox::db::{Model, Query};
use renox::grid::{Column, Grid};
use renox::prelude::*;
use renox::state_machine::{Available, MachineInfo, Outcome, StateMachine, TransitionError};
use serde::Serialize;
use serde::de::DeserializeOwned;

use crate::{Entry, Field, RelationManager};

/// A boxed future that can be sent between threads, as an action returns.
pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;

/// A model in the admin panel (Filament's resources), declared once: the
/// grid's columns, the form's fields, its filters and actions, and who may
/// do what (its [`Policy`]).
///
/// ```
/// use renox::grid::Column;
/// use renox::prelude::*;
/// use renox_admin::{AdminResource, Field};
/// use serde::{Deserialize, Serialize};
///
/// #[derive(Model, Serialize, Default, Clone)]
/// struct Product { id: i64, name: String, price: i64, active: bool }
///
/// impl Policy for Product {
///     fn allows(&self, user: &User, ability: &str) -> bool {
///         // Everyone in the panel looks; only managers change things.
///         matches!(ability, "viewAny" | "view") || user.has_role("manager")
///     }
/// }
///
/// #[derive(Deserialize, Serialize, Validate)]
/// struct ProductForm {
///     #[validate(required, max = 100)]
///     name: String,
///     #[validate(required, min = 0)]
///     price: i64,
///     active: bool,
/// }
///
/// struct Products;
///
/// impl AdminResource for Products {
///     type Model = Product;
///     type Form = ProductForm;
///
///     fn label(&self) -> &str { "Product" }
///     fn plural_label(&self) -> &str { "Products" }
///
///     fn columns(&self) -> Vec<Column> {
///         vec![
///             Column::text("name", "Name").searchable(),
///             Column::money("price", "Price"),
///             Column::bool("active", "Active"),
///         ]
///     }
///
///     fn fields(&self) -> Vec<Field> {
///         vec![
///             Field::text("name", "Name").required(),
///             Field::money("price", "Price").required(),
///             Field::toggle("active", "Active").default_value(true),
///         ]
///     }
///
///     fn fill(&self, product: &mut Product, form: ProductForm) {
///         product.name = form.name;
///         product.price = form.price;
///         product.active = form.active;
///     }
/// }
/// ```
pub trait AdminResource: Send + Sync + 'static {
    /// The model the resource manages. `Default` gives the record a create
    /// page starts from, and the one list-level abilities are asked about.
    type Model: Model + Policy + Serialize + Default + Send + Sync;

    /// The form of the create and edit pages, checked with its own rules
    /// (`Valid<T>`). `Serialize` refills it after [`rules`](Self::rules)
    /// fails (passwords aren't refilled).
    type Form: DeserializeOwned + Validate + Serialize + Send + 'static;

    /// One record's name: "Product".
    fn label(&self) -> &str;

    /// Several records' name, in the navigation and the list's title:
    /// "Products".
    fn plural_label(&self) -> &str;

    /// What a record's view and edit pages call it: "Product #7" by
    /// default; its name reads better (`product.name.clone()`).
    fn record_title(&self, record: &Self::Model) -> String {
        format!("{} #{}", self.label(), record.id())
    }

    /// The resource's part of the address and of the route names
    /// (`/admin/products`, `admin.products.index`): letters, digits, `_`
    /// and `-`. The model's table by default.
    fn slug(&self) -> &str {
        Self::Model::TABLE
    }

    /// A heading the navigation puts the resource under ("Shop").
    fn navigation_group(&self) -> Option<&str> {
        None
    }

    /// The list's columns, left to right (`renox::grid`). Make some
    /// `.searchable()` for the list's search box.
    fn columns(&self) -> Vec<Column>;

    /// The create and edit pages' fields, in order.
    fn fields(&self) -> Vec<Field>;

    /// Copies a valid form into the record, before it is saved (a new
    /// record starts from `Default`).
    fn fill(&self, record: &mut Self::Model, form: Self::Form);

    /// Rules that need the record being edited (`None` while creating),
    /// checked after the form's own, e.g. a unique value that may stay the
    /// record's own: `v.field("sku", &form.sku).unique("products", "sku")`
    /// followed by `.ignore(record.id)` when there is one.
    fn rules(&self, form: &Self::Form, record: Option<&Self::Model>, v: &mut Validator) {
        let _ = (form, record, v);
    }

    /// The details of a record's view page. Without any, the resource has
    /// no view page and its rows open the edit page. By default, an entry
    /// per column ([`Entry::from_column`]).
    fn entries(&self) -> Vec<Entry> {
        self.columns()
            .iter()
            .filter_map(Entry::from_column)
            .collect()
    }

    /// Named filters over the list, shown as tabs above it ("Active",
    /// "Low stock"); the grid's columns filter on their own too.
    fn filters(&self) -> Vec<Filter<Self::Model>> {
        Vec::new()
    }

    /// The resource's own actions on selected rows (and, with
    /// [`AdminAction::row`], on one row).
    fn actions(&self) -> Vec<AdminAction<Self::Model>> {
        Vec::new()
    }

    /// Whether the panel offers a create page and a "New" button. Return
    /// `false` for a resource that is only edited (a settings row, a
    /// record other code creates): the create address answers 404.
    fn creatable(&self) -> bool {
        true
    }

    /// Whether records can be deleted from the panel (the delete buttons,
    /// the bulk delete, the trash and its restore). Return `false` for an
    /// edit-only resource: those addresses answer 404.
    fn deletable(&self) -> bool {
        true
    }

    /// Child rows managed from a record's own pages (Filament's relation
    /// managers): its variants, photos or tags, each a tab of the record
    /// with its own list. See [`RelationManager`].
    fn relations(&self) -> Vec<RelationManager> {
        Vec::new()
    }

    /// Runs after the create or edit page saved a record (Filament's
    /// `afterSave`), with who saved it and, on an edit, the record as it was
    /// ([`SaveContext::previous`]): audit an exact price change, refresh
    /// search keywords. An error answers the request with it, though the
    /// record is saved by then; do the work in a transaction of your own
    /// when it must go in with the record.
    ///
    /// ```
    /// # use renox::prelude::*;
    /// # use renox::grid::Column;
    /// # use renox_admin::{AdminResource, Field, SaveContext};
    /// # #[derive(Model, serde::Serialize, Default)] struct Product { id: i64, name: String, price: i64 }
    /// # impl Policy for Product { fn allows(&self, _: &User, _: &str) -> bool { true } }
    /// # #[derive(serde::Deserialize, serde::Serialize, Validate)] struct ProductForm { #[validate(required)] name: String }
    /// struct Products;
    ///
    /// impl AdminResource for Products {
    /// #   type Model = Product;
    /// #   type Form = ProductForm;
    /// #   fn label(&self) -> &str { "Product" }
    /// #   fn plural_label(&self) -> &str { "Products" }
    /// #   fn columns(&self) -> Vec<Column> { vec![] }
    /// #   fn fields(&self) -> Vec<Field> { vec![] }
    /// #   fn fill(&self, p: &mut Product, f: ProductForm) { p.name = f.name; }
    ///     // ...
    ///     async fn saved(&self, product: &Product, cx: &SaveContext) -> Result {
    ///         let before = cx.previous.as_ref().and_then(|old| old["price"].as_i64());
    ///         if before.is_some_and(|before| before != product.price) {
    ///             // Write the audit entry: who (`cx.user`), which product, from, to.
    ///             let _ = (&cx.state.db, &cx.user);
    ///         }
    ///         Ok(())
    ///     }
    /// }
    /// ```
    fn saved(&self, record: &Self::Model, cx: &SaveContext) -> impl Future<Output = Result> + Send {
        let _ = (record, cx);
        async { Ok(()) }
    }

    /// The model's state machine, for a read-only diagram on the view page:
    /// `Some(renox::state_machine::info_of::<Order>())`. Pair it with
    /// [`available_transitions`](Self::available_transitions) for the buttons
    /// and [`AdminAction::transitions`] for what they run. `None` by default.
    fn state_machine(&self) -> Option<MachineInfo> {
        None
    }

    /// The transitions `user` may run on `record` now, shown as buttons on
    /// its view page (each posts to the action of the same name, see
    /// [`AdminAction::transitions`]). For a state machine model, return
    /// `record.available(user, db).await`. None by default.
    fn available_transitions(
        &self,
        record: &Self::Model,
        user: &AuthUser,
        db: &Db,
    ) -> impl Future<Output = Result<Vec<Available>>> + Send {
        let _ = (record, user, db);
        async { Ok(Vec::new()) }
    }

    /// Every query the panel runs for this resource starts here: the
    /// model's query (with its default scope) by default. Narrow it to
    /// what the panel may ever show.
    fn query(&self) -> Query<Self::Model> {
        Self::Model::query()
    }

    /// Changes the list's grid after the panel built it from the columns,
    /// e.g. `grid.sort_by("name").per_page(50).groups(&["status"])`.
    fn grid(&self, grid: Grid) -> Grid {
        grid
    }

    /// Whether `user` may do `ability` (`viewAny`, `view`, `create`,
    /// `update`, `delete`, `deleteAny`, `restore`, `restoreAny`,
    /// `forceDelete`, `forceDeleteAny`, or an action's own) on `record`.
    /// By default, the model's [`Policy`] after `App::gate_before`; when
    /// there's no record (the list, the create page, what the list
    /// offers), the policy is asked about a blank record
    /// (`Self::Model::default()`).
    fn allows(&self, user: &AuthUser, ability: &str, record: Option<&Self::Model>) -> bool {
        match record {
            Some(record) => user.can(ability, record),
            None => user.can(ability, &Self::Model::default()),
        }
    }
}

/// What an action's code gets besides the records.
#[derive(Clone)]
#[non_exhaustive]
pub struct ActionContext {
    /// The app's state, e.g. its database (`state.db`).
    pub state: AppState,
    /// Who chose the action.
    pub user: AuthUser,
    /// What the user typed in the action's form ([`AdminAction::form`]);
    /// empty for an action without one.
    pub input: ActionInput,
}

/// What the user typed into an action's form, already checked against the
/// form's fields ([`AdminAction::form`]). Money fields are in the currency's
/// smallest unit, toggles are `true` or `false`, and a field left empty is
/// the empty text.
///
/// ```
/// # use renox_admin::ActionInput;
/// # fn demo(input: &ActionInput) {
/// let percent: f64 = input.parse("percent").unwrap_or(0.0);
/// let reason = input.text("reason");
/// let notify = input.bool("notify");
/// # let _ = (percent, reason, notify); }
/// ```
#[derive(Debug, Clone, Default)]
pub struct ActionInput {
    values: Vec<(String, String)>,
}

impl ActionInput {
    pub(crate) fn new(values: Vec<(String, String)>) -> Self {
        Self { values }
    }

    /// The value of field `name`, if the form had one.
    pub fn get(&self, name: &str) -> Option<&str> {
        self.values
            .iter()
            .find(|(key, _)| key == name)
            .map(|(_, value)| value.as_str())
    }

    /// The value of field `name`, or `""`.
    pub fn text(&self, name: &str) -> &str {
        self.get(name).unwrap_or("")
    }

    /// The value of field `name` read as a `T` (a number, a date…), or
    /// `None` when it is empty or doesn't read as one.
    pub fn parse<T: std::str::FromStr>(&self, name: &str) -> Option<T> {
        self.get(name)?.trim().parse().ok()
    }

    /// Whether the checkbox or toggle `name` was on.
    pub fn bool(&self, name: &str) -> bool {
        matches!(self.get(name), Some("true" | "on" | "1"))
    }

    /// Every field's name and value, in the form's order.
    pub fn iter(&self) -> impl Iterator<Item = (&str, &str)> {
        self.values.iter().map(|(k, v)| (k.as_str(), v.as_str()))
    }
}

/// What a resource's [`saved`](AdminResource::saved) hook gets besides the
/// record.
#[derive(Clone)]
#[non_exhaustive]
pub struct SaveContext {
    /// The app's state, e.g. its database (`state.db`).
    pub state: AppState,
    /// Who saved the record.
    pub user: AuthUser,
    /// The record was just created (the create page), not edited.
    pub created: bool,
    /// On an edit, the record as it was before the form changed it (its
    /// fields as JSON); `None` for a new record.
    pub previous: Option<renox::serde_json::Value>,
}

type Run<M> = Arc<dyn Fn(Vec<M>, ActionContext) -> BoxFuture<'static, Result<Toast>> + Send + Sync>;

/// An action of a resource's list (Filament's bulk actions): a button over
/// the selected rows that runs `run` with their records and shows the
/// toast it returns. With [`row`](Self::row), each row's menu has it too.
///
/// ```
/// # use renox::prelude::*;
/// use renox_admin::AdminAction;
/// # #[derive(Model, serde::Serialize, Default, Clone)] struct Product { id: i64, active: bool }
///
/// let deactivate = AdminAction::new("deactivate", "Deactivate", |products: Vec<Product>, cx| async move {
///     let ids: Vec<i64> = products.iter().map(|p| p.id).collect();
///     let n = Product::query().where_in("id", ids).update(&cx.state.db, &[("active", &false)]).await?;
///     Ok(Toast::success(format!("{n} products deactivated.")))
/// })
/// .confirm("Deactivate the selected products?")
/// .row();
/// # let _ = deactivate;
/// ```
///
/// Every record must pass [`AdminResource::allows`] for the action's
/// ability (`update` by default), or nothing runs (403).
pub struct AdminAction<M> {
    key: String,
    label: String,
    ability: String,
    confirm: Option<String>,
    danger: bool,
    bulk: bool,
    row: bool,
    form: Vec<Field>,
    description: Option<String>,
    submit: Option<String>,
    run: Run<M>,
}

impl<M> Clone for AdminAction<M> {
    fn clone(&self) -> Self {
        Self {
            key: self.key.clone(),
            label: self.label.clone(),
            ability: self.ability.clone(),
            confirm: self.confirm.clone(),
            danger: self.danger,
            bulk: self.bulk,
            row: self.row,
            form: self.form.clone(),
            description: self.description.clone(),
            submit: self.submit.clone(),
            run: self.run.clone(),
        }
    }
}

impl<M> fmt::Debug for AdminAction<M> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("AdminAction")
            .field("key", &self.key)
            .field("label", &self.label)
            .field("ability", &self.ability)
            .finish_non_exhaustive()
    }
}

impl<M: Send + 'static> AdminAction<M> {
    /// An action named `key` (letters, digits, `_` and `-`; part of its
    /// address), shown as `label`, that runs `run`.
    pub fn new<F, Fut>(key: &str, label: &str, run: F) -> Self
    where
        F: Fn(Vec<M>, ActionContext) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<Toast>> + Send + 'static,
    {
        Self {
            key: key.to_owned(),
            label: label.to_owned(),
            ability: "update".into(),
            confirm: None,
            danger: false,
            bulk: true,
            row: false,
            form: Vec::new(),
            description: None,
            submit: None,
            run: Arc::new(move |records, cx| Box::pin(run(records, cx))),
        }
    }

    /// The ability each record must allow (`update` by default).
    pub fn ability(mut self, ability: &str) -> Self {
        self.ability = ability.to_owned();
        self
    }

    /// Asks first, in a dialog with this question.
    pub fn confirm(mut self, question: &str) -> Self {
        self.confirm = Some(question.to_owned());
        self
    }

    /// Shown in red, for actions that can't be undone.
    pub fn danger(mut self) -> Self {
        self.danger = true;
        self
    }

    /// Also in each row's menu, for that row alone.
    pub fn row(mut self) -> Self {
        self.row = true;
        self
    }

    /// Only in each row's menu, not over the selection.
    pub fn row_only(mut self) -> Self {
        self.row = true;
        self.bulk = false;
        self
    }

    /// Asks for input first: the button opens a sheet with these fields
    /// (any [`Field`] except `belongs_to`), and the action runs once they
    /// are valid, with what was typed in [`ActionContext::input`]. A field
    /// is checked as far as its declaration goes: `required`, numbers and
    /// dates that read as such, `min` and `max`, a select's choices.
    ///
    /// ```
    /// # use renox::prelude::*;
    /// use renox_admin::{AdminAction, Field};
    /// # #[derive(Model, serde::Serialize, Default, Clone)] struct Product { id: i64, price: i64 }
    ///
    /// let reprice = AdminAction::new("reprice", "Change price by %", |products: Vec<Product>, cx| async move {
    ///     let percent: f64 = cx.input.parse("percent").unwrap_or(0.0);
    ///     for product in &products {
    ///         let price = (product.price as f64 * (1.0 + percent / 100.0)).round() as i64;
    ///         Product::query()
    ///             .where_eq("id", product.id)
    ///             .update(&cx.state.db, &[("price", &price)])
    ///             .await?;
    ///     }
    ///     Ok(Toast::success(format!("{} prices changed by {percent} %.", products.len())))
    /// })
    /// .form(vec![Field::number("percent", "Percent").required().min(-90).max(500).suffix("%")])
    /// .description("Every selected product's price changes by this much.")
    /// .submit_label("Change prices")
    /// .row();
    /// # let _ = reprice;
    /// ```
    pub fn form(mut self, fields: Vec<Field>) -> Self {
        self.form = fields;
        self
    }

    /// A line under the title of the input sheet ([`form`](Self::form)).
    pub fn description(mut self, text: &str) -> Self {
        self.description = Some(text.to_owned());
        self
    }

    /// The input sheet's button, instead of the action's label.
    pub fn submit_label(mut self, label: &str) -> Self {
        self.submit = Some(label.to_owned());
        self
    }

    pub(crate) fn fields(&self) -> &[Field] {
        &self.form
    }

    pub(crate) fn describe(&self) -> Option<&str> {
        self.description.as_deref()
    }

    pub(crate) fn submit(&self) -> Option<&str> {
        self.submit.as_deref()
    }

    pub(crate) fn key(&self) -> &str {
        &self.key
    }

    pub(crate) fn label(&self) -> &str {
        &self.label
    }

    pub(crate) fn ability_name(&self) -> &str {
        &self.ability
    }

    pub(crate) fn question(&self) -> Option<&str> {
        self.confirm.as_deref()
    }

    pub(crate) fn is_danger(&self) -> bool {
        self.danger
    }

    pub(crate) fn on_bulk(&self) -> bool {
        self.bulk
    }

    pub(crate) fn on_row(&self) -> bool {
        self.row
    }

    pub(crate) fn run(
        &self,
        records: Vec<M>,
        cx: ActionContext,
    ) -> BoxFuture<'static, Result<Toast>> {
        (self.run)(records, cx)
    }
}

impl<M: StateMachine + Send + Sync + 'static> AdminAction<M> {
    /// One row action per transition of `M`'s machine (system transitions
    /// left out), named like the transition, that runs it as the panel's
    /// user with the engine's checks: permission, `from` state and guards.
    /// A transition that needs a reason asks for it in a sheet. Use it as
    /// the resource's [`actions`](AdminResource::actions).
    ///
    /// The view page offers only the ones that are
    /// [`available`](AdminResource::available_transitions) for the record.
    pub fn transitions() -> Vec<Self> {
        renox::state_machine::info_of::<M>()
            .transitions
            .into_iter()
            .filter(|t| !t.system)
            .map(|t| {
                let name = t.name.clone();
                let label = t.label.clone();
                let mut action = Self::new(&t.name, &t.label, move |records: Vec<M>, cx| {
                    let name = name.clone();
                    let label = label.clone();
                    async move {
                        let reason = cx.input.text("reason").trim().to_owned();
                        let mut pending = false;
                        for record in &records {
                            let mut run = record.transition(&name).by(&cx.user);
                            if !reason.is_empty() {
                                run = run.reason(reason.clone());
                            }
                            match run
                                .run(&cx.state)
                                .await
                                .map_err(TransitionError::into_error)?
                            {
                                Outcome::Done(_) => {}
                                Outcome::Pending(_) => pending = true,
                            }
                        }
                        Ok(Toast::success(if pending {
                            format!("{label}: waiting for approval.")
                        } else {
                            format!("{label}: done.")
                        }))
                    }
                })
                .row_only();
                if t.needs_reason {
                    action = action.form(vec![Field::textarea("reason", "Reason").required()]);
                }
                if t.tone.as_deref() == Some("danger") {
                    action = action.danger();
                }
                action
            })
            .collect()
    }
}

type Scope<M> = Arc<dyn Fn(Query<M>) -> Query<M> + Send + Sync>;

/// A named filter over a resource's list (Filament's tabs and preset
/// filters), chosen with `?filter=key`.
///
/// ```
/// # use renox::prelude::*;
/// use renox_admin::Filter;
/// # #[derive(Model, serde::Serialize, Default)] struct Product { id: i64, active: bool, stock: i64 }
///
/// let filters = vec![
///     Filter::new("active", "Active", |q| q.where_eq("active", true)),
///     Filter::new("low", "Low stock", |q| q.where_op("stock", "<", 5)),
/// ];
/// # let _: Vec<Filter<Product>> = filters;
/// ```
pub struct Filter<M> {
    key: String,
    label: String,
    scope: Scope<M>,
}

impl<M> Clone for Filter<M> {
    fn clone(&self) -> Self {
        Self {
            key: self.key.clone(),
            label: self.label.clone(),
            scope: self.scope.clone(),
        }
    }
}

impl<M> fmt::Debug for Filter<M> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Filter")
            .field("key", &self.key)
            .field("label", &self.label)
            .finish_non_exhaustive()
    }
}

impl<M> Filter<M> {
    /// A filter named `key` (its value in `?filter=`), shown as `label`,
    /// that narrows the list's query with `scope`.
    pub fn new(
        key: &str,
        label: &str,
        scope: impl Fn(Query<M>) -> Query<M> + Send + Sync + 'static,
    ) -> Self {
        Self {
            key: key.to_owned(),
            label: label.to_owned(),
            scope: Arc::new(scope),
        }
    }

    pub(crate) fn key(&self) -> &str {
        &self.key
    }

    pub(crate) fn label(&self) -> &str {
        &self.label
    }

    pub(crate) fn apply(&self, query: Query<M>) -> Query<M> {
        (self.scope)(query)
    }
}