Decode a `SessionEnvelope` from `HashedChange.metadata` bytes. Validates the magic prefix and schema version before deserializing. # Errors Returns `SessionEnvelopeError` if: - The data is too short - The magic prefix doesn't match - The schema version is unsupported - Postcard deserialization fails
(data: &[u8])
| 275 | /// - The schema version is unsupported |
| 276 | /// - Postcard deserialization fails |
| 277 | pub fn decode(data: &[u8]) -> EnvelopeResult<Self> { |
| 278 | if data.len() < MAGIC.len() + 1 { |
| 279 | return Err(codec_error(format!( |
| 280 | "data too short: {} bytes (minimum {})", |
| 281 | data.len(), |
| 282 | MAGIC.len() + 1 |
| 283 | ))); |
| 284 | } |
| 285 | |
| 286 | // Check magic |
| 287 | if &data[..4] != MAGIC { |
| 288 | return Err(codec_error(format!( |
| 289 | "invalid magic: expected {:?}, got {:?}", |
| 290 | MAGIC, |
| 291 | &data[..4] |
| 292 | ))); |
| 293 | } |
| 294 | |
| 295 | // Deserialize payload (includes schema_version field) |
| 296 | let envelope: Self = postcard::from_bytes(&data[4..]) |
| 297 | .map_err(|e| codec_error(format!("postcard deserialize failed: {}", e)))?; |
| 298 | |
| 299 | // Check version from the deserialized struct |
| 300 | if envelope.schema_version > SCHEMA_VERSION { |
| 301 | return Err(codec_error(format!( |
| 302 | "unsupported schema version: {} (max supported: {})", |
| 303 | envelope.schema_version, SCHEMA_VERSION |
| 304 | ))); |
| 305 | } |
| 306 | |
| 307 | Ok(envelope) |
| 308 | } |
| 309 | |
| 310 | /// Check whether a byte slice looks like a `SessionEnvelope`. |
| 311 | /// |