use futures::TryStreamExt;
use napi::bindgen_prelude::*;
use napi_derive::napi;
use crate::{AuthorsClient, Iroh};
#[derive(Debug, Clone, PartialEq, Eq)]
#[napi]
pub struct AuthorId(pub(crate) iroh_docs::AuthorId);
#[napi]
impl AuthorId {
#[napi(factory)]
pub fn from_string(str: String) -> Result<Self> {
let author: iroh_docs::AuthorId = str.parse()?;
Ok(AuthorId(author))
}
#[napi]
pub fn is_equal(&self, other: &AuthorId) -> bool {
*self == *other
}
#[napi]
pub fn to_string(&self) -> String {
self.0.to_string()
}
}
#[derive(Debug, Clone)]
#[napi]
pub struct Author(pub(crate) iroh_docs::Author);
#[napi]
impl Author {
#[napi(factory)]
pub fn from_string(str: String) -> Result<Self> {
let author: iroh_docs::Author = str.parse()?;
Ok(Author(author))
}
#[napi]
pub fn id(&self) -> AuthorId {
AuthorId(self.0.id())
}
#[napi]
pub fn to_string(&self) -> String {
self.0.to_string()
}
}
#[napi]
pub struct Authors {
client: AuthorsClient,
}
#[napi]
impl Iroh {
#[napi(getter)]
pub fn authors(&self) -> Authors {
Authors {
client: self.authors_client.clone().expect("missing docs"),
}
}
}
#[napi]
impl Authors {
#[napi]
pub async fn default(&self) -> Result<AuthorId> {
let author = self.client.default().await?;
Ok(AuthorId(author))
}
#[napi]
pub async fn list(&self) -> Result<Vec<AuthorId>> {
let authors = self
.client
.list()
.await?
.map_ok(AuthorId)
.try_collect::<Vec<_>>()
.await?;
Ok(authors)
}
#[napi]
pub async fn create(&self) -> Result<AuthorId> {
let author = self.client.create().await?;
Ok(AuthorId(author))
}
#[napi]
pub async fn export(&self, author: &AuthorId) -> Result<Author> {
let author = self.client.export(author.0).await?;
match author {
Some(author) => Ok(Author(author)),
None => Err(anyhow::anyhow!("Author Not Found").into()),
}
}
#[napi]
pub async fn import(&self, author: &Author) -> Result<AuthorId> {
self.client.import(author.0.clone()).await?;
Ok(AuthorId(author.0.id()))
}
#[napi]
pub async fn delete(&self, author: &AuthorId) -> Result<()> {
self.client.delete(author.0).await?;
Ok(())
}
}