Skip to content
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

Return visitor errors over skip_decode errors if there are any #58

Merged
merged 6 commits into from
Jun 7, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 50 additions & 15 deletions scale-decode/src/visitor/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,17 @@ impl<'temp, 'scale, 'resolver, V: Visitor> ResolvedTypeVisitor<'resolver>
let res = self.visitor.visit_composite(&mut items, self.type_id);

// Skip over any bytes that the visitor chose not to decode:
items.skip_decoding()?;
*self.data = items.bytes_from_undecoded();
let skip_res = items.skip_decoding();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to extract "this logic" to a helper function that we can re-use instead repeating it for the other visitors?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the types are all a bit different and just happen to have the same method names; it could be a macro though :)

if skip_res.is_ok() {
*self.data = items.bytes_from_undecoded();
}

res
// Prioritize returning visitor errors over skip_decoding errors.
match (res, skip_res) {
(Err(e), _) => Err(e),
(_, Err(e)) => Err(e.into()),
(Ok(v), _) => Ok(v),
}
}

fn visit_variant<Path, Fields, Var>(self, _path: Path, variants: Var) -> Self::Value
Expand All @@ -143,10 +150,17 @@ impl<'temp, 'scale, 'resolver, V: Visitor> ResolvedTypeVisitor<'resolver>
let res = self.visitor.visit_variant(&mut variant, self.type_id);

// Skip over any bytes that the visitor chose not to decode:
variant.skip_decoding()?;
*self.data = variant.bytes_from_undecoded();
let skip_res = variant.skip_decoding();
if skip_res.is_ok() {
*self.data = variant.bytes_from_undecoded();
}

res
// Prioritize returning visitor errors over skip_decoding errors.
match (res, skip_res) {
(Err(e), _) => Err(e),
(_, Err(e)) => Err(e.into()),
(Ok(v), _) => Ok(v),
}
}

fn visit_sequence<Path>(self, _path: Path, inner_type_id: Self::TypeId) -> Self::Value
Expand All @@ -161,10 +175,17 @@ impl<'temp, 'scale, 'resolver, V: Visitor> ResolvedTypeVisitor<'resolver>
let res = self.visitor.visit_sequence(&mut items, self.type_id);

// Skip over any bytes that the visitor chose not to decode:
items.skip_decoding()?;
*self.data = items.bytes_from_undecoded();
let skip_res = items.skip_decoding();
if skip_res.is_ok() {
*self.data = items.bytes_from_undecoded();
}

res
// Prioritize returning visitor errors over skip_decoding errors.
match (res, skip_res) {
(Err(e), _) => Err(e),
(_, Err(e)) => Err(e.into()),
(Ok(v), _) => Ok(v),
}
}

fn visit_array(self, inner_type_id: Self::TypeId, len: usize) -> Self::Value {
Expand All @@ -176,10 +197,17 @@ impl<'temp, 'scale, 'resolver, V: Visitor> ResolvedTypeVisitor<'resolver>
let res = self.visitor.visit_array(&mut arr, self.type_id);

// Skip over any bytes that the visitor chose not to decode:
arr.skip_decoding()?;
*self.data = arr.bytes_from_undecoded();
let skip_res = arr.skip_decoding();
if skip_res.is_ok() {
*self.data = arr.bytes_from_undecoded();
}

res
// Prioritize returning visitor errors over skip_decoding errors.
match (res, skip_res) {
(Err(e), _) => Err(e),
(_, Err(e)) => Err(e.into()),
(Ok(v), _) => Ok(v),
}
}

fn visit_tuple<TypeIds>(self, type_ids: TypeIds) -> Self::Value
Expand All @@ -196,10 +224,17 @@ impl<'temp, 'scale, 'resolver, V: Visitor> ResolvedTypeVisitor<'resolver>
let res = self.visitor.visit_tuple(&mut items, self.type_id);

// Skip over any bytes that the visitor chose not to decode:
items.skip_decoding()?;
*self.data = items.bytes_from_undecoded();
let skip_res = items.skip_decoding();
if skip_res.is_ok() {
*self.data = items.bytes_from_undecoded();
}

res
// Prioritize returning visitor errors over skip_decoding errors.
match (res, skip_res) {
(Err(e), _) => Err(e),
(_, Err(e)) => Err(e.into()),
(Ok(v), _) => Ok(v),
}
}

fn visit_primitive(self, primitive: Primitive) -> Self::Value {
Expand Down
Loading