summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Cargo.toml9
-rw-r--r--src/backend/backend.rs28
-rw-r--r--src/backend/id.rs16
-rw-r--r--src/backend/mod.rs8
-rw-r--r--src/backend/node.rs17
-rw-r--r--src/main.rs8
6 files changed, 86 insertions, 0 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 6e866a8..b924dd5 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -16,3 +16,12 @@ homepage = "http://github.com/matthiasbeyer/distrox"
edition = "2018"
[dependencies]
+# TODO: Replace with thiserror
+anyhow = "1"
+
+async-trait = "0.1"
+cid = "0.6"
+daglib = { git = "https://git.sr.ht/~matthiasbeyer/daglib", branch = "master" }
+ipfs-embed = "0.19"
+libipld = "0.11"
+tokio = { version = "1", features = ["full"] }
diff --git a/src/backend/backend.rs b/src/backend/backend.rs
new file mode 100644
index 0000000..5aaf970
--- /dev/null
+++ b/src/backend/backend.rs
@@ -0,0 +1,28 @@
+use anyhow::Result;
+
+use crate::backend::Id;
+use crate::backend::Node;
+
+pub struct IpfsEmbedBackend {
+ ipfs: ipfs_embed::Ipfs<ipfs_embed::DefaultParams>,
+}
+
+#[async_trait::async_trait]
+impl daglib::DagBackend<Id, Node> for IpfsEmbedBackend {
+
+ async fn get(&self, id: Id) -> Result<Option<(Id, Node)>> {
+ let block = self.ipfs.fetch(id.as_ref()).await?;
+ let node = block.decode::<libipld::cbor::DagCborCodec, crate::backend::Node>()?;
+ Ok(Some((id, node)))
+ }
+
+ async fn put(&mut self, node: Node) -> Result<Id> {
+ let block = libipld::block::Block::encode(libipld::cbor::DagCborCodec, libipld::multihash::Code::Blake3_256, &node)?;
+ let cid = Id::from(block.cid().clone());
+ self.ipfs
+ .insert(&block)?
+ .await
+ .map(|_| cid)
+ }
+}
+
diff --git a/src/backend/id.rs b/src/backend/id.rs
new file mode 100644
index 0000000..5e5e9d3
--- /dev/null
+++ b/src/backend/id.rs
@@ -0,0 +1,16 @@
+#[derive(Clone, Debug, Eq, PartialEq, libipld::DagCbor)]
+pub struct Id(cid::Cid);
+
+impl daglib::NodeId for Id { }
+
+impl From<cid::Cid> for Id {
+ fn from(cid: cid::Cid) -> Self {
+ Id(cid)
+ }
+}
+
+impl AsRef<cid::Cid> for Id {
+ fn as_ref(&self) -> &cid::Cid {
+ &self.0
+ }
+}
diff --git a/src/backend/mod.rs b/src/backend/mod.rs
new file mode 100644
index 0000000..24f907c
--- /dev/null
+++ b/src/backend/mod.rs
@@ -0,0 +1,8 @@
+mod id;
+pub use id::*;
+
+mod node;
+pub use node::*;
+
+mod backend;
+pub use backend::*;
diff --git a/src/backend/node.rs b/src/backend/node.rs
new file mode 100644
index 0000000..c819d3e
--- /dev/null
+++ b/src/backend/node.rs
@@ -0,0 +1,17 @@
+#[derive(Debug, Eq, PartialEq, libipld::DagCbor)]
+pub struct Node {
+ /// Version
+ v: String,
+
+ /// Parent Nodes, identified by cid
+ parents: Vec<crate::backend::Id>,
+}
+
+impl daglib::Node for Node {
+ type Id = crate::backend::Id;
+
+ fn parent_ids(&self) -> Vec<Self::Id> {
+ self.parents.clone()
+ }
+}
+
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..ee348a8
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,8 @@
+use anyhow::Result;
+
+mod backend;
+
+#[tokio::main]
+async fn main() -> Result<()> {
+ Ok(())
+}