-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: extract serialisation in to module and add test
- Loading branch information
Showing
4 changed files
with
187 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
hugr-core/src/extension/op_def/serialize_signature_func.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use super::{PolyFuncTypeRV, SignatureFunc}; | ||
#[derive(serde::Deserialize, serde::Serialize, PartialEq, Debug)] | ||
struct SerSignatureFunc { | ||
/// If the type scheme is available explicitly, store it. | ||
signature: Option<PolyFuncTypeRV>, | ||
/// Whether an associated binary function is expected. | ||
/// If `signature` is `None`, a true value here indicates a custom compute function. | ||
/// If `signature` is not `None`, a true value here indicates a custom validation function. | ||
binary: bool, | ||
} | ||
|
||
pub(super) fn serialize<S>(value: &super::SignatureFunc, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: serde::Serializer, | ||
{ | ||
match value { | ||
SignatureFunc::PolyFuncType(custom) => SerSignatureFunc { | ||
signature: Some(custom.poly_func.clone()), | ||
binary: custom.validate.is_some(), | ||
}, | ||
SignatureFunc::MissingValidateFunc(poly_func) => SerSignatureFunc { | ||
signature: Some(poly_func.clone()), | ||
binary: true, | ||
}, | ||
SignatureFunc::CustomFunc(_) => SerSignatureFunc { | ||
signature: None, | ||
binary: true, | ||
}, | ||
SignatureFunc::MissingComputeFunc => SerSignatureFunc { | ||
signature: None, | ||
binary: false, | ||
}, | ||
} | ||
.serialize(serializer) | ||
} | ||
|
||
pub(super) fn deserialize<'de, D>(deserializer: D) -> Result<super::SignatureFunc, D::Error> | ||
where | ||
D: serde::Deserializer<'de>, | ||
{ | ||
let SerSignatureFunc { signature, binary } = SerSignatureFunc::deserialize(deserializer)?; | ||
|
||
match (signature, binary) { | ||
(Some(sig), false) => Ok(sig.into()), | ||
(Some(sig), true) => Ok(SignatureFunc::MissingValidateFunc(sig)), | ||
(None, true) => Ok(SignatureFunc::MissingComputeFunc), | ||
(None, false) => Err(serde::de::Error::custom( | ||
"No signature provided and custom computation not expected.", | ||
)), | ||
} | ||
} | ||
#[derive(serde::Deserialize, serde::Serialize, Debug)] | ||
/// Wrapper we can derive serde for, to allow round-trip serialization | ||
struct Wrapper { | ||
#[serde( | ||
serialize_with = "serialize", | ||
deserialize_with = "deserialize", | ||
flatten | ||
)] | ||
inner: SignatureFunc, | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use cool_asserts::assert_matches; | ||
|
||
use super::*; | ||
use crate::{ | ||
extension::{op_def::NoValidate, prelude::USIZE_T, CustomSignatureFunc, CustomValidator}, | ||
types::{FuncValueType, Signature, TypeArg}, | ||
}; | ||
// Define test-only conversions via serialization roundtrip | ||
impl From<SerSignatureFunc> for SignatureFunc { | ||
fn from(value: SerSignatureFunc) -> Self { | ||
let ser = serde_json::to_value(value).unwrap(); | ||
let w: Wrapper = serde_json::from_value(ser).unwrap(); | ||
w.inner | ||
} | ||
} | ||
|
||
impl From<SignatureFunc> for SerSignatureFunc { | ||
fn from(value: SignatureFunc) -> Self { | ||
let ser = serde_json::to_value(Wrapper { inner: value }).unwrap(); | ||
serde_json::from_value(ser).unwrap() | ||
} | ||
} | ||
struct CustomSig; | ||
|
||
impl CustomSignatureFunc for CustomSig { | ||
fn compute_signature<'o, 'a: 'o>( | ||
&'a self, | ||
_arg_values: &[TypeArg], | ||
_def: &'o crate::extension::op_def::OpDef, | ||
_extension_registry: &crate::extension::ExtensionRegistry, | ||
) -> Result<crate::types::PolyFuncTypeRV, crate::extension::SignatureError> { | ||
Ok(Default::default()) | ||
} | ||
|
||
fn static_params(&self) -> &[crate::types::type_param::TypeParam] { | ||
&[] | ||
} | ||
} | ||
#[test] | ||
fn test_serial_sig_func() { | ||
// test round-trip | ||
let sig: FuncValueType = Signature::new_endo(USIZE_T.clone()).into(); | ||
let simple: SignatureFunc = sig.clone().into(); | ||
let ser: SerSignatureFunc = simple.into(); | ||
|
||
assert_eq!( | ||
ser, | ||
SerSignatureFunc { | ||
signature: Some(sig.clone().into()), | ||
binary: false | ||
} | ||
); | ||
let deser = SignatureFunc::from(ser); | ||
assert_matches!( deser, | ||
SignatureFunc::PolyFuncType(CustomValidator { | ||
poly_func, | ||
validate, | ||
}) => { | ||
assert_eq!(poly_func, sig.clone().into()); | ||
assert!(validate.is_none()); | ||
}); | ||
let with_custom: SignatureFunc = | ||
CustomValidator::new_with_validator(sig.clone(), NoValidate).into(); | ||
let ser: SerSignatureFunc = with_custom.into(); | ||
assert_eq!( | ||
ser, | ||
SerSignatureFunc { | ||
signature: Some(sig.clone().into()), | ||
binary: true | ||
} | ||
); | ||
let deser = SignatureFunc::from(ser); | ||
assert_matches!(deser, | ||
SignatureFunc::MissingValidateFunc(poly_func) => { | ||
assert_eq!(poly_func, sig.clone().into()); | ||
} | ||
); | ||
let custom: SignatureFunc = CustomSig.into(); | ||
let ser: SerSignatureFunc = custom.into(); | ||
assert_eq!( | ||
ser, | ||
SerSignatureFunc { | ||
signature: None, | ||
binary: true | ||
} | ||
); | ||
|
||
let deser = SignatureFunc::from(ser); | ||
assert_matches!(deser, SignatureFunc::MissingComputeFunc); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters