Encode the canonical span payload.
(&self)
| 129 | |
| 130 | /// Encode the canonical span payload. |
| 131 | pub fn encode(&self) -> Result<Vec<u8>, SpanPatchError> { |
| 132 | if self.version != 1 && self.version != VERSION && self.version != LEGACY_SOURCE_VERSION { |
| 133 | return Err(SpanPatchError::UnsupportedVersion(self.version)); |
| 134 | } |
| 135 | let mut bytes = MAGIC.to_vec(); |
| 136 | if self.version == 1 { |
| 137 | if !self.semantic_owners.is_empty() { |
| 138 | return Err(SpanPatchError::InvalidEncoding); |
| 139 | } |
| 140 | let legacy = SpanPatchV1 { |
| 141 | version: 1, |
| 142 | prior_metadata: self.prior_metadata.clone(), |
| 143 | ops: self.ops.clone(), |
| 144 | }; |
| 145 | bytes.extend( |
| 146 | postcard::to_allocvec(&legacy).map_err(|_| SpanPatchError::InvalidEncoding)?, |
| 147 | ); |
| 148 | } else { |
| 149 | bytes.extend(postcard::to_allocvec(self).map_err(|_| SpanPatchError::InvalidEncoding)?); |
| 150 | } |
| 151 | Ok(bytes) |
| 152 | } |
| 153 | |
| 154 | /// Decode one complete supported span payload. |
| 155 | pub fn decode(bytes: &[u8]) -> Result<Self, SpanPatchError> { |