Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return errors involving missing links instead of panicking #73

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ pub enum Error {
KeyNotFound(String),
#[error("Proof is missing data for query")]
MissingData,
#[error("Expected link")]
MissingLink,
#[error("Expected Some(Link::Reference)")]
MissingLinkReference,
#[error("Cannot traverse Link::Modified")]
ModifiedLinkTraversal,
#[error("Path Error: {0}")]
Path(String),
#[error("Proof Error: {0}")]
Expand Down
13 changes: 7 additions & 6 deletions src/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::cmp::max;

use ed::{Decode, Encode};

use super::error::Result;
use super::error::{Error, Result};
pub use commit::{Commit, NoopCommit};
pub use hash::{kv_hash, node_hash, Hash, Hasher, HASH_LENGTH, NULL_HASH};
use kv::KV;
Expand Down Expand Up @@ -380,16 +380,17 @@ impl Tree {
/// `Link::Loaded`).
#[inline]
pub fn load<S: Fetch>(&mut self, left: bool, source: &S) -> Result<()> {
// TODO: return Err instead of panic?
let link = self.link(left).expect("Expected link");
let link = self.link(left).ok_or(Error::MissingLink)?;
let (child_heights, hash) = match link {
Link::Reference {
child_heights,
hash,
..
} => (child_heights, hash),
_ => panic!("Expected Some(Link::Reference)"),
};
} => Ok((child_heights, hash)),
Link::Modified { .. } | Link::Uncommitted { .. } | Link::Loaded { .. } => {
Err(Error::MissingLinkReference)
}
}?;

let tree = source.fetch(link)?;
debug_assert_eq!(tree.key(), link.key());
Expand Down
13 changes: 5 additions & 8 deletions src/tree/walk/ref_walker.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::super::{Link, Tree};
use super::Fetch;
use crate::error::Result;
use crate::error::{Error::ModifiedLinkTraversal, Result};

/// Allows read-only traversal of a `Tree`, fetching from the given source when
/// traversing to a pruned node. The fetched nodes are then retained in memory
Expand All @@ -22,7 +22,6 @@ where
{
/// Creates a `RefWalker` with the given tree and source.
pub fn new(tree: &'a mut Tree, source: S) -> Self {
// TODO: check if tree has modified links, panic if so
RefWalker { tree, source }
}

Expand All @@ -41,12 +40,10 @@ where
};

match link {
Link::Reference { .. } => {
self.tree.load(left, &self.source)?;
}
Link::Modified { .. } => panic!("Cannot traverse Link::Modified"),
Link::Uncommitted { .. } | Link::Loaded { .. } => {}
}
Link::Reference { .. } => self.tree.load(left, &self.source),
Link::Modified { .. } => Err(ModifiedLinkTraversal),
Link::Uncommitted { .. } | Link::Loaded { .. } => Ok(()),
}?;

let child = self.tree.child_mut(left).unwrap();
Ok(Some(RefWalker::new(child, self.source.clone())))
Expand Down