Skip to content
This repository was archived by the owner on Oct 23, 2022. It is now read-only.

Commit c6952cb

Browse files
committed
feat: make IpldDag an async_trait
Signed-off-by: ljedrz <[email protected]>
1 parent 6b6ddcd commit c6952cb

File tree

3 files changed

+24
-30
lines changed

3 files changed

+24
-30
lines changed

src/dag.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
use crate::error::Error;
22
use crate::path::{IpfsPath, IpfsPathError, SubPath};
3-
use crate::repo::RepoTypes;
4-
use crate::Ipfs;
3+
use crate::repo::{Repo, RepoTypes};
4+
use async_trait::async_trait;
55
use bitswap::Block;
66
use cid::{Cid, Codec, Version};
77
use libipld::block::{decode_ipld, encode_ipld};
88
use libipld::ipld::Ipld;
99

10-
#[derive(Clone, Debug)]
11-
pub struct IpldDag<Types: RepoTypes> {
12-
ipfs: Ipfs<Types>,
13-
}
10+
#[async_trait]
11+
pub trait IpldDag {
12+
async fn put_dag(&self, data: Ipld, codec: Codec) -> Result<Cid, Error>;
1413

15-
impl<Types: RepoTypes> IpldDag<Types> {
16-
pub fn new(ipfs: Ipfs<Types>) -> Self {
17-
IpldDag { ipfs }
18-
}
14+
async fn get_dag(&self, path: IpfsPath) -> Result<Ipld, Error>;
15+
}
1916

20-
pub async fn put(&self, data: Ipld, codec: Codec) -> Result<Cid, Error> {
17+
#[async_trait]
18+
impl<T: RepoTypes> IpldDag for Repo<T> {
19+
async fn put_dag(&self, data: Ipld, codec: Codec) -> Result<Cid, Error> {
2120
let bytes = encode_ipld(&data, codec)?;
2221
let hash = multihash::Sha2_256::digest(&bytes);
2322
let version = if codec == Codec::DagProtobuf {
@@ -27,24 +26,24 @@ impl<Types: RepoTypes> IpldDag<Types> {
2726
};
2827
let cid = Cid::new(version, codec, hash)?;
2928
let block = Block::new(bytes, cid);
30-
let (cid, _) = self.ipfs.repo.put_block(block).await?;
29+
let (cid, _) = self.put_block(block).await?;
3130
Ok(cid)
3231
}
3332

34-
pub async fn get(&self, path: IpfsPath) -> Result<Ipld, Error> {
33+
async fn get_dag(&self, path: IpfsPath) -> Result<Ipld, Error> {
3534
let cid = match path.root().cid() {
3635
Some(cid) => cid,
3736
None => return Err(anyhow::anyhow!("expected cid")),
3837
};
39-
let mut ipld = decode_ipld(&cid, self.ipfs.repo.get_block(&cid).await?.data())?;
38+
let mut ipld = decode_ipld(&cid, self.get_block(&cid).await?.data())?;
4039
for sub_path in path.iter() {
4140
if !can_resolve(&ipld, sub_path) {
4241
let path = sub_path.to_owned();
4342
return Err(IpfsPathError::ResolveError { ipld, path }.into());
4443
}
4544
ipld = resolve(ipld, sub_path);
4645
ipld = match ipld {
47-
Ipld::Link(cid) => decode_ipld(&cid, self.ipfs.repo.get_block(&cid).await?.data())?,
46+
Ipld::Link(cid) => decode_ipld(&cid, self.get_block(&cid).await?.data())?,
4847
ipld => ipld,
4948
};
5049
}

src/lib.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -350,10 +350,6 @@ impl<Types: IpfsTypes> std::ops::Deref for Ipfs<Types> {
350350
}
351351

352352
impl<Types: IpfsTypes> Ipfs<Types> {
353-
fn dag(&self) -> IpldDag<Types> {
354-
IpldDag::new(self.clone())
355-
}
356-
357353
fn ipns(&self) -> Ipns<Types> {
358354
Ipns::new(self.clone())
359355
}
@@ -401,31 +397,30 @@ impl<Types: IpfsTypes> Ipfs<Types> {
401397

402398
/// Puts an ipld dag node into the ipfs repo.
403399
pub async fn put_dag(&self, ipld: Ipld) -> Result<Cid, Error> {
404-
self.dag()
405-
.put(ipld, Codec::DagCBOR)
400+
self.repo
401+
.put_dag(ipld, Codec::DagCBOR)
406402
.instrument(self.span.clone())
407403
.await
408404
}
409405

410406
/// Gets an ipld dag node from the ipfs repo.
411407
pub async fn get_dag(&self, path: IpfsPath) -> Result<Ipld, Error> {
412-
self.dag().get(path).instrument(self.span.clone()).await
408+
self.repo.get_dag(path).instrument(self.span.clone()).await
413409
}
414410

415411
/// Adds a file into the ipfs repo.
416412
pub async fn add(&self, path: PathBuf) -> Result<Cid, Error> {
417-
let dag = self.dag();
418413
let file = File::new(path).await?;
419414
let path = file
420-
.put_unixfs_v1(&dag)
415+
.put_unixfs_v1(&self.repo)
421416
.instrument(self.span.clone())
422417
.await?;
423418
Ok(path)
424419
}
425420

426421
/// Gets a file from the ipfs repo.
427422
pub async fn get(&self, path: IpfsPath) -> Result<File, Error> {
428-
File::get_unixfs_v1(&self.dag(), path)
423+
File::get_unixfs_v1(&self.repo, path)
429424
.instrument(self.span.clone())
430425
.await
431426
}

src/unixfs/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::dag::IpldDag;
22
use crate::error::Error;
33
use crate::path::IpfsPath;
4-
use crate::repo::RepoTypes;
4+
use crate::repo::{Repo, RepoTypes};
55
use async_std::fs;
66
use async_std::io::ReadExt;
77
use async_std::path::PathBuf;
@@ -31,20 +31,20 @@ impl File {
3131
}
3232

3333
pub async fn get_unixfs_v1<T: RepoTypes>(
34-
dag: &IpldDag<T>,
34+
repo: &Repo<T>,
3535
path: IpfsPath,
3636
) -> Result<Self, Error> {
37-
let ipld = dag.get(path).await?;
37+
let ipld = repo.get_dag(path).await?;
3838
let pb_node: PbNode = (&ipld).try_into()?;
3939
Ok(File { data: pb_node.data })
4040
}
4141

42-
pub async fn put_unixfs_v1<T: RepoTypes>(&self, dag: &IpldDag<T>) -> Result<Cid, Error> {
42+
pub async fn put_unixfs_v1<T: RepoTypes>(&self, repo: &Repo<T>) -> Result<Cid, Error> {
4343
let links: Vec<Ipld> = vec![];
4444
let mut pb_node = BTreeMap::<String, Ipld>::new();
4545
pb_node.insert("Data".to_string(), self.data.clone().into());
4646
pb_node.insert("Links".to_string(), links.into());
47-
dag.put(pb_node.into(), Codec::DagProtobuf).await
47+
repo.put_dag(pb_node.into(), Codec::DagProtobuf).await
4848
}
4949
}
5050

0 commit comments

Comments
 (0)