use bytes::Bytes;
use futures::TryStreamExt;
use napi::bindgen_prelude::*;
use napi_derive::napi;
use crate::{BlobFormat, Iroh, TagsClient};
#[derive(Debug)]
#[napi]
pub struct TagInfo {
pub name: Vec<u8>,
pub format: BlobFormat,
pub hash: String,
}
impl From<iroh_blobs::rpc::client::tags::TagInfo> for TagInfo {
fn from(res: iroh_blobs::rpc::client::tags::TagInfo) -> Self {
TagInfo {
name: res.name.0.to_vec(),
format: res.format.into(),
hash: res.hash.to_string(),
}
}
}
#[napi]
pub struct Tags {
client: TagsClient,
}
#[napi]
impl Iroh {
pub fn tags(&self) -> Tags {
Tags {
client: self.tags_client.clone(),
}
}
}
#[napi]
impl Tags {
#[napi]
pub async fn list(&self) -> Result<Vec<TagInfo>> {
let tags = self
.client
.list()
.await?
.map_ok(|l| l.into())
.try_collect::<Vec<_>>()
.await?;
Ok(tags)
}
#[napi]
pub async fn delete(&self, name: Vec<u8>) -> Result<()> {
let tag = iroh_blobs::Tag(Bytes::from(name));
self.client.delete(tag).await?;
Ok(())
}
}