Skip to content

Commit 4da0e40

Browse files
committed
encoding: ignore unknown field
1 parent c5fab18 commit 4da0e40

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

crates/ast_node/src/encoding/decode.rs

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,18 @@ pub fn expand(DeriveInput { ident, data, .. }: DeriveInput) -> syn::ItemImpl {
3939
};
4040

4141
let count = data.fields.len();
42-
let head: Option<syn::Expr> = (count != 1).then(|| {
43-
syn::parse_quote! {{
44-
let n = <cbor4ii::core::types::Array<()>>::len(reader)?;
45-
debug_assert_eq!(n, Some(#count));
46-
}}
42+
let head: Option<syn::Stmt> = (count != 1).then(|| {
43+
syn::parse_quote! {
44+
let len = <cbor4ii::core::types::Array<()>>::len(reader)?.unwrap();
45+
}
46+
});
47+
let tail: Option<syn::Stmt> = head.is_some().then(|| {
48+
syn::parse_quote! {
49+
// ignore unknown field
50+
for _ in 0..(len - #count) {
51+
cbor4ii::core::dec::IgnoredAny::decode(reader)?;
52+
}
53+
}
4754
});
4855

4956
syn::parse_quote! {
@@ -53,8 +60,12 @@ pub fn expand(DeriveInput { ident, data, .. }: DeriveInput) -> syn::ItemImpl {
5360
-> Result<Self, cbor4ii::core::error::DecodeError<R::Error>>
5461
{
5562
#head;
56-
#(#fields)*
57-
Ok(#build_struct)
63+
let value = {
64+
#(#fields)*
65+
#build_struct
66+
};
67+
#tail
68+
Ok(value)
5869
}
5970
}
6071
}
@@ -180,7 +191,7 @@ pub fn expand(DeriveInput { ident, data, .. }: DeriveInput) -> syn::ItemImpl {
180191
}
181192
})
182193
.collect::<Vec<_>>();
183-
let num = field.fields.len();
194+
let count = field.fields.len();
184195

185196
let stmt = field.fields.iter()
186197
.zip(names.iter())
@@ -203,10 +214,18 @@ pub fn expand(DeriveInput { ident, data, .. }: DeriveInput) -> syn::ItemImpl {
203214

204215
syn::parse_quote!{
205216
#idx => {
206-
let len = cbor4ii::core::types::Array::len(reader)?;
207-
debug_assert_eq!(len, Some(#num));
208-
#(#stmt)*
209-
#build_struct
217+
let len = cbor4ii::core::types::Array::len(reader)?.unwrap();
218+
let value = {
219+
#(#stmt)*
220+
#build_struct
221+
};
222+
223+
// ignore unknown field
224+
for _ in 0..(len - #count) {
225+
cbor4ii::core::dec::IgnoredAny::decode(reader)?;
226+
}
227+
228+
value
210229
},
211230
}
212231
}

0 commit comments

Comments
 (0)