Skip to content

Commit

Permalink
Graph Artifact instantiation now minimally works
Browse files Browse the repository at this point in the history
  • Loading branch information
dnorman committed Dec 9, 2020
1 parent 76a2d87 commit 9a29f78
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
8 changes: 8 additions & 0 deletions crates/graph/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[derive(Debug)]
pub enum Error {
Store(mindbase_store::Error),
}
Expand All @@ -7,3 +8,10 @@ impl From<mindbase_store::Error> for Error {
Self::Store(e)
}
}

impl std::convert::From<Error> for std::io::Error {
fn from(error: Error) -> Self {
use std::io::ErrorKind;
std::io::Error::new(ErrorKind::Other, format!("{:?}", error))
}
}
22 changes: 11 additions & 11 deletions crates/graph/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ pub mod traits;
// use traits::NodeInstance;

#[derive(Default, Debug)]
struct Graph<S, A, I>
pub struct Graph<S, A, I>
where
S: Store,
A: traits::Artifact,
I: traits::ArtifactInstance,
I: traits::ArtifactInstance<A>,
{
_store: S,
artifacts: S::Tree,
Expand All @@ -35,9 +35,9 @@ impl<S, A, I> Graph<S, A, I>
where
S: Store,
A: traits::Artifact,
I: traits::ArtifactInstance,
I: traits::ArtifactInstance<A>,
{
fn new(store: S) -> Result<Self, Error> {
pub fn new(store: S) -> Result<Self, Error> {
let artifacts = store.open_tree("graph::artifacts")?;

fn write_once(
Expand All @@ -63,20 +63,20 @@ where
_i: PhantomData,
})
}
fn put_artifact<T: Into<A>>(&mut self, artifact: T) -> Result<A::ID, Error> {
pub fn put_artifact<T: Into<A>>(&mut self, artifact: T) -> Result<I, Error> {
let artifact: A = artifact.into();

let artifact_id = artifact.id();
let (artifact_id, bytes) = artifact.get_id_and_bytes();
// Only store it if we haven't seen this one before
let instance = I::instantiate(&artifact_id);

self.artifacts.merge(artifact_id, artifact);
self.artifacts.merge(artifact_id, bytes)?;

// either way we want to create an instance

// LEFT OFF HERE
// let instance = A::new(artifact_id);
// self.instances.insert(instance.id, instance.clone());
let (instance_id, bytes) = instance.get_id_and_bytes();
self.instances.insert(instance_id.as_ref(), bytes)?;

instance
Ok(instance)
}
}
15 changes: 11 additions & 4 deletions crates/graph/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
use serde::Serialize;
pub trait Artifact: Serialize + AsRef<[u8]> {
type ID: ArtifactInstance;
pub trait Artifact {
type ID: AsRef<[u8]>;
fn id(&self) -> Self::ID;
fn get_id_and_bytes(&self) -> (Self::ID, Vec<u8>);
}
pub trait ArtifactInstance<A: Artifact>: Clone + std::fmt::Display + std::cmp::Ord {
type ID: AsRef<[u8]>;

fn instantiate(artifact: &A::ID) -> Self;
fn id(&self) -> Self::ID;
fn get_id_and_bytes(&self) -> (Self::ID, Vec<u8>);
fn from_id_and_bytes<B: AsRef<u8>>(id: Self::ID, bytes: B);
}
pub trait ArtifactInstance: Clone + std::fmt::Display + std::cmp::Ord + Serialize + AsRef<[u8]> {}

0 comments on commit 9a29f78

Please sign in to comment.