Skip to main content

sim_lib_pattern/
claims.rs

1//! Organ claims for the pattern surface.
2//!
3//! These helpers name the pattern organ and its operation keys, then publish
4//! them as kernel claims so the organ and its operations are discoverable
5//! through the standard Card surface.
6
7use sim_kernel::{
8    Cx, LibId, OpKey, Result, Symbol,
9    standard::{publish_organ_claims, publish_organ_claims_for_lib},
10};
11
12use crate::match_form::MatchForm;
13
14/// Returns the organ symbol that identifies the pattern surface.
15pub fn pattern_organ_symbol() -> Symbol {
16    Symbol::qualified("organ", "pattern")
17}
18
19/// Returns the operation key for declaring an ADT.
20pub fn pattern_adt_op_key() -> OpKey {
21    pattern_op_key("adt")
22}
23
24/// Returns the operation key for constructing a tagged value.
25pub fn pattern_tag_op_key() -> OpKey {
26    pattern_op_key("tag")
27}
28
29/// Returns the operation key for matching a value against pattern arms.
30pub fn pattern_match_op_key() -> OpKey {
31    pattern_op_key("match")
32}
33
34/// Returns the operation key for destructuring a value or expression.
35pub fn pattern_destructure_op_key() -> OpKey {
36    pattern_op_key("destructure")
37}
38
39/// Returns the operation key for exhaustiveness checking.
40pub fn pattern_exhaustive_op_key() -> OpKey {
41    pattern_op_key("exhaustive")
42}
43
44/// Returns every pattern-surface operation this crate models, whether or not
45/// it is currently exported as a live runtime callable.
46pub fn pattern_declared_op_keys() -> Vec<OpKey> {
47    [
48        pattern_adt_op_key(),
49        pattern_tag_op_key(),
50        pattern_match_op_key(),
51        pattern_destructure_op_key(),
52        pattern_exhaustive_op_key(),
53    ]
54    .into()
55}
56
57/// Live pattern claim-to-export mappings backed by the loaded runtime surface.
58pub fn pattern_live_ops() -> Vec<(OpKey, Symbol)> {
59    vec![(pattern_match_op_key(), MatchForm::symbol())]
60}
61
62/// Returns the operation keys the pattern organ currently publishes as live claims.
63pub fn pattern_op_keys() -> Vec<OpKey> {
64    pattern_live_ops()
65        .into_iter()
66        .map(|(op_key, _export_symbol)| op_key)
67        .collect()
68}
69
70/// Publishes the pattern organ and its operation keys as kernel claims.
71pub fn publish_pattern_organ_claims(cx: &mut Cx) -> Result<()> {
72    publish_organ_claims(cx, pattern_organ_symbol(), pattern_op_keys())
73}
74
75/// Publishes pattern organ claims as part of a loaded lib receipt.
76pub fn publish_pattern_organ_claims_for_lib(cx: &mut Cx, lib_id: LibId) -> Result<()> {
77    publish_organ_claims_for_lib(cx, lib_id, pattern_organ_symbol(), pattern_op_keys())
78}
79
80fn pattern_op_key(name: &str) -> OpKey {
81    OpKey::new(Symbol::new("pattern"), Symbol::new(name), 1)
82}