Skip to content

Commit

Permalink
Add fallible build_recexpr
Browse files Browse the repository at this point in the history
  • Loading branch information
mwillsey committed Dec 16, 2021
1 parent 2d8657f commit cfdce7f
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,15 @@ pub trait Language: Debug + Clone + Eq + Ord + Hash {
fn build_recexpr<F>(&self, mut get_node: F) -> RecExpr<Self>
where
F: FnMut(Id) -> Self,
{
self.try_build_recexpr::<_, std::convert::Infallible>(|id| Ok(get_node(id)))
.unwrap()
}

/// Same as [`Language::build_recexpr`], but fallible.
fn try_build_recexpr<F, Err>(&self, mut get_node: F) -> Result<RecExpr<Self>, Err>
where
F: FnMut(Id) -> Result<Self, Err>,
{
let mut set = IndexSet::<Self>::default();
let mut ids = HashMap::<Id, Id>::default();
Expand All @@ -174,7 +183,7 @@ pub trait Language: Debug + Clone + Eq + Ord + Hash {
continue;
}

let node = get_node(id);
let node = get_node(id)?;

// check to see if we can do this node yet
let mut ids_has_all_children = true;
Expand All @@ -197,7 +206,7 @@ pub trait Language: Debug + Clone + Eq + Ord + Hash {
// finally, add the root node and create the expression
let mut nodes: Vec<Self> = set.into_iter().collect();
nodes.push(self.clone().map_children(|id| ids[&id]));
RecExpr::from(nodes)
Ok(RecExpr::from(nodes))
}
}

Expand Down

0 comments on commit cfdce7f

Please sign in to comment.