-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add 1.1 binary reader support for length-prefixed structs #768
Add 1.1 binary reader support for length-prefixed structs #768
Conversation
src/lazy/binary/raw/v1_1/struct.rs
Outdated
enum StructType { | ||
Invalid, | ||
FlexSym, | ||
SymbolAddress, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not limit it to FlexSym
/SymbolAddress
and communicate Invalid
via a Result<StructType>
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't use a result, because at the time I was thinking I'd have to somehow work a Result into the different iter
functions, which felt like a much bigger ripple than I wanted to happen. I've reworked that now, so that it only holds FlexSym
, and SymbolAddress
. If new
has an unknown opcode it will panic, since the struct iterator shouldn't be created without a valid struct.
src/lazy/binary/raw/v1_1/struct.rs
Outdated
let (flex_sym, after) = buffer.read_flex_int()?; | ||
let sym_value = flex_sym.value(); | ||
let (sym, after) = match sym_value.cmp(&0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you move this bit of logic to the FlexSym
type?
src/lazy/binary/raw/v1_1/struct.rs
Outdated
} | ||
|
||
/// Helper function called from [`Self::next`] to parse the current field and value from the | ||
/// struct. On success, returns an both the field pair via [`LazyRawFieldExpr`] as well as the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// struct. On success, returns an both the field pair via [`LazyRawFieldExpr`] as well as the | |
/// struct. On success, returns both the field pair via [`LazyRawFieldExpr`] as well as the |
( | ||
// { $10: <NOP>, $11: 0e0 } - with nops, skip the NOP'd fields. | ||
&[ 0xC4, 0x15, 0xEC, 0x17, 0x5A ], | ||
&[ | ||
(11usize.into(), IonType::Float), | ||
], | ||
), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a test case where the NOP(s) are at the end of the container?
// { "foo": 1, $11: 2 } | ||
&[ 0xD9, 0xFB, 0x66, 0x6F, 0x6F, 0x51, 0x01, 0x17, 0x91, 0x02], | ||
&[ ("foo".into(), IonType::Int), (11usize.into(), IonType::Symbol)], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realize you're implementing an older copy of the spec, but the most recent version merged the distinct struct "kinds" into a single kind that can switch from symbol addresses to flexsym partway through a struct by emitting a FlexUInt 0
in field name position. This can wait for a follow-on PR since we have lots of updates to make around opcodes.
src/lazy/binary/raw/v1_1/reader.rs
Outdated
// Symbol Address | ||
( | ||
// { $10: 1, $11: 2 } | ||
&[0xC6, 0x15, 0x51, 0x01, 0x17, 0x51, 0x02], | ||
&[ | ||
(10usize.into(), IonType::Int), | ||
(11usize.into(), IonType::Int), | ||
] | ||
), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add test cases for the empty struct case and a nested struct?
4f173e6
to
7980ee2
Compare
let (flex_sym, after) = buffer.read_flex_sym()?; | ||
let (sym, after) = match flex_sym.value() { | ||
FlexSymValue::SymbolRef(sym_ref) => (sym_ref, after), | ||
FlexSymValue::Opcode(_opcode) => todo!(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice.
/// `offset` is the position of the slice in some larger input stream. It is only used to | ||
/// populate an appropriate error message if reading fails. | ||
pub fn read(input: &'top [u8], offset: usize) -> IonResult<FlexSym<'top>> { | ||
use crate::{result::IonFailure, IonError}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of interest, does your editor pull in traits/imports to the nearest scope itself or are you doing that manually?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do it manually. I try to keep it local to where it's used if it's not something that's going to be used elsewhere.
I would love it if my editor could promote the imports to higher scope if there was an existing local import though, or even if clippy could just point it out.
7980ee2
to
030623a
Compare
030623a
to
40e8b15
Compare
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #768 +/- ##
==========================================
- Coverage 80.60% 75.39% -5.21%
==========================================
Files 140 129 -11
Lines 28209 27098 -1111
Branches 28209 27098 -1111
==========================================
- Hits 22737 20430 -2307
- Misses 3641 5098 +1457
+ Partials 1831 1570 -261 ☔ View full report in Codecov by Sentry. |
Issue #, if available: #664
Description of changes:
This PR adds support to the 1.1 raw binary reader for length-prefixed structs.
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.