From 7e80119efb48184edf3a6a544297708bcb4de849 Mon Sep 17 00:00:00 2001 From: Ted Driggs Date: Wed, 1 Feb 2023 13:38:06 -0800 Subject: [PATCH] Add Error::unsupported_shape_with_expected Unsupported shape errors can now indicate what they expected to receive. --- core/src/error/kind.rs | 19 ++++++++++++++++--- core/src/error/mod.rs | 12 +++++++++++- core/src/util/shape.rs | 5 ++++- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/core/src/error/kind.rs b/core/src/error/kind.rs index 0030e0d..2b4eda6 100644 --- a/core/src/error/kind.rs +++ b/core/src/error/kind.rs @@ -15,7 +15,10 @@ pub(in crate::error) enum ErrorKind { Custom(String), DuplicateField(FieldName), MissingField(FieldName), - UnsupportedShape(DeriveInputShape), + UnsupportedShape { + observed: DeriveInputShape, + expected: Option, + }, UnknownField(ErrorUnknownField), UnexpectedFormat(MetaFormat), UnexpectedType(String), @@ -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", @@ -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), diff --git a/core/src/error/mod.rs b/core/src/error/mod.rs index 92b3bc5..f86d58e 100644 --- a/core/src/error/mod.rs +++ b/core/src/error/mod.rs @@ -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(shape: &str, expected: &T) -> Self { + Error::new(ErrorKind::UnsupportedShape { + observed: shape.into(), + expected: Some(expected.to_string()), + }) } pub fn unsupported_format(format: &str) -> Self { diff --git a/core/src/util/shape.rs b/core/src/util/shape.rs index 9ab1048..5a90891 100644 --- a/core/src/util/shape.rs +++ b/core/src/util/shape.rs @@ -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, + )) } }