Skip to main content

sim_lib_standard_core/
install.rs

1//! Installing language profiles into a profile registry and publishing claims.
2
3use sim_kernel::{Cx, OpKey, Result, Symbol};
4
5use crate::{
6    LanguageProfile, ProfileRegistry, install_language_profile, standard_install_capability,
7};
8
9/// Summary returned by [`install_profile_stub`]: the installed profile symbol
10/// and its organ and conformance-test counts.
11#[derive(Clone, Debug, PartialEq, Eq)]
12pub struct StandardInstallReport {
13    /// Symbol of the installed profile.
14    pub profile: Symbol,
15    /// Number of organs the profile uses.
16    pub organ_count: usize,
17    /// Number of conformance tests the profile declares.
18    pub test_count: usize,
19}
20
21/// Operation key for the standard install operation.
22pub fn standard_install_op_key() -> OpKey {
23    OpKey::new(Symbol::new("standard"), Symbol::new("install"), 1)
24}
25
26/// Install `profile` into `registry` and publish its claims, gated on
27/// [`standard_install_capability`].
28///
29/// This is a first-reach entry point: it registers the profile, publishes its
30/// profile and badge claims, and returns a [`StandardInstallReport`].
31///
32/// [`standard_install_capability`]: crate::standard_install_capability
33///
34/// # Examples
35///
36/// ```
37/// use std::sync::Arc;
38///
39/// use sim_kernel::{Cx, DefaultFactory, NoopEvalPolicy, Symbol};
40/// use sim_lib_standard_core::{
41///     LanguageProfile, ProfileRegistry, install_profile_stub, standard_install_capability,
42/// };
43///
44/// let mut cx = Cx::new(Arc::new(NoopEvalPolicy), Arc::new(DefaultFactory));
45/// cx.grant(standard_install_capability());
46/// let mut registry = ProfileRegistry::new();
47///
48/// let profile = LanguageProfile::new(Symbol::qualified("lang", "demo/v1"))
49///     .with_reader(Symbol::qualified("codec", "lisp"));
50/// let report = install_profile_stub(&mut cx, &mut registry, profile).unwrap();
51///
52/// assert_eq!(report.profile, Symbol::qualified("lang", "demo/v1"));
53/// assert!(registry.profile(&Symbol::qualified("lang", "demo/v1")).is_some());
54/// ```
55pub fn install_profile_stub(
56    cx: &mut Cx,
57    registry: &mut ProfileRegistry,
58    profile: LanguageProfile,
59) -> Result<StandardInstallReport> {
60    cx.require(&standard_install_capability())?;
61    let report = StandardInstallReport {
62        profile: profile.symbol.clone(),
63        organ_count: profile.organs.len(),
64        test_count: profile.conformance_tests.len(),
65    };
66    install_language_profile(cx, registry, profile, &[])?;
67    Ok(report)
68}