rustio-admin 0.8.0

Django Admin, but for Rust. A small, focused admin framework.
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
//! Field classification + filter inference (renamed from
//! `intelligence` per Section 4 of the strategic reset plan).
//!
//! Pure helpers that turn *schema metadata* into *user-facing hints*:
//! the form-field label beside an input, the masked display of a
//! sensitive value on a list page, the filter dropdown inferred from
//! a `status` column. Nothing in this module touches the filesystem,
//! the database, or produces HTML — it returns structured data that
//! the admin renderer consumes.
//!
//! ## Public API
//!
//! - [`classify_field`] — labels a field by role (`Id`, `Email`,
//!   `Status`, …). Every downstream renderer branches on this enum.
//! - [`field_ui_metadata`] — packages the label, placeholder, hint,
//!   and sensitivity marker a form needs to render one input.
//! - [`infer_filters`] — walks a model's fields and decides which
//!   filters make sense on its list page.
//! - [`format_relation_cell`] — render a foreign-key cell on the list
//!   page as `Target #42` (or `42` when the target name is unknown).
//! - [`mask_pii`] — deterministic string masker used to hide personal
//!   data by default on list views.
//!
//! Slimmed for Tier 1: the legacy module's country/industry/GDPR
//! `ContextConfig` plumbing was wired through `crate::ai` (Tier 2) so
//! it's been removed; classification is now shape-based only.
//! `classify_search` / `SearchIntent` are dropped per Section 3.

use crate::admin::{AdminField, FieldType};

/// The role a field plays in the admin UI. One field maps to exactly
/// one role; the ordering of branches in [`classify_field`] resolves
/// overlaps (e.g. an `email` column is `FieldRole::Email`, not
/// `FieldRole::PlainText`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FieldRole {
    /// Primary key. Rendered monospace, excluded from edit forms.
    Id,
    /// `DateTime<Utc>`-shaped columns.
    Timestamp,
    /// Booleans — rendered as a pill on the list page, a checkbox in forms.
    Bool,
    /// Numeric values that aren't identifiers.
    NumericCount,
    /// `<something>_id` integer column that points at another model.
    ForeignKey,
    /// A `status` / `*_status` column.
    Status,
    /// An email address. Masked by default on list views.
    Email,
    /// A phone number. Masked by default.
    Phone,
    /// Everything else.
    PlainText,
}

impl FieldRole {
    /// `true` when the role carries personal data and should be masked
    /// by default on list views.
    pub fn is_sensitive(self) -> bool {
        matches!(self, FieldRole::Email | FieldRole::Phone)
    }
}

/// Everything a form / list renderer needs to present one field to a
/// human. All strings are plain text (no HTML) — the caller escapes
/// before emitting.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FieldUI {
    pub role: FieldRole,
    pub label: String,
    pub placeholder: Option<String>,
    pub hint: Option<String>,
    /// `true` when the field should carry the lock marker and (for
    /// list views) be masked by default.
    pub sensitive: bool,
    /// One-line explanation of *why* the field is sensitive.
    pub sensitivity_note: Option<String>,
    /// Set when the field is a FK to a known model. Carries the
    /// *singular* display name of the target (e.g. `"Applicant"`).
    pub relation_label: Option<String>,
}

/// What shape of filter the admin list page should render for a given
/// field.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FilterKind {
    /// `<select>` over distinct string values.
    DropdownText,
    /// Yes / No dropdown over a boolean column.
    BoolYesNo,
    /// Two `date` / `datetime-local` inputs bounding a range.
    DateRange,
    /// Numeric exact-match input.
    NumericExact,
    /// Single-line input, compared exactly.
    ExactMatch,
    /// `<select>` populated by the admin runtime from rows of the
    /// target model.
    RelationSelect { target_model: String },
}

/// One filter the list page should show for a model.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FilterDef {
    pub field: String,
    pub label: String,
    pub kind: FilterKind,
}

// ---------------------------------------------------------------------------
// classify_field
// ---------------------------------------------------------------------------

