use anyhow::Result;
use cid::Cid;
use crate::{platform::PlatformSphereChannel, wasm::SphereFs};
use noosphere_core::context::SphereCursor;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub struct SphereContext {
#[wasm_bindgen(skip)]
pub inner: PlatformSphereChannel,
}
#[wasm_bindgen]
impl SphereContext {
#[wasm_bindgen]
pub async fn fs(&mut self) -> Result<SphereFs, String> {
Ok(SphereFs {
inner: SphereCursor::latest(self.inner.mutable().clone()),
})
}
#[wasm_bindgen(js_name = "fsAt")]
pub async fn fs_at(&mut self, version: String) -> Result<SphereFs, String> {
let cid = Cid::try_from(version).map_err(|error| format!("{:?}", error))?;
Ok(SphereFs {
inner: SphereCursor::mounted_at(self.inner.mutable().clone(), &cid.into()),
})
}
}