Add ObjectTree::expect for convenience

This commit is contained in:
Tad Hardesty 2019-02-20 13:29:38 -08:00
parent a9538cc4a6
commit 7383da9ab3
3 changed files with 14 additions and 7 deletions

View file

@ -36,10 +36,10 @@ impl<'o> Type<'o> {
Constant::Float(_) => Type::Number,
Constant::List(_) => Type::List(None),
Constant::Call(func, _) => match func {
ConstFn::Icon => Type::Instance(objtree.find("/icon").unwrap()),
ConstFn::Matrix => Type::Instance(objtree.find("/matrix").unwrap()),
ConstFn::Icon => Type::Instance(objtree.expect("/icon")),
ConstFn::Matrix => Type::Instance(objtree.expect("/matrix")),
ConstFn::Newlist => Type::List(None),
ConstFn::Sound => Type::Instance(objtree.find("/sound").unwrap()),
ConstFn::Sound => Type::Instance(objtree.expect("/sound")),
},
// TODO: New => Instance, Prefab => Typepath
_ => Type::Any,
@ -108,7 +108,7 @@ impl<'o> ProcAnalyzer<'o> {
let mut local_vars = HashMap::new();
local_vars.insert(".".to_owned(), Analysis::empty());
local_vars.insert("args".to_owned(), Type::List(None).into());
local_vars.insert("usr".to_owned(), Analysis::from_static_type(objtree.find("/mob").unwrap()));
local_vars.insert("usr".to_owned(), Analysis::from_static_type(objtree.expect("/mob")));
if !ty.is_root() {
local_vars.insert("src".to_owned(), Analysis::from_static_type(ty));
}
@ -455,7 +455,7 @@ impl<'o> ProcAnalyzer<'o> {
if of.is_empty() {
Analysis::empty()
} else if of[0] == "list" {
let mut analysis = Analysis::from_static_type(self.objtree.find("/list").unwrap());
let mut analysis = Analysis::from_static_type(self.objtree.expect("/list"));
analysis.ty = Type::List(self.objtree.type_by_path(&of[1..]));
analysis
} else if let Some(ty) = self.objtree.type_by_path(of) {

View file

@ -12,7 +12,7 @@ fn main() {
// Used to check https://github.com/tgstation/tgstation/pull/38171
// for mistakes transferring between `flags_1` and `item_flags`.
println!("---- item_flags example ----");
objtree.find("/obj/item").expect("no root").recurse(&mut |ty| {
objtree.expect("/obj/item").recurse(&mut |ty| {
print!("{}: ", ty.path);
let mut flags_1 = ty
.get_value("flags_1")
@ -63,7 +63,7 @@ fn main() {
// Used to check https://github.com/tgstation/tgstation/pull/38116
// for changes to any machinery types's `anchored` value, and to find
// machinery for which `anchored = TRUE` was then redundant.
objtree.find("/obj/machinery").expect("no root").recurse(&mut |ty| {
objtree.expect("/obj/machinery").recurse(&mut |ty| {
// print every type's `anchored` value for diffing
let var = ty.get_value("anchored").unwrap();
let anch = var.constant.as_ref().unwrap().to_bool();

View file

@ -395,6 +395,13 @@ impl ObjectTree {
self.types.get(path).map(|&ix| TypeRef::new(self, ix))
}
pub fn expect(&self, path: &str) -> TypeRef {
match self.types.get(path) {
Some(&ix) => TypeRef::new(self, ix),
None => panic!("type not found: {:?}", path),
}
}
pub fn parent_of(&self, type_: &Type) -> Option<&Type> {
self.graph.node_weight(type_.parent_type)
}