use std::sync::Arc;
use crate::{BlobFormat, Hash, Iroh, IrohError, TagsClient};
use bytes::Bytes;
use futures::TryStreamExt;
#[derive(Debug, uniffi::Record)]
pub struct TagInfo {
pub name: Vec<u8>,
pub format: BlobFormat,
pub hash: Arc<Hash>,
}
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: Arc::new(res.hash.into()),
}
}
}
#[derive(uniffi::Object)]
pub struct Tags {
client: TagsClient,
}
#[uniffi::export]
impl Iroh {
pub fn tags(&self) -> Tags {
Tags {
client: self.tags_client.clone(),
}
}
}
#[uniffi::export]
impl Tags {
#[uniffi::method(async_runtime = "tokio")]
pub async fn list(&self) -> Result<Vec<TagInfo>, IrohError> {
let tags = self
.client
.list()
.await?
.map_ok(|l| l.into())
.try_collect::<Vec<_>>()
.await?;
Ok(tags)
}
#[uniffi::method(async_runtime = "tokio")]
pub async fn delete(&self, name: Vec<u8>) -> Result<(), IrohError> {
let tag = iroh_blobs::Tag(Bytes::from(name));
self.client.delete(tag).await?;
Ok(())
}
}