diff --git a/ipfs/repo.nim b/ipfs/repo.nim index c82f87c8..eaefaeb2 100644 --- a/ipfs/repo.nim +++ b/ipfs/repo.nim @@ -1,8 +1,10 @@ +import std/options import std/tables import std/hashes import pkg/libp2p import ./ipfsobject +export options export ipfsobject type @@ -18,5 +20,8 @@ proc store*(repo: Repo, obj: IpfsObject) = proc contains*(repo: Repo, id: Cid): bool = repo.storage.hasKey(id) -proc retrieve*(repo: Repo, id: Cid): IpfsObject = - repo.storage[id] +proc retrieve*(repo: Repo, id: Cid): Option[IpfsObject] = + if repo.contains(id): + repo.storage[id].some + else: + IpfsObject.none diff --git a/tests/ipfs/testRepo.nim b/tests/ipfs/testRepo.nim index b54f5cef..a70f2962 100644 --- a/tests/ipfs/testRepo.nim +++ b/tests/ipfs/testRepo.nim @@ -14,7 +14,10 @@ suite "repo": test "retrieves IPFS objects by their content id": repo.store(obj) - check repo.retrieve(obj.cid) == obj + check repo.retrieve(obj.cid).get() == obj + + test "signals retrieval failure": + check repo.retrieve(obj.cid).isNone test "knows which content ids are stored": check repo.contains(obj.cid) == false