From ae7f61d15d0a1578f6af66a6c3bbb6b9d36fa593 Mon Sep 17 00:00:00 2001 From: Richard Giliam Date: Tue, 30 Apr 2024 14:59:37 -0700 Subject: [PATCH] Add 1.1 binary reader support for blobs and clobs --- src/lazy/binary/raw/v1_1/reader.rs | 40 +++++++++++++++++++++ src/lazy/binary/raw/v1_1/type_code.rs | 2 ++ src/lazy/binary/raw/v1_1/type_descriptor.rs | 2 ++ src/lazy/binary/raw/v1_1/value.rs | 10 ++++-- 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/lazy/binary/raw/v1_1/reader.rs b/src/lazy/binary/raw/v1_1/reader.rs index c41907ee..7b9efa7c 100644 --- a/src/lazy/binary/raw/v1_1/reader.rs +++ b/src/lazy/binary/raw/v1_1/reader.rs @@ -376,4 +376,44 @@ mod tests { Ok(()) } + + fn blobs() -> IonResult<()> { + let data: Vec = vec![ + 0xe0, 0x01, 0x01, 0xea, // IVM + 0xFE, 0x31, 0x49, 0x20, 0x61, 0x70, 0x70, 0x6c, 0x61, 0x75, 0x64, 0x20, 0x79, 0x6f, + 0x75, 0x72, 0x20, 0x63, 0x75, 0x72, 0x69, 0x6f, 0x73, 0x69, 0x74, 0x79, + ]; + + let mut reader = LazyRawBinaryReader_1_1::new(&data); + let _ivm = reader.next()?.expect_ivm()?; + + let bytes: &[u8] = &[ + 0x49, 0x20, 0x61, 0x70, 0x70, 0x6c, 0x61, 0x75, 0x64, 0x20, 0x79, 0x6f, 0x75, 0x72, + 0x20, 0x63, 0x75, 0x72, 0x69, 0x6f, 0x73, 0x69, 0x74, 0x79, + ]; + assert_eq!(reader.next()?.expect_value()?.read()?.expect_blob()?, bytes); + + Ok(()) + } + + #[test] + fn clobs() -> IonResult<()> { + let data: Vec = vec![ + 0xe0, 0x01, 0x01, 0xea, // IVM + 0xFF, 0x31, 0x49, 0x20, 0x61, 0x70, 0x70, 0x6c, 0x61, 0x75, 0x64, 0x20, 0x79, 0x6f, + 0x75, 0x72, 0x20, 0x63, 0x75, 0x72, 0x69, 0x6f, 0x73, 0x69, 0x74, 0x79, + ]; + + let mut reader = LazyRawBinaryReader_1_1::new(&data); + let _ivm = reader.next()?.expect_ivm()?; + + let bytes: &[u8] = &[ + 0x49, 0x20, 0x61, 0x70, 0x70, 0x6c, 0x61, 0x75, 0x64, 0x20, 0x79, 0x6f, 0x75, 0x72, + 0x20, 0x63, 0x75, 0x72, 0x69, 0x6f, 0x73, 0x69, 0x74, 0x79, + ]; + + assert_eq!(reader.next()?.expect_value()?.read()?.expect_clob()?, bytes); + + Ok(()) + } } diff --git a/src/lazy/binary/raw/v1_1/type_code.rs b/src/lazy/binary/raw/v1_1/type_code.rs index 837e104f..769fa133 100644 --- a/src/lazy/binary/raw/v1_1/type_code.rs +++ b/src/lazy/binary/raw/v1_1/type_code.rs @@ -43,6 +43,8 @@ pub enum OpcodeType { // delimited list start // delimited s-expression start LargeInteger, // 0xF5 - Integer preceeded by FlexUInt length + Blob, // 0xFE - + Clob, // 0xFF - Invalid, // Represents an encoded value that does not match a defined opcode. } diff --git a/src/lazy/binary/raw/v1_1/type_descriptor.rs b/src/lazy/binary/raw/v1_1/type_descriptor.rs index 2695ff28..2d76a09c 100644 --- a/src/lazy/binary/raw/v1_1/type_descriptor.rs +++ b/src/lazy/binary/raw/v1_1/type_descriptor.rs @@ -51,6 +51,8 @@ impl Opcode { (0xF, 0x5) => (LargeInteger, low_nibble, Some(IonType::Int)), (0xF, 0x8) => (String, 0xFF, Some(IonType::String)), // 0xFF indicates >15 byte string. (0xF, 0x9) => (InlineSymbol, 0xFF, Some(IonType::Symbol)), + (0xF, 0xE) => (Blob, low_nibble, Some(IonType::Blob)), + (0xF, 0xF) => (Clob, low_nibble, Some(IonType::Clob)), _ => (Invalid, low_nibble, None), }; Opcode { diff --git a/src/lazy/binary/raw/v1_1/value.rs b/src/lazy/binary/raw/v1_1/value.rs index ee1956f5..ef5b697a 100644 --- a/src/lazy/binary/raw/v1_1/value.rs +++ b/src/lazy/binary/raw/v1_1/value.rs @@ -292,12 +292,18 @@ impl<'top> LazyRawBinaryValue_1_1<'top> { /// Helper method called by [`Self::read`]. Reads the current value as a blob. fn read_blob(&self) -> ValueParseResult<'top, BinaryEncoding_1_1> { - todo!(); + debug_assert!(self.encoded_value.ion_type() == IonType::Blob); + + let raw_bytes = self.value_body()?; + Ok(RawValueRef::Blob(raw_bytes.into())) } /// Helper method called by [`Self::read`]. Reads the current value as a clob. fn read_clob(&self) -> ValueParseResult<'top, BinaryEncoding_1_1> { - todo!(); + debug_assert!(self.encoded_value.ion_type() == IonType::Clob); + + let raw_bytes = self.value_body()?; + Ok(RawValueRef::Clob(raw_bytes.into())) } /// Helper method called by [`Self::read`]. Reads the current value as an S-expression.