ifc-approval 0.3.0

Bounded IFC4 approval resource semantics.
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
//! Transaction-staged authoring for the bounded approval domain.
//!
//! `IfcApproval` and the two approval relationships are resources, not
//! `IfcRoot` subtypes, so they carry no `GlobalId`; never invent one for
//! them. Only `IfcRelAssociatesApproval` is rooted, and its caller-supplied
//! GlobalId is validated before staging; it carries `IfcRoot.OwnerHistory`,
//! which IFC2X3 requires.
//!
//! Every writer binds the model's declared release and lays its record out
//! by attribute name from that release's table (see `release.rs`, #202,
//! #212). What the release cannot hold is refused, never dropped or
//! written into a slot that means something else.

use std::collections::HashSet;
use std::sync::Arc;

use ifc_model::guid::Guid;
use ifc_model::{Edit, EntityId, Model, Transaction, Value};

use ifc_schema::Schema;

use crate::release::{bind, require_owner_history};
use crate::{ApprovalError, ApprovalResult};

const APPROVAL: &str = "IFCAPPROVAL";
const APPROVAL_REL: &str = "IFCAPPROVALRELATIONSHIP";
const RESOURCE_REL: &str = "IFCRESOURCEAPPROVALRELATIONSHIP";
const ASSIGNMENT: &str = "IFCRELASSOCIATESAPPROVAL";

/// A date and time for an authoring draft, in the form the release
/// declares it.
///
/// IFC4 and IFC4X3 declare `IfcDateTime`, ISO 8601 text written as given;
/// IFC2X3 declares `IfcDateTimeSelect`, a reference to an existing or
/// earlier-staged `IfcCalendarDate`, `IfcLocalTime` or `IfcDateAndTime`.
/// A form the release does not declare is refused.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum DateTimeInput<'a> {
    /// IFC4/IFC4X3 `IfcDateTime` text.
    Text(&'a str),
    /// An IFC2X3 `IfcDateTimeSelect` record.
    Record(EntityId),
}

impl<'a> From<&'a str> for DateTimeInput<'a> {
    fn from(text: &'a str) -> Self {
        Self::Text(text)
    }
}

impl From<EntityId> for DateTimeInput<'_> {
    fn from(record: EntityId) -> Self {
        Self::Record(record)
    }
}

/// Draft for one `IfcApproval`.
#[derive(Debug, Clone, Copy, Default)]
#[non_exhaustive]
pub struct ApprovalDraft<'a> {
    /// Optional identifier; at least this or `name` is required.
    pub identifier: Option<&'a str>,
    /// Optional name; at least this or `identifier` is required.
    pub name: Option<&'a str>,
    /// Optional description.
    pub description: Option<&'a str>,
    /// Optional approval time: IFC4/IFC4X3 `IfcDateTime` text, or an
    /// existing or earlier-staged IFC2X3 `IfcDateTimeSelect` record
    /// (`ApprovalDateTime`, which IFC2X3 requires).
    pub time_of_approval: Option<DateTimeInput<'a>>,
    /// Optional status label.
    pub status: Option<&'a str>,
    /// Optional level label.
    pub level: Option<&'a str>,
    /// Optional qualifier text.
    pub qualifier: Option<&'a str>,
    /// Optional existing or earlier-staged `IfcActorSelect` target.
    pub requesting_approval: Option<EntityId>,
    /// Optional existing or earlier-staged `IfcActorSelect` target.
    pub giving_approval: Option<EntityId>,
}

impl<'a> ApprovalDraft<'a> {
    /// Starts a draft with every field unset.
    #[must_use]
    pub fn new() -> Self {
        Self {
            identifier: None,
            name: None,
            description: None,
            time_of_approval: None,
            status: None,
            level: None,
            qualifier: None,
            requesting_approval: None,
            giving_approval: None,
        }
    }

    /// Sets [`Self::identifier`]: Optional identifier; at least this or `name` is required.
    #[must_use]
    pub fn identifier(mut self, value: &'a str) -> Self {
        self.identifier = Some(value);
        self
    }

    /// Sets [`Self::name`]: Optional name; at least this or `identifier` is required.
    #[must_use]
    pub fn name(mut self, value: &'a str) -> Self {
        self.name = Some(value);
        self
    }

    /// Sets [`Self::description`]: Optional description.
    #[must_use]
    pub fn description(mut self, value: &'a str) -> Self {
        self.description = Some(value);
        self
    }

