Skip to main content

qualia_audio/
language.rs

1//! Swarm L — language resource bundle scaffold (oral-first).
2
3use crate::hash::q_hash;
4
5/// Versioned language resource identity (no requirement for orthography).
6#[derive(Debug, Clone)]
7pub struct LanguageResourceBundle {
8    pub authority_did_hash: u64,
9    pub local_name_hash: u64,
10    pub has_orthography: bool,
11    pub inventory_revision: u32,
12    pub access_class: AccessClass,
13    pub permitted_training: bool,
14}
15
16#[repr(u8)]
17#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18pub enum AccessClass {
19    Open = 0,
20    Community = 1,
21    Restricted = 2,
22    Ceremonial = 3,
23}
24
25impl LanguageResourceBundle {
26    /// Oral-only synthetic fixture (no writing system).
27    pub fn oral_fixture(name: &str) -> Self {
28        Self {
29            authority_did_hash: q_hash("did:webizen:fixture-authority"),
30            local_name_hash: q_hash(name),
31            has_orthography: false,
32            inventory_revision: 1,
33            access_class: AccessClass::Community,
34            permitted_training: false,
35        }
36    }
37
38    /// `true` when able to train.
39    pub fn can_train(&self) -> bool {
40        self.permitted_training && self.access_class != AccessClass::Ceremonial
41    }
42}
43
44/// Multi-tier annotation span (frame-accurate).
45#[derive(Debug, Clone, Copy)]
46pub struct AnnotationTier {
47    pub utterance_start: u64,
48    pub utterance_end: u64,
49    pub word_start: u64,
50    pub word_end: u64,
51    pub phone_start: u64,
52    pub phone_end: u64,
53    pub meaning_hash: u64,
54    pub is_machine_proposal: bool,
55}
56
57/// Persist a minimal JSON-ish line record (cold path).
58pub fn save_bundle_record(
59    path: &std::path::Path,
60    bundle: &LanguageResourceBundle,
61) -> Result<(), String> {
62    if let Some(p) = path.parent() {
63        std::fs::create_dir_all(p).map_err(|e| e.to_string())?;
64    }
65    let line = format!(
66        "authority={:016x} name={:016x} ortho={} rev={} access={:?} train={}\n",
67        bundle.authority_did_hash,
68        bundle.local_name_hash,
69        bundle.has_orthography,
70        bundle.inventory_revision,
71        bundle.access_class as u8,
72        bundle.permitted_training
73    );
74    std::fs::write(path, line).map_err(|e| e.to_string())
75}
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80
81    #[test]
82    fn oral_without_orthography() {
83        let b = LanguageResourceBundle::oral_fixture("test-variety");
84        assert!(!b.has_orthography);
85        assert!(!b.can_train());
86    }
87
88    #[test]
89    fn persist_oral_bundle() {
90        let dir = std::env::temp_dir().join(format!(
91            "lang-{}",
92            std::time::SystemTime::now()
93                .duration_since(std::time::UNIX_EPOCH)
94                .unwrap()
95                .as_nanos()
96        ));
97        let path = dir.join("bundle.txt");
98        let b = LanguageResourceBundle::oral_fixture("x");
99        save_bundle_record(&path, &b).unwrap();
100        assert!(path.is_file());
101        let _ = std::fs::remove_dir_all(&dir);
102    }
103}