Skip to main content

flm_core/core/registry/
resolver.rs

1//! The implementation of [`ElementResolver`] for [`FlmRegistry`]: looking an
2//! element up by its type name when a stored tree is read back.
3//!
4//! A tree written to its stored form does not carry the definitions of its
5//! elements. Most nodes are stored with enough data to rebuild their element,
6//! but a node whose element is a synthesized element or a presentation element
7//! is stored with the element's type name alone. Reading such a node back
8//! needs a lookup from type name to element value. The reading side takes that
9//! lookup as an [`ElementResolver`] trait object; this module makes every
10//! [`FlmRegistry`] one, so that a tree can be read back against the registry it
11//! was produced under.
12//!
13//! The lookup is [`FlmRegistry::element_value`]. It returns the element value
14//! when exactly one value carries the type name, nothing when none does, and
15//! the error [`AmbiguousElement`] when several do; the error lists every
16//! feature that registered a value under the name. Two features may register
17//! elements of the same type name, and then the name does not identify one
18//! element. Reading refuses such a node instead of taking the first value,
19//! because a wrong element would produce an incorrect document whose damage
20//! would be found much later.
21
22use std::sync::Arc;
23
24use crate::model::element::{AmbiguousElement, ElementSpec};
25use crate::core::registry::frozen::FlmRegistry;
26use crate::lang::serialize::ElementResolver;
27
28impl ElementResolver for FlmRegistry {
29    fn resolve_element(&self, name: &str) -> Result<Option<Arc<ElementSpec>>, AmbiguousElement> {
30        self.element_value(name).map(|value| value.cloned())
31    }
32}