Skip to content

Commit 14c91e9

Browse files
committed
variable sized object header
1 parent 835e179 commit 14c91e9

File tree

6 files changed

+184
-77
lines changed

6 files changed

+184
-77
lines changed

crates/batson/examples/read_file.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ cargo run --example read_file file.json [path]
4040
let output_json2 = batson_to_json_string(&batson).expect("failed to convert batson to JSON");
4141
println!("JSON unchanged after re-encoding: {:?}", output_json == output_json2);
4242

43-
println!("\n\noutput json:\n{}", output_json);
43+
if output_json.len() < 2000 {
44+
println!("\n\noutput json:\n{}", output_json);
45+
} else {
46+
println!("\n\noutput json is too long to display");
47+
}
4448
}
4549

4650
fn to_batson_path(s: &str) -> BatsonPath {

crates/batson/src/array.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,17 +144,17 @@ pub(crate) fn encode_array(encoder: &mut Encoder, array: &JsonArray) -> EncodeRe
144144
} else if let Some(packed_array) = PackedArray::new(array) {
145145
match packed_array {
146146
PackedArray::Header(array) => {
147-
encoder.push(Category::HeaderArray.encode_with(array.len() as u8));
147+
encoder.encode_length(Category::HeaderArray, array.len())?;
148148
// no alignment necessary, it's a vec of u8
149149
encoder.extend(&array);
150150
}
151151
PackedArray::I64(array) => {
152-
encoder.push(Category::I64Array.encode_with(array.len() as u8));
152+
encoder.encode_length(Category::I64Array, array.len())?;
153153
encoder.align::<i64>();
154154
encoder.extend(bytemuck::cast_slice(&array));
155155
}
156156
PackedArray::U8(array) => {
157-
encoder.push(Category::U8Array.encode_with(array.len() as u8));
157+
encoder.encode_length(Category::U8Array, array.len())?;
158158
// no alignment necessary, it's a vec of u8
159159
encoder.extend(&array);
160160
}

crates/batson/src/encoder.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ impl Encoder {
4848
self.data.len()
4949
}
5050

51+
pub fn reset_position(&mut self, position: usize) {
52+
self.data.truncate(position);
53+
}
54+
5155
pub fn encode_null(&mut self) {
5256
let h = Category::Primitive.encode_with(Primitive::Null as u8);
5357
self.push(h);
@@ -141,19 +145,27 @@ impl Encoder {
141145
self.push(cat.encode_with(Length::U8 as u8));
142146
self.push(s);
143147
} else if let Ok(int) = u16::try_from(len) {
144-
self.push(cat.encode_with(Length::U16 as u8));
145-
self.extend(&int.to_le_bytes());
146-
} else if let Ok(int) = u32::try_from(len) {
147-
self.push(cat.encode_with(Length::U32 as u8));
148-
self.extend(&int.to_le_bytes());
148+
self.encode_len_u16(cat, int);
149149
} else {
150-
return Err(EncodeError::StrTooLong);
150+
self.encode_len_u32(cat, len)?;
151151
}
152152
}
153153
}
154154
Ok(())
155155
}
156156

157+
pub fn encode_len_u16(&mut self, cat: Category, int: u16) {
158+
self.push(cat.encode_with(Length::U16 as u8));
159+
self.extend(&int.to_le_bytes());
160+
}
161+
162+
pub fn encode_len_u32(&mut self, cat: Category, len: usize) -> EncodeResult<()> {
163+
self.push(cat.encode_with(Length::U32 as u8));
164+
let int = u32::try_from(len).map_err(|_| EncodeError::StrTooLong)?;
165+
self.extend(&int.to_le_bytes());
166+
Ok(())
167+
}
168+
157169
pub fn push(&mut self, h: u8) {
158170
self.data.push(h);
159171
}

crates/batson/src/errors.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub type EncodeResult<T> = Result<T, EncodeError>;
99
#[derive(Debug, Copy, Clone)]
1010
pub enum EncodeError {
1111
StrTooLong,
12+
ObjectTooLarge,
1213
}
1314

1415
pub type DecodeResult<T> = Result<T, DecodeError>;

0 commit comments

Comments
 (0)