    /// Sets [`Self::time_of_approval`]: text, or an IFC2X3 date record.
    #[must_use]
    pub fn time_of_approval(mut self, value: impl Into<DateTimeInput<'a>>) -> Self {
        self.time_of_approval = Some(value.into());
        self
    }

    /// Sets [`Self::status`]: Optional status label.
    #[must_use]
    pub fn status(mut self, value: &'a str) -> Self {
        self.status = Some(value);
        self
    }

    /// Sets [`Self::level`]: Optional level label.
    #[must_use]
    pub fn level(mut self, value: &'a str) -> Self {
        self.level = Some(value);
        self
    }

    /// Sets [`Self::qualifier`]: Optional qualifier text.
    #[must_use]
    pub fn qualifier(mut self, value: &'a str) -> Self {
        self.qualifier = Some(value);
        self
    }

    /// Sets [`Self::requesting_approval`]: Optional existing or earlier-staged `IfcActorSelect` target.
    #[must_use]
    pub fn requesting_approval(mut self, value: EntityId) -> Self {
        self.requesting_approval = Some(value);
        self
    }

    /// Sets [`Self::giving_approval`]: Optional existing or earlier-staged `IfcActorSelect` target.
    #[must_use]
    pub fn giving_approval(mut self, value: EntityId) -> Self {
        self.giving_approval = Some(value);
        self
    }
}

/// Draft for one direct approval relationship.
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub struct ApprovalRelationshipDraft<'a> {
    /// Optional relationship name.
    pub name: Option<&'a str>,
    /// Optional relationship description.
    pub description: Option<&'a str>,
    /// Existing or earlier-staged approval.
    pub relating_approval: EntityId,
    /// Non-empty unique related approvals.
    pub related_approvals: &'a [EntityId],
}

impl<'a> ApprovalRelationshipDraft<'a> {
    /// Starts a draft with its required fields; the rest are unset.
    #[must_use]
    pub fn new(relating_approval: EntityId, related_approvals: &'a [EntityId]) -> Self {
        Self {
            name: None,
            description: None,
            relating_approval,
            related_approvals,
        }
    }

    /// Sets [`Self::name`]: Optional relationship name.
    #[must_use]
    pub fn name(mut self, value: &'a str) -> Self {
        self.name = Some(value);
        self
    }

    /// Sets [`Self::description`]: Optional relationship description.
    #[must_use]
    pub fn description(mut self, value: &'a str) -> Self {
        self.description = Some(value);
        self
    }
}

/// Draft for one approval-to-resource relationship.
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub struct ResourceApprovalDraft<'a> {
    /// Optional relationship name.
    pub name: Option<&'a str>,
    /// Optional relationship description.
    pub description: Option<&'a str>,
    /// Non-empty unique `IfcResourceObjectSelect` targets.
    pub related_resources: &'a [EntityId],
    /// Existing or earlier-staged approval.
    pub relating_approval: EntityId,
}

impl<'a> ResourceApprovalDraft<'a> {
    /// Starts a draft with its required fields; the rest are unset.
    #[must_use]
    pub fn new(related_resources: &'a [EntityId], relating_approval: EntityId) -> Self {
        Self {
            name: None,
            description: None,
            related_resources,
            relating_approval,
        }
    }

    /// Sets [`Self::name`]: Optional relationship name.
    #[must_use]
    pub fn name(mut self, value: &'a str) -> Self {
        self.name = Some(value);
        self
    }

    /// Sets [`Self::description`]: Optional relationship description.
    #[must_use]
    pub fn description(mut self, value: &'a str) -> Self {
        self.description = Some(value);
        self
    }
}

/// Draft for one rooted approval association to definitions.
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub struct ApprovalAssociationDraft<'a> {
    /// Compressed IFC GlobalId.
    pub global_id: &'a str,
    /// Optional relationship name.
    pub name: Option<&'a str>,
    /// Optional relationship description.
    pub description: Option<&'a str>,
    /// Non-empty unique `IfcDefinitionSelect` targets.
    pub related_objects: &'a [EntityId],
    /// Existing or earlier-staged approval.
    pub relating_approval: EntityId,
}

impl<'a> ApprovalAssociationDraft<'a> {
    /// Starts a draft with its required fields; the rest are unset.
    #[must_use]
    pub fn new(
        global_id: &'a str,
        related_objects: &'a [EntityId],
        relating_approval: EntityId,
    ) -> Self {
        Self {
            global_id,
            name: None,
            description: None,
            related_objects,
            relating_approval,
        }
    }

