Skip to content

Commit

Permalink
Field_error improved, Boxed error kind, explicit naming also for `D…
Browse files Browse the repository at this point in the history
…ecodeErrorKind` (#197)
  • Loading branch information
Nicceboy authored Nov 10, 2023
1 parent 4c61085 commit a75b26b
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 117 deletions.
6 changes: 3 additions & 3 deletions macros/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ops::{Deref, Neg};
use std::ops::Deref;

use quote::ToTokens;
use syn::{Lit, NestedMeta, Path, UnOp};
Expand Down Expand Up @@ -937,7 +937,7 @@ impl<'a> FieldConfig<'a> {
.unwrap_or_else(|| context.to_string())
);

let or_else = quote!(.map_err(|error| #crate_root::de::Error::field_error(#ident, error, decoder.codec()))?);
let or_else = quote!(.map_err(|error| #crate_root::de::Error::field_error(#ident, error.into(), decoder.codec()))?);
let default_fn = self.default.as_ref().map(|default_fn| match default_fn {
Some(path) => quote!(#path),
None => quote!(<#ty>::default),
Expand All @@ -946,7 +946,7 @@ impl<'a> FieldConfig<'a> {
let tag = self.tag(context);
let constraints = self.constraints.const_expr(crate_root);
let handle_extension = if self.is_not_option_or_default_type() {
quote!(.ok_or_else(|| #crate_root::de::Error::field_error(#ident, "extension required but not present", decoder.codec()))?)
quote!(.ok_or_else(|| #crate_root::de::Error::field_error(#ident, crate::error::DecodeError::extension_present_but_not_required(#tag, decoder.codec()), decoder.codec()))?)
} else if self.is_default_type() {
quote!(.unwrap_or_else(#default_fn))
} else {
Expand Down
6 changes: 1 addition & 5 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,7 @@ pub trait Error: core::fmt::Display {
/// Creates a new error about being unable to decode a field in a compound
/// type, such as a set or sequence.
#[must_use]
fn field_error<D: core::fmt::Display>(
name: &'static str,
error: D,
codec: crate::Codec,
) -> Self;
fn field_error(name: &'static str, error: DecodeError, codec: crate::Codec) -> Self;
/// Creates a new error about finding a duplicate field.
#[must_use]
fn duplicate_field(name: &'static str, codec: crate::Codec) -> Self;
Expand Down
Loading

0 comments on commit a75b26b

Please sign in to comment.