use crate::Commands;
pub(super) fn handle_substrate_cmd(
command: &Commands,
_clm_root: &str,
stdout: &mut String,
stderr: &mut String,
) -> Option<i32> {
match command {
Commands::SubstrateWrite { uri, content } => {
let adapter = match clm_rust_core::substrate::parse_adapter(uri) {
Ok(a) => a,
Err(e) => {
stderr.push_str(&format!("[SUBSTRATE ERROR] {}\n", e));
return Some(1);
}
};
let bytes = content.as_bytes();
let hash = clm_rust_core::hash::raw_hash_bytes(bytes);
match adapter.write_blob(&hash, bytes) {
Ok(_) => {
stdout.push_str(&format!("[SUBSTRATE WRITE] Wrote hash {} to {}\n", hash, uri));
Some(0)
}
Err(e) => {
stderr.push_str(&format!("[SUBSTRATE WRITE ERROR] {}\n", e));
Some(1)
}
}
}
Commands::SubstrateRead { uri, hash } => {
let adapter = match clm_rust_core::substrate::parse_adapter(uri) {
Ok(a) => a,
Err(e) => {
stderr.push_str(&format!("[SUBSTRATE ERROR] {}\n", e));
return Some(1);
}
};
match adapter.read_by_hash(hash) {
Ok(bytes) => {
let content = String::from_utf8_lossy(&bytes);
stdout.push_str(&format!("[SUBSTRATE READ] Hash {} from {}:\n{}\n", hash, uri, content));
Some(0)
}
Err(e) => {
stderr.push_str(&format!("[SUBSTRATE READ ERROR] {}\n", e));
Some(1)
}
}
}
Commands::SubstrateList { uri } => {
let adapter = match clm_rust_core::substrate::parse_adapter(uri) {
Ok(a) => a,
Err(e) => {
stderr.push_str(&format!("[SUBSTRATE ERROR] {}\n", e));
return Some(1);
}
};
match adapter.list_hashes() {
Ok(hashes) => {
stdout.push_str(&format!("[SUBSTRATE LIST] Found {} hashes in {}:\n", hashes.len(), uri));
for h in &hashes {
stdout.push_str(&format!(" - {}\n", h));
}
Some(0)
}
Err(e) => {
stderr.push_str(&format!("[SUBSTRATE LIST ERROR] {}\n", e));
Some(1)
}
}
}
Commands::SubstrateMigrate { from, to, hash } => {
let src_adapter = match clm_rust_core::substrate::parse_adapter(from) {
Ok(a) => a,
Err(e) => {
stderr.push_str(&format!("[SUBSTRATE ERROR] Source: {}\n", e));
return Some(1);
}
};
let dst_adapter = match clm_rust_core::substrate::parse_adapter(to) {
Ok(a) => a,
Err(e) => {
stderr.push_str(&format!("[SUBSTRATE ERROR] Target: {}\n", e));
return Some(1);
}
};
match clm_rust_core::substrate::migrate_mcard(src_adapter.as_ref(), dst_adapter.as_ref(), hash) {
Ok(vcard) => {
stdout.push_str(&format!("[SUBSTRATE MIGRATE] Migrated hash {} from {} -> {}\n[SUBSTRATE PINNED VCARD] vcard_hash: {}\n", hash, from, to, vcard.vcard_hash));
Some(0)
}
Err(e) => {
stderr.push_str(&format!("[SUBSTRATE MIGRATE ERROR] {}\n", e));
Some(1)
}
}
}
_ => None,
}
}