diff options
Diffstat (limited to 'vendor/derive_builder/src/error.rs')
-rw-r--r-- | vendor/derive_builder/src/error.rs | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/vendor/derive_builder/src/error.rs b/vendor/derive_builder/src/error.rs new file mode 100644 index 000000000..3e1fb4eba --- /dev/null +++ b/vendor/derive_builder/src/error.rs @@ -0,0 +1,37 @@ +#[cfg(feature = "std")] +use std::{error::Error, fmt}; + +#[cfg(not(feature = "std"))] +use core::fmt; + +/// Runtime error when a `build()` method is called and one or more required fields +/// do not have a value. +#[derive(Debug, Clone)] +pub struct UninitializedFieldError(&'static str); + +impl UninitializedFieldError { + /// Create a new `UnitializedFieldError` for the specified field name. + pub fn new(field_name: &'static str) -> Self { + UninitializedFieldError(field_name) + } + + /// Get the name of the first-declared field that wasn't initialized + pub fn field_name(&self) -> &'static str { + self.0 + } +} + +impl fmt::Display for UninitializedFieldError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "Field not initialized: {}", self.0) + } +} + +#[cfg(feature = "std")] +impl Error for UninitializedFieldError {} + +impl From<&'static str> for UninitializedFieldError { + fn from(field_name: &'static str) -> Self { + Self::new(field_name) + } +} |