    /// Sets [`Self::name`]: Optional relationship name.
    #[must_use]
    pub fn name(mut self, value: &'a str) -> Self {
        self.name = Some(value);
        self
    }

    /// Sets [`Self::description`]: Optional relationship description.
    #[must_use]
    pub fn description(mut self, value: &'a str) -> Self {
        self.description = Some(value);
        self
    }
}

/// Validate and stage one approval in the model's declared release.
///
/// # Release
///
/// IFC4 and IFC4X3 declare nine attributes and require at least one of
/// `Identifier` and `Name`. IFC2X3 declares seven and requires `Identifier`,
/// `Name` and `ApprovalDateTime` (the IFC4 `TimeOfApproval`) as an
/// `IfcDateTimeSelect` record; it declares no `Status`, `Level`,
/// `Qualifier`, `RequestingApproval` or `GivingApproval` under those names.
///
/// # Errors
///
/// Neither identifier nor name; an actor that is not an `IfcActorSelect`; a
/// date record that is not a date record of the release; a header binding
/// no single verified release (`MultipleSchemas`, `UnsupportedSchema`); a
/// value the release does not declare (`AuthoringNotInSchema`) or cannot
/// hold (`AuthoringValueType`: text where IFC2X3 declares a record, a
/// record where IFC4 declares text); and a required value left unset
/// (`AuthoringRequired`). Nothing is staged on an error.
pub fn create_approval(
    tx: &mut Transaction,
    model: &Model,
    draft: ApprovalDraft<'_>,
) -> ApprovalResult<EntityId> {
    let layout = bind(model)?;
    if draft.identifier.is_none() && draft.name.is_none() {
        return Err(ApprovalError::AuthoringInvalid {
            entity: APPROVAL,
            attribute: "WR",
            value: "Identifier and Name are both absent".into(),
        });
    }
    for target in [draft.requesting_approval, draft.giving_approval]
        .into_iter()
        .flatten()
    {
        validate_target_in(layout.schema(), tx, model, target, "IfcActorSelect")?;
    }
    let time = match draft.time_of_approval {
        None => Value::Null,
        Some(DateTimeInput::Text(text)) => Value::Text(Arc::from(text)),
        Some(DateTimeInput::Record(target)) => {
            if let Some((_, declared)) = layout.declared(APPROVAL, "TimeOfApproval") {
                if layout.admits_entity(&declared.type_name, 8) {
                    validate_target_in(
                        layout.schema(),
                        tx,
                        model,
                        target,
                        declared.type_name.as_str(),
                    )?;
                }
            }
            Value::Ref(target)
        }
    };
    let record = layout.named_record(
        APPROVAL,
        vec![
            ("Identifier", optional_text(draft.identifier)),
            ("Name", optional_text(draft.name)),
            ("Description", optional_text(draft.description)),
            ("TimeOfApproval", time),
            ("Status", optional_text(draft.status)),
            ("Level", optional_text(draft.level)),
            ("Qualifier", optional_text(draft.qualifier)),
            (
                "RequestingApproval",
                optional_ref(draft.requesting_approval),
            ),
            ("GivingApproval", optional_ref(draft.giving_approval)),
        ],
    )?;
    Ok(tx.create(record))
}

/// Validate and stage one approval-to-approval relationship in the model's
/// declared release.
///
/// IFC2X3 declares a single `RelatedApproval` and a required `Name`; IFC4
/// and IFC4X3 a set of `RelatedApprovals` and an optional `Name`.
///
/// # Errors
///
/// An empty, duplicated or self-referencing set; a target that is not an
/// `IfcApproval`; a header binding no single verified release; in IFC2X3
/// more than one related approval (`AuthoringValueType`) or no name
/// (`AuthoringRequired`). Nothing is staged on an error.
pub fn relate_approvals(
    tx: &mut Transaction,
    model: &Model,
    draft: ApprovalRelationshipDraft<'_>,
) -> ApprovalResult<EntityId> {
    let layout = bind(model)?;
    layout.require_entity(APPROVAL_REL)?;
    validate_target_in(
        layout.schema(),
        tx,
        model,
        draft.relating_approval,
        APPROVAL,
    )?;
    validate_set(
        layout.schema(),
        tx,
        model,
        APPROVAL_REL,
        "RelatedApprovals",
        draft.related_approvals,
        APPROVAL,
        Some(draft.relating_approval),
    )?;
    let single = layout
        .declared(APPROVAL_REL, "RelatedApprovals")
        .is_some_and(|(_, declared)| !declared.aggregate);
    let related = match (single, draft.related_approvals) {
        (false, related) => refs(related),
        (true, [one]) => Value::Ref(*one),
        (true, _) => {
            return Err(ApprovalError::AuthoringValueType {
                entity: APPROVAL_REL,
                attribute: "RelatedApprovals",
                declared: "IfcApproval",
                schema: layout.version(),
            })
        }
    };
    let record = layout.named_record(
        APPROVAL_REL,
        vec![
            ("Name", optional_text(draft.name)),
            ("Description", optional_text(draft.description)),
            ("RelatingApproval", Value::Ref(draft.relating_approval)),
            ("RelatedApprovals", related),
        ],
    )?;
    Ok(tx.create(record))
}

