Decode a versioned wire message. Bytes must begin with the reserved [`ENVELOPE_MARKER`] (`0xc1`). Bytes without the marker are rejected — raw v1 frames are not accepted. An envelope with `version > WireVersion::CURRENT.0` is rejected with [`WireVersionError::UnsupportedVersion`].
(bytes: &[u8])
| 57 | /// An envelope with `version > WireVersion::CURRENT.0` is rejected |
| 58 | /// with [`WireVersionError::UnsupportedVersion`]. |
| 59 | pub fn decode_versioned<T>(bytes: &[u8]) -> Result<T, WireVersionError> |
| 60 | where |
| 61 | T: zerompk::FromMessagePackOwned, |
| 62 | { |
| 63 | match parse_envelope(bytes)? { |
| 64 | Some((version_raw, inner_bytes)) => { |
| 65 | // Version 0 inside the envelope is malformed — reject loudly. |
| 66 | if version_raw == 0 { |
| 67 | return Err(WireVersionError::DecodeFailure( |
| 68 | "v2 envelope with version 0 is invalid".to_string(), |
| 69 | )); |
| 70 | } |
| 71 | let peer_version = WireVersion(version_raw); |
| 72 | if peer_version > WireVersion::CURRENT { |
| 73 | return Err(WireVersionError::UnsupportedVersion { |
| 74 | peer_version, |
| 75 | supported_min: WireVersion::CURRENT, |
| 76 | supported_max: WireVersion::CURRENT, |
| 77 | }); |
| 78 | } |
| 79 | zerompk::from_msgpack(inner_bytes).map_err(|e| { |
| 80 | WireVersionError::DecodeFailure(format!("decode inner (v{peer_version}): {e}")) |
| 81 | }) |
| 82 | } |
| 83 | None => Err(WireVersionError::DecodeFailure( |
| 84 | "missing envelope marker: raw v1 frames are not accepted".to_string(), |
| 85 | )), |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /// Wrap arbitrary pre-encoded bytes in a v2 versioned envelope. |
| 90 | pub fn wrap_bytes_versioned(inner: &[u8]) -> Result<Vec<u8>, WireVersionError> { |