use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::{Arc, RwLock};
pub trait SubstrateAdapter: Send + Sync {
fn read_by_hash(&self, hash: &str) -> Result<Vec<u8>, String>;
fn write_blob(&self, hash: &str, content: &[u8]) -> Result<(), String>;
fn list_hashes(&self) -> Result<Vec<String>, String>;
fn substrate_uri(&self) -> String;
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SubstratePinnedVCard {
pub vcard_hash: String,
pub content_hash: String,
pub source_uri: String,
pub target_uri: String,
pub timestamp: String,
}
#[derive(Clone)]
pub struct MemorySubstrateAdapter {
id: String,
storage: Arc<RwLock<HashMap<String, Vec<u8>>>>,
}
impl MemorySubstrateAdapter {
pub fn new(id: &str) -> Self {
Self {
id: id.to_string(),
storage: Arc::new(RwLock::new(HashMap::new())),
}
}
}
impl SubstrateAdapter for MemorySubstrateAdapter {
fn read_by_hash(&self, hash: &str) -> Result<Vec<u8>, String> {
let store = self.storage.read().map_err(|e| e.to_string())?;
store.get(hash).cloned().ok_or_else(|| format!("Hash {} not found in memory://{}", hash, self.id))
}
fn write_blob(&self, hash: &str, content: &[u8]) -> Result<(), String> {
let mut store = self.storage.write().map_err(|e| e.to_string())?;
store.insert(hash.to_string(), content.to_vec());
Ok(())
}
fn list_hashes(&self) -> Result<Vec<String>, String> {
let store = self.storage.read().map_err(|e| e.to_string())?;
Ok(store.keys().cloned().collect())
}
fn substrate_uri(&self) -> String {
format!("memory://{}", self.id)
}
}
pub struct FileSubstrateAdapter {
base_dir: PathBuf,
}
impl FileSubstrateAdapter {
pub fn new(dir_path: impl AsRef<Path>) -> Result<Self, String> {
let path = dir_path.as_ref().to_path_buf();
fs::create_dir_all(&path).map_err(|e| e.to_string())?;
Ok(Self { base_dir: path })
}
}
impl SubstrateAdapter for FileSubstrateAdapter {
fn read_by_hash(&self, hash: &str) -> Result<Vec<u8>, String> {
let file_path = self.base_dir.join(hash);
fs::read(&file_path).map_err(|e| format!("Failed to read hash {} from file://{:?}: {}", hash, file_path, e))
}
fn write_blob(&self, hash: &str, content: &[u8]) -> Result<(), String> {
let file_path = self.base_dir.join(hash);
fs::write(&file_path, content).map_err(|e| format!("Failed to write hash {} to file://{:?}: {}", hash, file_path, e))
}
fn list_hashes(&self) -> Result<Vec<String>, String> {
let mut hashes = Vec::new();
if let Ok(entries) = fs::read_dir(&self.base_dir) {
for entry in entries.flatten() {
if let Ok(file_type) = entry.file_type()
&& file_type.is_file()
&& let Some(name) = entry.file_name().to_str() {
hashes.push(name.to_string());
}
}
}
Ok(hashes)
}
fn substrate_uri(&self) -> String {
format!("file://{}", self.base_dir.display())
}
}
pub struct SqliteSubstrateAdapter {
db_path: PathBuf,
}
impl SqliteSubstrateAdapter {
pub fn new(db_path: impl AsRef<Path>) -> Result<Self, String> {
let path = db_path.as_ref().to_path_buf();
if let Some(parent) = path.parent() {
let _ = fs::create_dir_all(parent);
}
let conn = rusqlite::Connection::open(&path).map_err(|e| e.to_string())?;
conn.execute(
"CREATE TABLE IF NOT EXISTS card (hash TEXT PRIMARY KEY, content TEXT, g_time TEXT);",
[],
).map_err(|e| e.to_string())?;
Ok(Self { db_path: path })
}
}
impl SubstrateAdapter for SqliteSubstrateAdapter {
fn read_by_hash(&self, hash: &str) -> Result<Vec<u8>, String> {
let conn = rusqlite::Connection::open(&self.db_path).map_err(|e| e.to_string())?;
let content: String = conn.query_row(
"SELECT content FROM card WHERE hash = ?1;",
rusqlite::params![hash],
|row| row.get(0),
).map_err(|e| format!("Hash {} not found in sqlite://{:?}: {}", hash, self.db_path, e))?;
Ok(content.into_bytes())
}
fn write_blob(&self, hash: &str, content: &[u8]) -> Result<(), String> {
let conn = rusqlite::Connection::open(&self.db_path).map_err(|e| e.to_string())?;
let content_str = String::from_utf8_lossy(content).to_string();
let now = crate::fs_utils::compute_g_time();
conn.execute(
"INSERT OR REPLACE INTO card (hash, content, g_time) VALUES (?1, ?2, ?3);",
rusqlite::params![hash, content_str, now],
).map_err(|e| e.to_string())?;
Ok(())
}
fn list_hashes(&self) -> Result<Vec<String>, String> {
let conn = rusqlite::Connection::open(&self.db_path).map_err(|e| e.to_string())?;
let mut stmt = conn.prepare("SELECT hash FROM card;").map_err(|e| e.to_string())?;
let rows = stmt.query_map([], |row| row.get(0)).map_err(|e| e.to_string())?;
let mut hashes = Vec::new();
for h in rows.flatten() {
hashes.push(h);
}
Ok(hashes)
}
fn substrate_uri(&self) -> String {
format!("sqlite://{}", self.db_path.display())
}
}
pub struct IpfsSubstrateAdapter {
id: String,
dir: PathBuf,
}
impl IpfsSubstrateAdapter {
pub fn new(id: &str) -> Result<Self, String> {
let dir = if id.contains('/') {
PathBuf::from(id)
} else {
std::env::temp_dir().join(format!("clm_ipfs_{}", id))
};
let _ = fs::create_dir_all(&dir);
Ok(Self {
id: id.to_string(),
dir,
})
}
}
impl SubstrateAdapter for IpfsSubstrateAdapter {
fn read_by_hash(&self, hash: &str) -> Result<Vec<u8>, String> {
let file_path = self.dir.join(hash);
fs::read(&file_path).map_err(|e| format!("Hash {} not found on IPFS node {}: {}", hash, self.id, e))
}
fn write_blob(&self, hash: &str, content: &[u8]) -> Result<(), String> {
let file_path = self.dir.join(hash);
fs::write(&file_path, content).map_err(|e| format!("Failed to write hash {} to IPFS node {}: {}", hash, self.id, e))
}
fn list_hashes(&self) -> Result<Vec<String>, String> {
let mut hashes = Vec::new();
if let Ok(entries) = fs::read_dir(&self.dir) {
for entry in entries.flatten() {
if let Ok(file_type) = entry.file_type()
&& file_type.is_file()
&& let Some(name) = entry.file_name().to_str() {
hashes.push(name.to_string());
}
}
}
Ok(hashes)
}
fn substrate_uri(&self) -> String {
format!("ipfs://{}", self.id)
}
}
pub struct NfsSubstrateAdapter {
mount_dir: PathBuf,
}
impl NfsSubstrateAdapter {
pub fn new(mount_path: impl AsRef<Path>) -> Result<Self, String> {
let path = mount_path.as_ref().to_path_buf();
fs::create_dir_all(&path).map_err(|e| e.to_string())?;
Ok(Self { mount_dir: path })
}
}
impl SubstrateAdapter for NfsSubstrateAdapter {
fn read_by_hash(&self, hash: &str) -> Result<Vec<u8>, String> {
let file_path = self.mount_dir.join(hash);
fs::read(&file_path).map_err(|e| format!("Failed to read hash {} from nfs://{:?}: {}", hash, file_path, e))
}
fn write_blob(&self, hash: &str, content: &[u8]) -> Result<(), String> {
let file_path = self.mount_dir.join(hash);
fs::write(&file_path, content).map_err(|e| format!("Failed to write hash {} to nfs://{:?}: {}", hash, file_path, e))
}
fn list_hashes(&self) -> Result<Vec<String>, String> {
let mut hashes = Vec::new();
if let Ok(entries) = fs::read_dir(&self.mount_dir) {
for entry in entries.flatten() {
if let Ok(file_type) = entry.file_type()
&& file_type.is_file()
&& let Some(name) = entry.file_name().to_str() {
hashes.push(name.to_string());
}
}
}
Ok(hashes)
}
fn substrate_uri(&self) -> String {
format!("nfs://{}", self.mount_dir.display())
}
}
pub struct S3SubstrateAdapter {
bucket_prefix: PathBuf,
}
impl S3SubstrateAdapter {
pub fn new(bucket_prefix: impl AsRef<Path>) -> Result<Self, String> {
let path = bucket_prefix.as_ref().to_path_buf();
fs::create_dir_all(&path).map_err(|e| e.to_string())?;
Ok(Self { bucket_prefix: path })
}
}
impl SubstrateAdapter for S3SubstrateAdapter {
fn read_by_hash(&self, hash: &str) -> Result<Vec<u8>, String> {
let file_path = self.bucket_prefix.join(hash);
fs::read(&file_path).map_err(|e| format!("Failed to read hash {} from s3://{:?}: {}", hash, file_path, e))
}
fn write_blob(&self, hash: &str, content: &[u8]) -> Result<(), String> {
let file_path = self.bucket_prefix.join(hash);
fs::write(&file_path, content).map_err(|e| format!("Failed to write hash {} to s3://{:?}: {}", hash, file_path, e))
}
fn list_hashes(&self) -> Result<Vec<String>, String> {
let mut hashes = Vec::new();
if let Ok(entries) = fs::read_dir(&self.bucket_prefix) {
for entry in entries.flatten() {
if let Ok(file_type) = entry.file_type()
&& file_type.is_file()
&& let Some(name) = entry.file_name().to_str() {
hashes.push(name.to_string());
}
}
}
Ok(hashes)
}
fn substrate_uri(&self) -> String {
format!("s3://{}", self.bucket_prefix.display())
}
}
pub fn parse_adapter(uri: &str) -> Result<Box<dyn SubstrateAdapter>, String> {
if uri.starts_with("memory://") {
let id = uri.trim_start_matches("memory://");
Ok(Box::new(MemorySubstrateAdapter::new(id)))
} else if uri.starts_with("ipfs://") {
let id = uri.trim_start_matches("ipfs://");
Ok(Box::new(IpfsSubstrateAdapter::new(id)?))
} else if uri.starts_with("sqlite://") {
let path = uri.trim_start_matches("sqlite://");
Ok(Box::new(SqliteSubstrateAdapter::new(path)?))
} else if uri.starts_with("file://") {
let path = uri.trim_start_matches("file://");
Ok(Box::new(FileSubstrateAdapter::new(path)?))
} else if uri.starts_with("nfs://") {
let path = uri.trim_start_matches("nfs://");
Ok(Box::new(NfsSubstrateAdapter::new(path)?))
} else if uri.starts_with("s3://") {
let path = uri.trim_start_matches("s3://");
Ok(Box::new(S3SubstrateAdapter::new(path)?))
} else {
Err(format!("Unsupported substrate URI scheme: {}", uri))
}
}
pub fn migrate_mcard(
from_adapter: &dyn SubstrateAdapter,
to_adapter: &dyn SubstrateAdapter,
hash: &str,
) -> Result<SubstratePinnedVCard, String> {
let content = from_adapter.read_by_hash(hash)?;
let read_hash = crate::hash::raw_hash_bytes(&content);
if read_hash != hash {
return Err(format!("Source hash mismatch during migration: expected {}, got {}", hash, read_hash));
}
to_adapter.write_blob(hash, &content)?;
let dst_content = to_adapter.read_by_hash(hash)?;
let dst_hash = crate::hash::raw_hash_bytes(&dst_content);
if dst_hash != hash {
return Err(format!("Destination hash mismatch during migration: expected {}, got {}", hash, dst_hash));
}
let now = chrono::Utc::now().to_rfc3339();
let vcard_raw = format!("pinned:{}:{}:{}:{}", hash, from_adapter.substrate_uri(), to_adapter.substrate_uri(), now);
let vcard_hash = crate::hash::raw_hash_bytes(vcard_raw.as_bytes());
Ok(SubstratePinnedVCard {
vcard_hash,
content_hash: hash.to_string(),
source_uri: from_adapter.substrate_uri(),
target_uri: to_adapter.substrate_uri(),
timestamp: now,
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_memory_adapter_roundtrip() {
let adapter = MemorySubstrateAdapter::new("test_node");
let content = b"hello substrate memory";
let hash = crate::hash::raw_hash_bytes(content);
adapter.write_blob(&hash, content).unwrap();
let read_back = adapter.read_by_hash(&hash).unwrap();
assert_eq!(read_back, content);
let list = adapter.list_hashes().unwrap();
assert_eq!(list, vec![hash]);
}
#[test]
fn test_substrate_migration_hash_invariance() {
let tmp_dir = std::env::temp_dir();
let file_path = tmp_dir.join("clm_s148_file_sub");
let sqlite_path = tmp_dir.join("clm_s148_sub.db");
let _ = fs::remove_dir_all(&file_path);
let _ = fs::remove_file(&sqlite_path);
let src = FileSubstrateAdapter::new(&file_path).unwrap();
let dst = SqliteSubstrateAdapter::new(&sqlite_path).unwrap();
let content = b"sovereign mcard payload for substrate migration test";
let hash = crate::hash::raw_hash_bytes(content);
src.write_blob(&hash, content).unwrap();
let vcard = migrate_mcard(&src, &dst, &hash).unwrap();
assert_eq!(vcard.content_hash, hash);
assert_eq!(vcard.source_uri, src.substrate_uri());
assert_eq!(vcard.target_uri, dst.substrate_uri());
let dst_read = dst.read_by_hash(&hash).unwrap();
assert_eq!(dst_read, content);
let _ = fs::remove_dir_all(&file_path);
let _ = fs::remove_file(&sqlite_path);
}
}