Skip to content

Commit

Permalink
Add Error::unsupported_shape_with_expected
Browse files Browse the repository at this point in the history
Unsupported shape errors can now indicate what they expected to receive.
  • Loading branch information
TedDriggs committed Feb 3, 2023
1 parent a4c0e95 commit 7e80119
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
19 changes: 16 additions & 3 deletions core/src/error/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ pub(in crate::error) enum ErrorKind {
Custom(String),
DuplicateField(FieldName),
MissingField(FieldName),
UnsupportedShape(DeriveInputShape),
UnsupportedShape {
observed: DeriveInputShape,
expected: Option<String>,
},
UnknownField(ErrorUnknownField),
UnexpectedFormat(MetaFormat),
UnexpectedType(String),
Expand All @@ -39,7 +42,7 @@ impl ErrorKind {
DuplicateField(_) => "Duplicate field",
MissingField(_) => "Missing field",
UnknownField(_) => "Unexpected field",
UnsupportedShape(_) => "Unsupported shape",
UnsupportedShape { .. } => "Unsupported shape",
UnexpectedFormat(_) => "Unexpected meta-item format",
UnexpectedType(_) => "Unexpected literal type",
UnknownValue(_) => "Unknown literal value",
Expand Down Expand Up @@ -69,7 +72,17 @@ impl fmt::Display for ErrorKind {
DuplicateField(ref field) => write!(f, "Duplicate field `{}`", field),
MissingField(ref field) => write!(f, "Missing field `{}`", field),
UnknownField(ref field) => field.fmt(f),
UnsupportedShape(ref shape) => write!(f, "Unsupported shape `{}`", shape),
UnsupportedShape {
ref observed,
ref expected,
} => {
write!(f, "Unsupported shape `{}`", observed)?;
if let Some(expected) = &expected {
write!(f, ". Expected {}.", expected)?;
}

Ok(())
}
UnexpectedFormat(ref format) => write!(f, "Unexpected meta-item format `{}`", format),
UnexpectedType(ref ty) => write!(f, "Unexpected literal type `{}`", ty),
UnknownValue(ref val) => write!(f, "Unknown literal value `{}`", val),
Expand Down
12 changes: 11 additions & 1 deletion core/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,17 @@ impl Error {

/// Creates a new error for a struct or variant that does not adhere to the supported shape.
pub fn unsupported_shape(shape: &str) -> Self {
Error::new(ErrorKind::UnsupportedShape(shape.into()))
Error::new(ErrorKind::UnsupportedShape {
observed: shape.into(),
expected: None,
})
}

pub fn unsupported_shape_with_expected<T: fmt::Display>(shape: &str, expected: &T) -> Self {
Error::new(ErrorKind::UnsupportedShape {
observed: shape.into(),
expected: Some(expected.to_string()),
})
}

pub fn unsupported_format(format: &str) -> Self {
Expand Down
5 changes: 4 additions & 1 deletion core/src/util/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,10 @@ impl ShapeSet {
if self.contains_shape(shape) {
Ok(())
} else {
Err(crate::Error::unsupported_shape(shape.description()))
Err(crate::Error::unsupported_shape_with_expected(
shape.description(),
self,
))
}
}

Expand Down

0 comments on commit 7e80119

Please sign in to comment.