Skip to main content

sim_lib_view/
universal.rs

1//! Registration of the universal default view and editor.
2//!
3//! Both are registered as the lowest-quality, always-matching lens of their
4//! kind, so dispatch ends here when nothing specialized claims a value.
5
6use std::sync::Arc;
7
8use sim_kernel::Symbol;
9use sim_shape::{AnyShape, shape_value};
10
11use crate::contract::{Lens, LensKind, LensMeta};
12use crate::dispatch::LensRegistry;
13use crate::universal_editor::UniversalEditor;
14use crate::universal_view::UniversalView;
15
16/// The universal default view lens id.
17pub const UNIVERSAL_VIEW_ID: &str = "view:default";
18
19/// The universal default editor lens id.
20pub const UNIVERSAL_EDITOR_ID: &str = "edit:default";
21
22/// The lowest quality, so the universal default loses every ranked tie.
23const LOWEST_QUALITY: i32 = -1_000_000;
24
25fn any_shape() -> sim_kernel::Value {
26    shape_value(Symbol::qualified("core", "Any"), Arc::new(AnyShape))
27}
28
29/// Register the universal default view and editor into `registry`. When
30/// `readonly` is set, the universal editor renders but never commits.
31pub fn register_universal_default(registry: &mut LensRegistry, readonly: bool) {
32    registry.register(Lens::view(
33        LensMeta::new(Symbol::new(UNIVERSAL_VIEW_ID), LensKind::View)
34            .claiming_shape(any_shape())
35            .with_quality_cost(LOWEST_QUALITY, 0)
36            .as_universal_default(),
37        Arc::new(UniversalView),
38    ));
39    let editor = if readonly {
40        UniversalEditor::readonly()
41    } else {
42        UniversalEditor::writable()
43    };
44    registry.register(Lens::editor(
45        LensMeta::new(Symbol::new(UNIVERSAL_EDITOR_ID), LensKind::Editor)
46            .claiming_shape(any_shape())
47            .with_quality_cost(LOWEST_QUALITY, 0)
48            .as_universal_default(),
49        Arc::new(editor),
50    ));
51}