Skip to content

Commit

Permalink
Add 1.1 binary reader support for blobs and clobs (#755)
Browse files Browse the repository at this point in the history
  • Loading branch information
nirosys authored May 10, 2024
1 parent 46566f6 commit 2c86c00
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
40 changes: 40 additions & 0 deletions src/lazy/binary/raw/v1_1/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,4 +376,44 @@ mod tests {

Ok(())
}

fn blobs() -> IonResult<()> {
let data: Vec<u8> = 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<u8> = 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(())
}
}
2 changes: 2 additions & 0 deletions src/lazy/binary/raw/v1_1/type_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}

Expand Down
2 changes: 2 additions & 0 deletions src/lazy/binary/raw/v1_1/type_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 8 additions & 2 deletions src/lazy/binary/raw/v1_1/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 2c86c00

Please sign in to comment.