/// Assign a [`FieldRole`] to one field.
///
/// Order of precedence (highest first):
/// 1. Shape: `id`, `email`, `phone`, `status`, bool, datetime, FK, numeric.
/// 2. Fallback: `PlainText`.
pub fn classify_field(f: &AdminField) -> FieldRole {
    let name = f.name;
    if name == "id" {
        return FieldRole::Id;
    }
    if name == "email" {
        return FieldRole::Email;
    }
    if name == "phone" {
        return FieldRole::Phone;
    }
    if matches!(f.field_type, FieldType::Bool) {
        return FieldRole::Bool;
    }
    if matches!(
        f.field_type,
        FieldType::DateTime | FieldType::OptionalDateTime
    ) {
        return FieldRole::Timestamp;
    }
    if name == "status" || name.ends_with("_status") {
        return FieldRole::Status;
    }
    // FK only when the column is integer-typed. A String column
    // ending in `_id` is an opaque identifier, not a FK.
    if name.ends_with("_id") && matches!(f.field_type, FieldType::I32 | FieldType::I64) {
        return FieldRole::ForeignKey;
    }
    if matches!(f.field_type, FieldType::I32 | FieldType::I64) {
        return FieldRole::NumericCount;
    }
    FieldRole::PlainText
}

// ---------------------------------------------------------------------------
// field_ui_metadata
// ---------------------------------------------------------------------------

/// Package a field's display metadata for the admin form / list
/// renderers. All strings are plain text — escape before emitting.
pub fn field_ui_metadata(f: &AdminField) -> FieldUI {
    let role = classify_field(f);
    let label = humanise(f.name);
    let mut placeholder: Option<String> = None;
    let mut hint: Option<String> = None;
    let mut sensitive = false;
    let mut sensitivity_note: Option<String> = None;

    match role {
        FieldRole::Email => {
            placeholder = Some("name@example.com".into());
        }
        FieldRole::Phone => {
            placeholder = Some("+1 555 123 4567".into());
        }
        FieldRole::Timestamp => {
            placeholder = Some("YYYY-MM-DDTHH:MM".into());
            hint = Some("Interpreted as UTC.".into());
        }
        FieldRole::Status => {
            hint = Some("Short status label (e.g. active, pending, resolved).".into());
        }
        FieldRole::ForeignKey => {
            hint = Some("Foreign-key id — must reference an existing row.".into());
        }
        FieldRole::Id | FieldRole::Bool | FieldRole::NumericCount | FieldRole::PlainText => {}
    }

    // Name-based UI hints applied AFTER role classification.
    if f.name == "slug" {
        placeholder = Some("my-post-title".into());
        hint = Some("URL-friendly identifier".into());
    }

    if role.is_sensitive() {
        sensitive = true;
        sensitivity_note = Some("Personal data.".into());
    }

    FieldUI {
        role,
        label,
        placeholder,
        hint,
        sensitive,
        sensitivity_note,
        relation_label: None,
    }
}

/// Like [`field_ui_metadata`] but relation-aware. Pass the singular
/// display name of the target model when the schema records a relation
/// for this field; the returned [`FieldUI`] then carries
/// `relation_label` and a hint of the form "Foreign key to Target".
pub fn field_ui_metadata_with_relation(f: &AdminField, relation_target: Option<&str>) -> FieldUI {
    let mut ui = field_ui_metadata(f);
    if let Some(target) = relation_target.filter(|t| !t.is_empty()) {
        // A known relation always renders as ForeignKey even if the
        // column name wouldn't hit the `_id` heuristic.
        ui.role = FieldRole::ForeignKey;
        ui.relation_label = Some(target.to_string());
        ui.hint = Some(format!("Foreign key to {target}."));
    }
    ui
}

/// Render "Target #42" for a foreign-key cell on a list view. Falls
/// back to the raw id when the caller doesn't have a target name.
pub fn format_relation_cell(id: i64, target: Option<&str>) -> String {
    match target {
        Some(t) if !t.is_empty() => format!("{t} #{id}"),
        _ => id.to_string(),
    }
}

// ---------------------------------------------------------------------------
// infer_filters
// ---------------------------------------------------------------------------

/// Infer the filter controls for a model's list page from its fields.
/// Order follows the order of `fields`; every filter references a
/// field that actually exists on the model.
pub fn infer_filters(fields: &[AdminField]) -> Vec<FilterDef> {
    infer_filters_with_relations(fields, |_| None)
}

/// Like [`infer_filters`] but invokes `relation_target_of` for each
/// field to detect relation columns. If the callback returns
/// `Some(target)`, the filter is emitted as
/// [`FilterKind::RelationSelect`] instead of the numeric-exact fallback.
pub fn infer_filters_with_relations<F>(
    fields: &[AdminField],
    relation_target_of: F,
) -> Vec<FilterDef>
where
    F: Fn(&AdminField) -> Option<String>,
{
    let mut out: Vec<FilterDef> = Vec::new();
    for f in fields {
        if f.name == "id" {
            continue;
        }
        let role = classify_field(f);
        let kind = match role {
            FieldRole::Status => FilterKind::DropdownText,
            FieldRole::Bool => FilterKind::BoolYesNo,
            FieldRole::Timestamp => FilterKind::DateRange,
            FieldRole::NumericCount => FilterKind::NumericExact,
            FieldRole::ForeignKey => match relation_target_of(f) {
                Some(target_model) if !target_model.is_empty() => {
                    FilterKind::RelationSelect { target_model }
                }
                _ => FilterKind::NumericExact,
            },
            // Plain text, email, phone — no stock filter.
            _ => continue,
        };
        out.push(FilterDef {
            field: f.name.to_string(),
            label: humanise(f.name),
            kind,
        });
    }
    out
}

