(&mut self, field: &str)
| 923 | } |
| 924 | |
| 925 | fn read_string(&mut self, field: &str) -> Result<String, OperationCodecError> { |
| 926 | let len = self.read_u32()? as usize; |
| 927 | if len == 0 { |
| 928 | return Err(OperationCodecError::new(format!("{field} cannot be empty"))); |
| 929 | } |
| 930 | if len > MAX_OPERATION_STRING_BYTES { |
| 931 | return Err(OperationCodecError::new(format!( |
| 932 | "{field} has {len} UTF-8 bytes, maximum is {MAX_OPERATION_STRING_BYTES}" |
| 933 | ))); |
| 934 | } |
| 935 | let end = self |
| 936 | .offset |
| 937 | .checked_add(len) |
| 938 | .ok_or_else(|| OperationCodecError::new("canonical string length overflow"))?; |
| 939 | let bytes = self.bytes.get(self.offset..end).ok_or_else(|| { |
| 940 | OperationCodecError::new(format!( |
| 941 | "canonical object is truncated at byte {} while reading {field}", |
| 942 | self.offset |
| 943 | )) |
| 944 | })?; |
| 945 | self.offset = end; |
| 946 | String::from_utf8(bytes.to_vec()) |
| 947 | .map_err(|_| OperationCodecError::new(format!("{field} is not valid UTF-8"))) |
| 948 | } |
| 949 | |
| 950 | fn read_blob(&mut self, field: &str) -> Result<Vec<u8>, OperationCodecError> { |
| 951 | let len = self.read_u32()? as usize; |
no test coverage detected