derive_builder/
error.rs

1#[cfg(feature = "std")]
2use std::{error::Error, fmt};
3
4#[cfg(not(feature = "std"))]
5use core::fmt;
6
7/// Runtime error when a `build()` method is called and one or more required fields
8/// do not have a value.
9#[derive(Debug, Clone)]
10pub struct UninitializedFieldError(&'static str);
11
12impl UninitializedFieldError {
13    /// Create a new `UnitializedFieldError` for the specified field name.
14    pub fn new(field_name: &'static str) -> Self {
15        UninitializedFieldError(field_name)
16    }
17
18    /// Get the name of the first-declared field that wasn't initialized
19    pub fn field_name(&self) -> &'static str {
20        self.0
21    }
22}
23
24impl fmt::Display for UninitializedFieldError {
25    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
26        write!(f, "Field not initialized: {}", self.0)
27    }
28}
29
30#[cfg(feature = "std")]
31impl Error for UninitializedFieldError {}
32
33impl From<&'static str> for UninitializedFieldError {
34    fn from(field_name: &'static str) -> Self {
35        Self::new(field_name)
36    }
37}