/// Validate and stage one approval-to-resource relationship in the model's
/// declared release.
///
/// # Errors
///
/// An empty or duplicated set; a relating target that is not an
/// `IfcApproval` or a resource outside `IfcResourceObjectSelect`; a header
/// binding no single verified release; and an IFC2X3 model, which declares
/// no `IfcResourceApprovalRelationship` (`EntityNotInSchema`). Nothing is
/// staged on an error.
pub fn relate_resource_approval(
    tx: &mut Transaction,
    model: &Model,
    draft: ResourceApprovalDraft<'_>,
) -> ApprovalResult<EntityId> {
    let layout = bind(model)?;
    layout.require_entity(RESOURCE_REL)?;
    validate_target_in(
        layout.schema(),
        tx,
        model,
        draft.relating_approval,
        APPROVAL,
    )?;
    validate_set(
        layout.schema(),
        tx,
        model,
        RESOURCE_REL,
        "RelatedResourceObjects",
        draft.related_resources,
        "IfcResourceObjectSelect",
        None,
    )?;
    let record = layout.named_record(
        RESOURCE_REL,
        vec![
            ("Name", optional_text(draft.name)),
            ("Description", optional_text(draft.description)),
            ("RelatedResourceObjects", refs(draft.related_resources)),
            ("RelatingApproval", Value::Ref(draft.relating_approval)),
        ],
    )?;
    Ok(tx.create(record))
}

/// Validate and stage one rooted approval association.
///
/// # Release
///
/// Bound to the model's declared release (#202): the record is laid out by
/// attribute name from that release's table, and its references are checked
/// against it (`RelatedObjects` is `IfcDefinitionSelect` in IFC4 and IFC4X3,
/// `IfcRoot` restricted by WR21 in IFC2X3). `OwnerHistory` is left `$`,
/// which IFC4 and IFC4X3 allow and IFC2X3 does not, so an IFC2X3 model is
/// refused with [`ApprovalError::AuthoringRequired`]. Use
/// [`associate_approval_with_owner_history`] there.
///
/// # Errors
///
/// A malformed GlobalId, a relating approval that is not an `IfcApproval`,
/// an empty, duplicated or out-of-type `RelatedObjects`; a model that binds
/// no single known release ([`ApprovalError::MultipleSchemas`],
/// [`ApprovalError::UnsupportedSchema`]), and an IFC2X3 model. Nothing is
/// staged on an error.
pub fn associate_approval(
    tx: &mut Transaction,
    model: &Model,
    draft: ApprovalAssociationDraft<'_>,
) -> ApprovalResult<EntityId> {
    associate(tx, model, draft, None)
}

/// [`associate_approval`] with a caller-supplied `IfcOwnerHistory`, which
/// IFC2X3 requires on every `IfcRoot`.
///
/// `owner_history` must be in the model or staged earlier on `tx`, and must
/// be an `IfcOwnerHistory`; one is never invented here (build it with
/// `ifc-author`). In IFC4 and IFC4X3 the reference fills the optional slot.
///
/// # Errors
///
/// Those of [`associate_approval`] except the IFC2X3 refusal, and
/// [`ApprovalError::UnknownEntity`] for an `owner_history` that does not
/// resolve or [`ApprovalError::AuthoringReferenceType`] for one that is not
/// an `IfcOwnerHistory`. Nothing is staged on an error.
pub fn associate_approval_with_owner_history(
    tx: &mut Transaction,
    model: &Model,
    draft: ApprovalAssociationDraft<'_>,
    owner_history: EntityId,
) -> ApprovalResult<EntityId> {
    associate(tx, model, draft, Some(owner_history))
}

