Skip to main content

qualia_core_db/semantic_instruments/
manifest.rs

1//! Logical instrument manifest (cold metadata). Not a dataset volume.
2
3use std::collections::BTreeMap;
4
5use serde::{Deserialize, Serialize};
6
7use super::category::{seed_notice_is_labelled, ContentCategory};
8use super::errors::InstrumentError;
9
10/// Collectable format version for this SI-03 logical model. Not a wire freeze.
11pub const COLLECTABLE_FORMAT_VERSION: &str = "0.1.0-demo";
12
13/// In-memory manifest. Extra keys are preserved (non-critical unknown fields).
14#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
15pub struct InstrumentManifest {
16    pub format_version: String,
17    pub instrument_id: String,
18    pub release_id: String,
19    pub version: String,
20    pub name: String,
21    pub purpose: String,
22    pub content_category: ContentCategory,
23    pub prohibited_interpretation: String,
24    pub domain: String,
25    pub ontology_reference: String,
26    pub entry_point: String,
27    pub citation: String,
28    pub honesty_notice: String,
29    pub incomplete_input: String,
30    pub result_kind: String,
31    pub licence: String,
32    pub authored_by: String,
33    pub accessible_text: String,
34    pub visual_media_type: String,
35    /// Digest of the HMC bytes excluding this field's own value is computed
36    /// after members are packed; stored here once known.
37    #[serde(default)]
38    pub content_digest: String,
39    #[serde(default, flatten)]
40    pub extra: BTreeMap<String, serde_json::Value>,
41}
42
43impl InstrumentManifest {
44    /// `InstrumentManifest::validate`.
45    pub fn validate(&self) -> Result<(), InstrumentError> {
46        let required = [
47            ("format_version", self.format_version.as_str()),
48            ("instrument_id", self.instrument_id.as_str()),
49            ("release_id", self.release_id.as_str()),
50            ("version", self.version.as_str()),
51            ("name", self.name.as_str()),
52            ("purpose", self.purpose.as_str()),
53            (
54                "prohibited_interpretation",
55                self.prohibited_interpretation.as_str(),
56            ),
57            ("domain", self.domain.as_str()),
58            ("ontology_reference", self.ontology_reference.as_str()),
59            ("entry_point", self.entry_point.as_str()),
60            ("citation", self.citation.as_str()),
61            ("honesty_notice", self.honesty_notice.as_str()),
62            ("incomplete_input", self.incomplete_input.as_str()),
63            ("result_kind", self.result_kind.as_str()),
64            ("licence", self.licence.as_str()),
65            ("authored_by", self.authored_by.as_str()),
66            ("accessible_text", self.accessible_text.as_str()),
67            ("visual_media_type", self.visual_media_type.as_str()),
68        ];
69        for (name, value) in required {
70            if value.trim().is_empty() {
71                return Err(InstrumentError::MissingField(name));
72            }
73        }
74        if self.instrument_id == self.release_id {
75            return Err(InstrumentError::Canonical(
76                "instrument_id and release_id must differ".into(),
77            ));
78        }
79        if self.content_category.requires_seed_label()
80            && !seed_notice_is_labelled(&self.honesty_notice)
81        {
82            return Err(InstrumentError::DemoUnlabelled);
83        }
84        Ok(())
85    }
86}