Skip to content

Commit

Permalink
fix: Encode opaque symbolic constants (#273)
Browse files Browse the repository at this point in the history
Opaque ops weren't being decoded.
  • Loading branch information
aborgna-q authored Dec 15, 2023
1 parent cf43c0f commit 9332b6e
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions tket2/src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
// 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 {
Expand Down

0 comments on commit 9332b6e

Please sign in to comment.