/// Stage an `IfcRelAssociatesApproval`; `None` leaves `OwnerHistory` `$`.
fn associate(
    tx: &mut Transaction,
    model: &Model,
    draft: ApprovalAssociationDraft<'_>,
    owner_history: Option<EntityId>,
) -> ApprovalResult<EntityId> {
    let layout = bind(model)?;
    if Guid::parse(draft.global_id).is_none() {
        return Err(ApprovalError::AuthoringInvalid {
            entity: ASSIGNMENT,
            attribute: "GlobalId",
            value: draft.global_id.into(),
        });
    }
    validate_target_in(
        layout.schema(),
        tx,
        model,
        draft.relating_approval,
        APPROVAL,
    )?;
    if draft.related_objects.is_empty() {
        return Err(ApprovalError::AuthoringInvalid {
            entity: ASSIGNMENT,
            attribute: "RelatedObjects",
            value: "empty SET [1:?]".into(),
        });
    }
    let mut seen = HashSet::new();
    for &target in draft.related_objects {
        if !seen.insert(target) {
            return Err(ApprovalError::AuthoringInvalid {
                entity: ASSIGNMENT,
                attribute: "RelatedObjects",
                value: format!("duplicate {target}"),
            });
        }
        let actual =
            final_type(tx, model, target).ok_or(ApprovalError::UnknownEntity { id: target })?;
        if !layout.is_definition(actual) {
            return Err(ApprovalError::AuthoringReferenceType {
                target,
                expected: "IfcDefinitionSelect",
                actual: actual.into(),
            });
        }
    }
    if let Some(owner_history) = owner_history {
        require_owner_history(tx, model, owner_history)?;
    }
    let record = layout.named_record(
        ASSIGNMENT,
        vec![
            ("GlobalId", text(draft.global_id)),
            ("OwnerHistory", optional_ref(owner_history)),
            ("Name", optional_text(draft.name)),
            ("Description", optional_text(draft.description)),
            ("RelatedObjects", refs(draft.related_objects)),
            ("RelatingApproval", Value::Ref(draft.relating_approval)),
        ],
    )?;
    Ok(tx.create(record))
}

#[allow(clippy::too_many_arguments)]
fn validate_set(
    schema: &Schema,
    tx: &Transaction,
    model: &Model,
    kind: &'static str,
    attribute: &'static str,
    targets: &[EntityId],
    expected: &'static str,
    disallow: Option<EntityId>,
) -> ApprovalResult<()> {
    if targets.is_empty() {
        return Err(ApprovalError::AuthoringInvalid {
            entity: kind,
            attribute,
            value: "empty SET [1:?]".into(),
        });
    }
    let mut seen = HashSet::new();
    for &target in targets {
        if !seen.insert(target) {
            return Err(ApprovalError::AuthoringInvalid {
                entity: kind,
                attribute,
                value: format!("duplicate {target}"),
            });
        }
        if Some(target) == disallow {
            return Err(ApprovalError::AuthoringInvalid {
                entity: kind,
                attribute,
                value: format!("self reference {target}"),
            });
        }
        validate_target_in(schema, tx, model, target, expected)?;
    }
    Ok(())
}

/// Fail unless `target` resolves, in the model or staged on `tx`, to a type
/// `schema` accepts as `expected`.
fn validate_target_in(
    schema: &Schema,
    tx: &Transaction,
    model: &Model,
    target: EntityId,
    expected: &'static str,
) -> ApprovalResult<()> {
    let actual =
        final_type(tx, model, target).ok_or(ApprovalError::UnknownEntity { id: target })?;
    if schema.accepts_type(expected, actual) {
        Ok(())
    } else {
        Err(ApprovalError::AuthoringReferenceType {
            target,
            expected,
            actual: actual.into(),
        })
    }
}

fn final_type<'a>(tx: &'a Transaction, model: &'a Model, id: EntityId) -> Option<&'a str> {
    for edit in tx.edits().iter().rev() {
        match edit {
            Edit::Create {
                id: edit_id,
                entity,
            } if *edit_id == id => return Some(&entity.type_name),
            Edit::Remove { id: edit_id } if *edit_id == id => return None,
            Edit::Retype {
                id: edit_id,
                type_name,
            } if *edit_id == id => return Some(type_name),
            _ => {}
        }
    }
    model.get(id).map(|entity| entity.type_name.as_ref())
}

fn text(value: &str) -> Value {
    Value::Text(Arc::from(value))
}
fn optional_text(value: Option<&str>) -> Value {
    value.map_or(Value::Null, text)
}
fn optional_ref(value: Option<EntityId>) -> Value {
    value.map_or(Value::Null, Value::Ref)
}
fn refs(values: &[EntityId]) -> Value {
    Value::List(values.iter().copied().map(Value::Ref).collect())
}