use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub struct AddResult {
cid: String,
bytes_written: usize,
}
#[wasm_bindgen]
impl AddResult {
#[wasm_bindgen(constructor)]
pub fn new(cid: String, bytes_written: usize) -> Self {
Self { cid, bytes_written }
}
#[wasm_bindgen(getter)]
pub fn cid(&self) -> String {
self.cid.clone()
}
#[wasm_bindgen(getter)]
pub fn bytes_written(&self) -> usize {
self.bytes_written
}
}
#[wasm_bindgen]
pub struct GetResult {
is_found: bool,
data: Vec<u8>,
cid: String,
}
#[wasm_bindgen]
impl GetResult {
pub fn not_found(cid: String) -> Self {
Self {
is_found: false,
data: vec![],
cid,
}
}
#[wasm_bindgen(getter)]
pub fn found(&self) -> bool {
self.is_found
}
pub fn data(&self) -> Vec<u8> {
self.data.clone()
}
#[wasm_bindgen(getter)]
pub fn cid(&self) -> String {
self.cid.clone()
}
}
impl GetResult {
pub fn new_found(cid: impl Into<String>, data: Vec<u8>) -> Self {
Self {
is_found: true,
data,
cid: cid.into(),
}
}
pub fn new_not_found(cid: impl Into<String>) -> Self {
Self {
is_found: false,
data: vec![],
cid: cid.into(),
}
}
}
#[wasm_bindgen]
pub struct BatchStats {
total: usize,
succeeded: usize,
failed: usize,
}
#[wasm_bindgen]
impl BatchStats {
#[wasm_bindgen(constructor)]
pub fn new(total: usize, succeeded: usize, failed: usize) -> Self {
Self {
total,
succeeded,
failed,
}
}
#[wasm_bindgen(getter)]
pub fn total(&self) -> usize {
self.total
}
#[wasm_bindgen(getter)]
pub fn succeeded(&self) -> usize {
self.succeeded
}
#[wasm_bindgen(getter)]
pub fn failed(&self) -> usize {
self.failed
}
pub fn success_rate(&self) -> f64 {
if self.total == 0 {
0.0
} else {
self.succeeded as f64 / self.total as f64
}
}
}
fn fnv1a_u64(data: &[u8]) -> u64 {
let mut h: u64 = 14_695_981_039_346_656_037;
for &b in data {
h ^= b as u64;
h = h.wrapping_mul(1_099_511_628_211);
}
h
}
pub fn compute_cid_for_bytes(data: &[u8]) -> String {
let hash = fnv1a_u64(data);
format!("baf{hash:016x}")
}
pub fn verify_cid_for_bytes(cid: &str, data: &[u8]) -> bool {
compute_cid_for_bytes(data) == cid
}
#[cfg(all(test, not(target_arch = "wasm32")))]
mod tests {
use super::*;
#[test]
fn test_add_result_getters() {
let r = AddResult::new("bafabc123".to_string(), 42usize);
assert_eq!(r.cid(), "bafabc123");
assert_eq!(r.bytes_written(), 42);
}
#[test]
fn test_get_result_found() {
let payload = vec![1u8, 2, 3];
let r = GetResult::new_found("bafxyz", payload.clone());
assert!(r.found());
assert_eq!(r.data(), payload);
assert_eq!(r.cid(), "bafxyz");
}
#[test]
fn test_get_result_not_found() {
let r = GetResult::new_not_found("bafmissing");
assert!(!r.found());
assert!(r.data().is_empty());
assert_eq!(r.cid(), "bafmissing");
}
#[test]
fn test_batch_stats_success_rate() {
let s = BatchStats::new(10, 7, 3);
assert_eq!(s.total(), 10);
assert_eq!(s.succeeded(), 7);
assert_eq!(s.failed(), 3);
let rate = s.success_rate();
assert!(
(rate - 0.7f64).abs() < f64::EPSILON,
"expected 0.7, got {rate}"
);
}
#[test]
fn test_batch_stats_zero_total() {
let s = BatchStats::new(0, 0, 0);
assert_eq!(s.success_rate(), 0.0);
}
#[test]
fn test_compute_cid_deterministic() {
let a = compute_cid_for_bytes(b"same input");
let b = compute_cid_for_bytes(b"same input");
assert_eq!(a, b, "same input must always produce the same CID");
}
#[test]
fn test_compute_cid_different_inputs() {
let a = compute_cid_for_bytes(b"alpha data");
let b = compute_cid_for_bytes(b"beta data");
assert_ne!(a, b, "different inputs must produce different CIDs");
}
#[test]
fn test_compute_cid_prefix() {
let cid = compute_cid_for_bytes(b"prefix check");
assert!(
cid.starts_with("baf"),
"CID must start with 'baf', got: {cid}"
);
assert_eq!(cid.len(), 19, "unexpected CID length: {}", cid.len());
}
#[test]
fn test_verify_cid_correct() {
let data = b"verify correct";
let cid = compute_cid_for_bytes(data);
assert!(
verify_cid_for_bytes(&cid, data),
"verify_cid_for_bytes must return true for matching CID"
);
}
#[test]
fn test_verify_cid_wrong() {
let data = b"verify wrong";
let cid = compute_cid_for_bytes(data);
assert!(
!verify_cid_for_bytes(&cid, b"different payload"),
"verify_cid_for_bytes must return false for non-matching data"
);
assert!(
!verify_cid_for_bytes("baf0000000000000000", data),
"verify_cid_for_bytes must return false for wrong CID"
);
}
}