Skip to content

Commit 849de8e

Browse files
authored
Add 1.1 binary reader support for bools (#753)
* Add reader support for bools * Address clippy checks * Address feedback - bool with other length_code is truly unreachable
1 parent 9c88ae7 commit 849de8e

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

src/lazy/binary/raw/v1_1/immutable_buffer.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,10 @@ impl<'a> ImmutableBuffer<'a> {
229229
/// read, the returned `FlexUInt`'s `size_in_bytes()` method will return `0`.
230230
pub fn read_value_length(self, header: Header) -> ParseResult<'a, FlexUInt> {
231231
let length = match header.length_type() {
232-
LengthType::InOpcode(n) => FlexUInt::new(1, n as u64),
232+
LengthType::InOpcode(n) => {
233+
// FlexUInt represents the length, but is not physically present, hence the 0 size.
234+
FlexUInt::new(0, n as u64)
235+
}
233236
LengthType::FlexUIntFollows => {
234237
let (flexuint, _) = self.read_flex_uint()?;
235238
flexuint

src/lazy/binary/raw/v1_1/reader.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,22 @@ mod tests {
182182

183183
Ok(())
184184
}
185+
186+
#[test]
187+
fn bools() -> IonResult<()> {
188+
let data: Vec<u8> = vec![
189+
0xE0, 0x01, 0x01, 0xEA, // IVM
190+
0x5E, // true
191+
0x5F, // false
192+
];
193+
194+
let mut reader = LazyRawBinaryReader_1_1::new(&data);
195+
let _ivm = reader.next()?.expect_ivm()?;
196+
197+
assert!(reader.next()?.expect_value()?.read()?.expect_bool()?);
198+
199+
assert!(!(reader.next()?.expect_value()?.read()?.expect_bool()?));
200+
201+
Ok(())
202+
}
185203
}

src/lazy/binary/raw/v1_1/type_descriptor.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ impl Opcode {
4040
use OpcodeType::*;
4141

4242
let opcode_type = match (high_nibble, low_nibble) {
43+
(0x5, 0xE..=0xF) => Boolean,
4344
(0xE, 0x0) => IonVersionMarker,
4445
(0xE, 0xA) => NullNull,
4546
(0xE, 0xC..=0xD) => Nop,
@@ -110,6 +111,7 @@ impl Header {
110111
pub fn length_type(&self) -> LengthType {
111112
use LengthType::*;
112113
match (self.ion_type_code, self.length_code) {
114+
(OpcodeType::Boolean, 0xE..=0xF) => InOpcode(0),
113115
(OpcodeType::Nop, 0xC) => InOpcode(0),
114116
(OpcodeType::NullNull, 0xA) => InOpcode(0),
115117
_ => FlexUIntFollows,

src/lazy/binary/raw/v1_1/value.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
raw::{
1010
v1_1::{
1111
annotations_iterator::RawBinaryAnnotationsIterator_1_1,
12-
immutable_buffer::ImmutableBuffer, Header,
12+
immutable_buffer::ImmutableBuffer, Header, OpcodeType,
1313
},
1414
value::ValueParseResult,
1515
},
@@ -186,7 +186,15 @@ impl<'top> LazyRawBinaryValue_1_1<'top> {
186186

187187
/// Helper method called by [`Self::read`]. Reads the current value as a bool.
188188
fn read_bool(&self) -> ValueParseResult<'top, BinaryEncoding_1_1> {
189-
todo!();
189+
debug_assert!(self.encoded_value.ion_type() == IonType::Bool);
190+
let header = &self.encoded_value.header();
191+
let representation = header.type_code();
192+
let value = match (representation, header.length_code) {
193+
(OpcodeType::Boolean, 0xE) => true,
194+
(OpcodeType::Boolean, 0xF) => false,
195+
_ => unreachable!("found a boolean value with an illegal length code."),
196+
};
197+
Ok(RawValueRef::Bool(value))
190198
}
191199

192200
/// Helper method called by [`Self::read`]. Reads the current value as an int.

0 commit comments

Comments
 (0)