use std::sync::Arc;
use bee::manifest::{MantarayNode, unmarshal};
use bee::swarm::Reference;
use crate::api::ApiClient;
#[derive(Debug, Clone)]
pub enum InspectResult {
Manifest { node: Box<MantarayNode>, bytes_len: usize },
RawChunk { bytes_len: usize },
Error(String),
}
pub async fn load_node(
api: Arc<ApiClient>,
reference: Reference,
) -> Result<MantarayNode, String> {
let bytes = api
.bee()
.file()
.download_chunk(&reference, None)
.await
.map_err(|e| format!("download_chunk: {e}"))?;
unmarshal(&bytes, reference.as_bytes()).map_err(|e| format!("unmarshal: {e}"))
}
pub async fn inspect(api: Arc<ApiClient>, reference: Reference) -> InspectResult {
let bytes = match api.bee().file().download_chunk(&reference, None).await {
Ok(b) => b,
Err(e) => return InspectResult::Error(format!("download_chunk: {e}")),
};
let len = bytes.len();
match unmarshal(&bytes, reference.as_bytes()) {
Ok(node) => InspectResult::Manifest {
node: Box::new(node),
bytes_len: len,
},
Err(_) => InspectResult::RawChunk { bytes_len: len },
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn inspect_result_variants_compile_and_clone() {
let raw = InspectResult::RawChunk { bytes_len: 4096 };
let raw2 = raw.clone();
match raw2 {
InspectResult::RawChunk { bytes_len } => assert_eq!(bytes_len, 4096),
_ => panic!("variant mismatch"),
}
let err = InspectResult::Error("net fail".into());
let err2 = err.clone();
match err2 {
InspectResult::Error(s) => assert_eq!(s, "net fail"),
_ => panic!("variant mismatch"),
}
}
}