diff --git a/tket2/src/ops.rs b/tket2/src/ops.rs index 9f76820a..1bb8d067 100644 --- a/tket2/src/ops.rs +++ b/tket2/src/ops.rs @@ -183,25 +183,30 @@ pub fn symbolic_constant_op(s: &str) -> OpType { } /// match against a symbolic constant -pub(crate) fn match_symb_const_op(op: &OpType) -> Option<&str> { +pub(crate) fn match_symb_const_op(op: &OpType) -> Option { + // Extract the symbol for a symbolic operation node. + let symbol_from_typeargs = |args: &[TypeArg]| -> String { + args.first() + .and_then(|arg| match arg { + TypeArg::Opaque { arg } => match &arg.value { + serde_yaml::Value::String(s) => Some(s.clone()), + _ => None, + }, + _ => None, + }) + .unwrap_or_else(|| panic!("Found an invalid type arg in a symbolic operation node.")) + }; + if let OpType::LeafOp(LeafOp::CustomOp(e)) = op { match e.as_ref() { ExternalOp::Extension(e) if e.def().name() == &SYM_OP_ID && e.def().extension() == &EXTENSION_ID => { - // TODO also check extension name - - let Some(TypeArg::Opaque { arg }) = e.args().first() else { - panic!("should be an opaque type arg.") - }; - - let serde_yaml::Value::String(s) = &arg.value else { - panic!("unexpected yaml value.") - }; - - Some(s) + Some(symbol_from_typeargs(e.args())) + } + ExternalOp::Opaque(e) if e.name() == &SYM_OP_ID && e.extension() == &EXTENSION_ID => { + Some(symbol_from_typeargs(e.args())) } - ExternalOp::Opaque(_) => todo!(), _ => None, } } else {