// ---------------------------------------------------------------------------
// mask_pii
// ---------------------------------------------------------------------------

/// Produce a masked display string for a sensitive value. Keeps the
/// first few characters so a reviewer can tell which row they're
/// looking at, replaces the rest with `•`. Length of the output
/// matches the input. Deterministic, Unicode-safe.
pub fn mask_pii(value: &str) -> String {
    if value.is_empty() {
        return String::new();
    }
    let chars: Vec<char> = value.chars().collect();
    let n = chars.len();
    let keep = (n / 3).clamp(2, 4).min(n);
    let mut out = String::with_capacity(n);
    for (i, c) in chars.iter().enumerate() {
        if i < keep {
            out.push(*c);
        } else {
            out.push('');
        }
    }
    out
}

// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------

fn humanise(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    let mut next_upper = true;
    for ch in s.chars() {
        if ch == '_' {
            out.push(' ');
            next_upper = true;
        } else if next_upper {
            out.push(ch.to_ascii_uppercase());
            next_upper = false;
        } else {
            out.push(ch);
        }
    }
    out
}

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

    fn field(name: &'static str, ty: FieldType) -> AdminField {
        AdminField {
            name,
            label: name,
            field_type: ty,
            editable: true,
            relation: None,
            choices: None,
        }
    }

    #[test]
    fn classify_id_email_status_bool_timestamp() {
        assert_eq!(classify_field(&field("id", FieldType::I64)), FieldRole::Id);
        assert_eq!(
            classify_field(&field("email", FieldType::String)),
            FieldRole::Email
        );
        assert_eq!(
            classify_field(&field("status", FieldType::String)),
            FieldRole::Status
        );
        assert_eq!(
            classify_field(&field("order_status", FieldType::String)),
            FieldRole::Status
        );
        assert_eq!(
            classify_field(&field("active", FieldType::Bool)),
            FieldRole::Bool
        );
        assert_eq!(
            classify_field(&field("created_at", FieldType::DateTime)),
            FieldRole::Timestamp
        );
    }

    #[test]
    fn fk_only_for_integer_id_columns() {
        assert_eq!(
            classify_field(&field("user_id", FieldType::I64)),
            FieldRole::ForeignKey
        );
        // String column ending _id is opaque, not FK.
        assert_eq!(
            classify_field(&field("national_id", FieldType::String)),
            FieldRole::PlainText
        );
    }

    #[test]
    fn infer_filters_skips_id_and_picks_kinds() {
        let fields = vec![
            field("id", FieldType::I64),
            field("status", FieldType::String),
            field("active", FieldType::Bool),
            field("created_at", FieldType::DateTime),
            field("title", FieldType::String),
        ];
        let filters = infer_filters(&fields);
        assert_eq!(filters.len(), 3);
        assert!(matches!(filters[0].kind, FilterKind::DropdownText));
        assert!(matches!(filters[1].kind, FilterKind::BoolYesNo));
        assert!(matches!(filters[2].kind, FilterKind::DateRange));
    }

    #[test]
    fn mask_pii_keeps_prefix_and_replaces_with_bullets() {
        assert_eq!(mask_pii("alice@example.com"), "alic•••••••••••••");
        assert_eq!(mask_pii(""), "");
    }

    #[test]
    fn relation_label_overrides_role() {
        let f = field("user_id", FieldType::I64);
        let ui = field_ui_metadata_with_relation(&f, Some("User"));
        assert_eq!(ui.role, FieldRole::ForeignKey);
        assert_eq!(ui.relation_label.as_deref(), Some("User"));
        assert!(ui.hint.unwrap().contains("Foreign key to User"));
    }

    #[test]
    fn format_relation_cell_with_and_without_target() {
        assert_eq!(format_relation_cell(42, Some("User")), "User #42");
        assert_eq!(format_relation_cell(42, None), "42");
        assert_eq!(format_relation_cell(42, Some("")), "42");
    }
}