Skip to content

Commit ee7821f

Browse files
authored
fix(test): Remove encoder field for hashed string (#2410)
Change the serialisation used for hashing in `hugr-persistent` tests to not include the hugr version string. This should make snapshots independent of hugr versioning. Only affects testing. This is a temporary solution until #2091 is done.
1 parent a4312f7 commit ee7821f

File tree

3 files changed

+164
-126
lines changed

3 files changed

+164
-126
lines changed

hugr-persistent/src/persistent_hugr/snapshots/hugr_persistent__persistent_hugr__serial__tests__serde_persistent_hugr.snap

Lines changed: 53 additions & 53 deletions
Large diffs are not rendered by default.

hugr-persistent/src/state_space/snapshots/hugr_persistent__state_space__serial__tests__serialize_state_space.snap

Lines changed: 71 additions & 71 deletions
Large diffs are not rendered by default.

hugr-persistent/src/tests.rs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,9 +485,47 @@ fn test_try_add_commit(test_state_space: (CommitStateSpace, [CommitId; 4])) {
485485
}
486486

487487
/// A Hugr that serialises with no extensions
488-
#[serde_as]
489488
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, From, Into)]
490489
pub(crate) struct WrappedHugr {
491-
#[serde_as(as = "AsStringEnvelope")]
490+
#[serde(with = "serial")]
492491
pub hugr: Hugr,
493492
}
493+
494+
mod serial {
495+
use hugr_core::envelope::EnvelopeConfig;
496+
use hugr_core::std_extensions::STD_REG;
497+
use serde::Deserialize;
498+
499+
use super::*;
500+
501+
pub(crate) fn serialize<S>(hugr: &Hugr, serializer: S) -> Result<S::Ok, S::Error>
502+
where
503+
S: serde::Serializer,
504+
{
505+
let mut str = hugr
506+
.store_str_with_exts(EnvelopeConfig::text(), &STD_REG)
507+
.map_err(serde::ser::Error::custom)?;
508+
// TODO: replace this with a proper hugr hash (see https://github.com/CQCL/hugr/issues/2091)
509+
remove_encoder_version(&mut str);
510+
serializer.serialize_str(&str)
511+
}
512+
513+
fn remove_encoder_version(str: &mut String) {
514+
// Remove encoder version information for consistent test output
515+
let encoder_pattern = r#""encoder":"hugr-rs v"#;
516+
if let Some(start) = str.find(encoder_pattern) {
517+
if let Some(end) = str[start..].find(r#"","#) {
518+
let end = start + end + 2; // +2 for the `",` part
519+
str.replace_range(start..end, "");
520+
}
521+
}
522+
}
523+
524+
pub(crate) fn deserialize<'de, D>(deserializer: D) -> Result<Hugr, D::Error>
525+
where
526+
D: serde::Deserializer<'de>,
527+
{
528+
let str = String::deserialize(deserializer)?;
529+
Hugr::load_str(str, Some(&STD_REG)).map_err(serde::de::Error::custom)
530+
}
531+
}

0 commit comments

Comments
 (0)