1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//! Carrier Collection interface for MCard operations across runtimes (Sprint 552 / INV-552).
//!
//! Exposes handle, raw-byte, and manifest operations aligned with TypeScript
//! `MCardCollection` and Python `CardCollection`.
use crate::error::Result;
use crate::layer0::mcard_record::MCardRecord;
use crate::layer2::vfs::{DirectoryManifest, MCardFileSystem};
use rusqlite::Connection;
use std::sync::{Arc, Mutex};
/// Carrier collection interface for MCard operations across runtimes (INV-552).
#[derive(Clone)]
pub struct MCardCollection {
fs: MCardFileSystem,
}
impl MCardCollection {
/// Create a collection wrapping an existing MCardFileSystem.
pub fn new(fs: MCardFileSystem) -> Self {
Self { fs }
}
/// Initializes an isolated, in-memory MCardCollection.
pub fn new_in_memory() -> Result<Self> {
let fs = MCardFileSystem::new_in_memory()?;
Ok(Self { fs })
}
/// Wraps an existing shared SQLite connection.
pub fn from_connection(conn: Arc<Mutex<Connection>>) -> Self {
let fs = MCardFileSystem::new(conn, "");
Self { fs }
}
/// Access the underlying MCardFileSystem.
pub fn fs(&self) -> &MCardFileSystem {
&self.fs
}
/// Writes raw content bytes into `card` and binds `uri` in `handle_registry` (`INV-510-01`, `INV-510-02`).
pub fn write_raw_bytes(
&self,
uri: &str,
content: &[u8],
algorithm: Option<&str>,
) -> Result<MCardRecord> {
self.fs.write_raw_bytes(uri, content, algorithm)
}
/// Reads and verifies exact raw bytes bound to `uri` (`INV-510-01`).
pub fn read_raw_bytes(&self, uri: &str) -> Result<Vec<u8>> {
self.fs.read_raw_bytes(uri)
}
/// Atomic Compare-and-Swap handle binding (`INV-510-03`).
pub fn bind(&self, uri: &str, new_ref: &str, expected_old_ref: Option<&str>) -> Result<bool> {
self.fs.bind(uri, new_ref, expected_old_ref)
}
/// Writes a `DirectoryManifest` `MCard` to `dir_uri` (`INV-510-04`).
pub fn write_directory_manifest(
&self,
dir_uri: &str,
manifest: &DirectoryManifest,
) -> Result<MCardRecord> {
self.fs.write_directory_manifest(dir_uri, manifest)
}
/// Reads a `DirectoryManifest` `MCard` from `dir_uri` (`INV-510-04`).
pub fn read_directory_manifest(&self, dir_uri: &str) -> Result<DirectoryManifest> {
self.fs.read_directory_manifest(dir_uri)
}
}