|
| 1 | +use relrc::serialization::SerializedHistoryGraph; |
| 2 | + |
| 3 | +use super::*; |
| 4 | +use crate::hugr::patch::simple_replace::serial::SerialSimpleReplacement; |
| 5 | + |
| 6 | +/// Serialized format for [`PersistentReplacement`] |
| 7 | +pub type SerialPersistentReplacement<H> = SerialSimpleReplacement<H, PatchNode>; |
| 8 | + |
| 9 | +/// Serialized format for CommitData |
| 10 | +#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)] |
| 11 | +pub enum SerialCommitData<H> { |
| 12 | + /// Base commit containing a Hugr |
| 13 | + Base(H), |
| 14 | + /// Replacement commit containing a serialized replacement |
| 15 | + Replacement(SerialPersistentReplacement<H>), |
| 16 | +} |
| 17 | + |
| 18 | +impl CommitData { |
| 19 | + /// Create a new [`CommitData`]` from its serialized format |
| 20 | + pub fn from_serial<H: Into<Hugr>>(value: SerialCommitData<H>) -> Self { |
| 21 | + match value { |
| 22 | + SerialCommitData::Base(h) => CommitData::Base(h.into()), |
| 23 | + SerialCommitData::Replacement(replacement) => { |
| 24 | + CommitData::Replacement(replacement.into()) |
| 25 | + } |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + /// Convert this [`CommitData`] into its serialized format |
| 30 | + pub fn into_serial<H: From<Hugr>>(self) -> SerialCommitData<H> { |
| 31 | + match self { |
| 32 | + CommitData::Base(h) => SerialCommitData::Base(h.into()), |
| 33 | + CommitData::Replacement(replacement) => { |
| 34 | + SerialCommitData::Replacement(replacement.into_serial()) |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +impl<H: From<Hugr>> From<CommitData> for SerialCommitData<H> { |
| 41 | + fn from(value: CommitData) -> Self { |
| 42 | + value.into_serial() |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl<H: Into<Hugr>> From<SerialCommitData<H>> for CommitData { |
| 47 | + fn from(value: SerialCommitData<H>) -> Self { |
| 48 | + CommitData::from_serial(value) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +/// Serialized format for commit state space |
| 53 | +#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)] |
| 54 | +pub struct SerialCommitStateSpace<H> { |
| 55 | + /// The serialized history graph containing commit data |
| 56 | + pub graph: SerializedHistoryGraph<SerialCommitData<H>, (), PointerEqResolver>, |
| 57 | + /// The base commit ID |
| 58 | + pub base_commit: CommitId, |
| 59 | +} |
| 60 | + |
| 61 | +impl CommitStateSpace { |
| 62 | + /// Create a new [`CommitStateSpace`] from its serialized format |
| 63 | + pub fn from_serial<H: Into<Hugr> + Clone>(value: SerialCommitStateSpace<H>) -> Self { |
| 64 | + let SerialCommitStateSpace { graph, base_commit } = value; |
| 65 | + |
| 66 | + // Deserialize the SerializedHistoryGraph into a HistoryGraph with CommitData |
| 67 | + let graph = graph.map_nodes(|n| CommitData::from_serial(n)); |
| 68 | + let graph = HistoryGraph::try_from_serialized(graph, PointerEqResolver) |
| 69 | + .expect("failed to deserialize history graph"); |
| 70 | + |
| 71 | + Self { graph, base_commit } |
| 72 | + } |
| 73 | + |
| 74 | + /// Convert a [`CommitStateSpace`] into its serialized format |
| 75 | + pub fn into_serial<H: From<Hugr>>(self) -> SerialCommitStateSpace<H> { |
| 76 | + let Self { graph, base_commit } = self; |
| 77 | + let graph = graph.to_serialized(); |
| 78 | + let graph = graph.map_nodes(|n| n.into_serial()); |
| 79 | + SerialCommitStateSpace { graph, base_commit } |
| 80 | + } |
| 81 | + |
| 82 | + /// Create a serialized format from a reference to [`CommitStateSpace`] |
| 83 | + pub fn to_serial<H>(&self) -> SerialCommitStateSpace<H> |
| 84 | + where |
| 85 | + H: From<Hugr>, |
| 86 | + { |
| 87 | + let Self { graph, base_commit } = self; |
| 88 | + let graph = graph.to_serialized(); |
| 89 | + let graph = graph.map_nodes(|n| n.into_serial()); |
| 90 | + SerialCommitStateSpace { |
| 91 | + graph, |
| 92 | + base_commit: *base_commit, |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +impl<H: From<Hugr>> From<CommitStateSpace> for SerialCommitStateSpace<H> { |
| 98 | + fn from(value: CommitStateSpace) -> Self { |
| 99 | + value.into_serial() |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +impl<H: Clone + Into<Hugr>> From<SerialCommitStateSpace<H>> for CommitStateSpace { |
| 104 | + fn from(value: SerialCommitStateSpace<H>) -> Self { |
| 105 | + CommitStateSpace::from_serial(value) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +#[cfg(test)] |
| 110 | +mod tests { |
| 111 | + use derive_more::derive::Into; |
| 112 | + use rstest::rstest; |
| 113 | + use serde_with::serde_as; |
| 114 | + |
| 115 | + use super::*; |
| 116 | + use crate::{ |
| 117 | + envelope::serde_with::AsStringEnvelope, hugr::persistent::tests::test_state_space, |
| 118 | + }; |
| 119 | + |
| 120 | + #[serde_as] |
| 121 | + #[derive(Debug, Clone, serde::Serialize, serde::Deserialize, From, Into)] |
| 122 | + struct WrappedHugr { |
| 123 | + #[serde_as(as = "AsStringEnvelope")] |
| 124 | + pub hugr: Hugr, |
| 125 | + } |
| 126 | + |
| 127 | + #[cfg_attr(miri, ignore)] // Opening files is not supported in (isolated) miri |
| 128 | + #[rstest] |
| 129 | + fn test_serialize_state_space(test_state_space: (CommitStateSpace, [CommitId; 4])) { |
| 130 | + let (state_space, _) = test_state_space; |
| 131 | + let serialized = state_space.to_serial::<WrappedHugr>(); |
| 132 | + |
| 133 | + let deser = CommitStateSpace::from_serial(serialized); |
| 134 | + let _serialized_2 = deser.to_serial::<WrappedHugr>(); |
| 135 | + |
| 136 | + // TODO: add this once PointerEqResolver is replaced by a deterministic resolver |
| 137 | + // insta::assert_snapshot!(serde_json::to_string_pretty(&serialized).unwrap()); |
| 138 | + // see https://github.com/CQCL/hugr/issues/2299 |
| 139 | + // assert_eq!( |
| 140 | + // serde_json::to_string(&serialized), |
| 141 | + // serde_json::to_string(&serialized_2) |
| 142 | + // ); |
| 143 | + } |
| 144 | +} |
0 commit comments