sim_lib_dispatch/claims.rs
1use sim_kernel::{
2 Cx, LibId, OpKey, Result, Symbol,
3 standard::{publish_organ_claims, publish_organ_claims_for_lib},
4};
5
6/// Symbol identifying the dispatch organ in the claim store.
7///
8/// Used as the claim subject when publishing the organ's contributed
9/// operations into a [`Cx`].
10pub fn dispatch_organ_symbol() -> Symbol {
11 Symbol::qualified("organ", "dispatch")
12}
13
14/// Operation key for defining a generic function.
15pub fn dispatch_generic_op_key() -> OpKey {
16 dispatch_op_key("generic")
17}
18
19/// Operation key for adding a method to a generic function (multimethod).
20pub fn dispatch_multimethod_op_key() -> OpKey {
21 dispatch_op_key("multimethod")
22}
23
24/// Operation key for method combination (around/before/primary/after).
25pub fn dispatch_combine_op_key() -> OpKey {
26 dispatch_op_key("combine")
27}
28
29/// Operation key for reporting method specificity for a call.
30pub fn dispatch_specificity_op_key() -> OpKey {
31 dispatch_op_key("specificity")
32}
33
34/// Operation key for inspecting the applicable methods of a call.
35pub fn dispatch_inspect_op_key() -> OpKey {
36 dispatch_op_key("inspect")
37}
38
39/// All operation keys contributed by the dispatch organ, in claim order.
40pub fn dispatch_op_keys() -> Vec<OpKey> {
41 [
42 dispatch_generic_op_key(),
43 dispatch_multimethod_op_key(),
44 dispatch_combine_op_key(),
45 dispatch_specificity_op_key(),
46 dispatch_inspect_op_key(),
47 ]
48 .into()
49}
50
51/// Publishes the dispatch organ's claims and operation keys into a [`Cx`].
52///
53/// The kernel defines the claim/organ contract; this crate supplies the
54/// dispatch organ's operation set. After publishing, the organ and its ops
55/// are discoverable through the standard Card projection.
56///
57/// # Examples
58///
59/// ```
60/// use std::sync::Arc;
61/// use sim_kernel::{Cx, DefaultFactory, NoopEvalPolicy};
62/// use sim_lib_dispatch::publish_dispatch_organ_claims;
63///
64/// let mut cx = Cx::new(Arc::new(NoopEvalPolicy), Arc::new(DefaultFactory));
65/// publish_dispatch_organ_claims(&mut cx).unwrap();
66/// ```
67pub fn publish_dispatch_organ_claims(cx: &mut Cx) -> Result<()> {
68 publish_organ_claims(cx, dispatch_organ_symbol(), dispatch_op_keys())
69}
70
71/// Publishes dispatch organ claims as part of a loaded lib receipt.
72pub fn publish_dispatch_organ_claims_for_lib(cx: &mut Cx, lib_id: LibId) -> Result<()> {
73 publish_organ_claims_for_lib(cx, lib_id, dispatch_organ_symbol(), dispatch_op_keys())
74}
75
76fn dispatch_op_key(name: &str) -> OpKey {
77 OpKey::new(Symbol::new("dispatch"), Symbol::new(name), 1)
78}