Skip to content

Commit 4b769e6

Browse files
author
Pat Hickey
authored
witx: expose iterator of all error types (#250)
1 parent 6d60cf2 commit 4b769e6

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

tools/witx/src/ast.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![allow(dead_code)]
2-
use std::collections::HashMap;
2+
use std::collections::{HashMap, HashSet};
33
use std::rc::{Rc, Weak};
44

55
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -39,6 +39,18 @@ impl Document {
3939
_ => None,
4040
})
4141
}
42+
/// All of the (unique) types used as the first result value of a function.
43+
pub fn error_types<'a>(&'a self) -> impl Iterator<Item = TypeRef> + 'a {
44+
let errors: HashSet<TypeRef> = self
45+
.modules()
46+
.flat_map(|m| {
47+
m.funcs()
48+
.filter_map(|f| f.results.get(0).as_ref().map(|r| r.tref.clone()))
49+
.collect::<HashSet<TypeRef>>()
50+
})
51+
.collect();
52+
errors.into_iter()
53+
}
4254
pub fn module(&self, name: &Id) -> Option<Rc<Module>> {
4355
self.entries.get(&name).and_then(|e| match e {
4456
Entry::Module(m) => Some(m.upgrade().expect("always possible to upgrade entry")),

0 commit comments

Comments
 (0)