derive_builder_fork_arti/
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}
38
39/// Runtime error used when a sub-field's `build` method failed.
40///
41/// Represents an error from a sub-structure's builder, when
42/// [`#[builder(sub_builder)]`](../index.html#sub-fields-with-builders-nested-builders)
43/// is used.
44/// Contains an `E`, the error type returned by the sub-structure's builder.
45/// See
46/// [Errors from the sub-structure builder](../index.html#errors-from-the-sub-structure-builder).
47#[derive(Debug, Clone)]
48pub struct SubfieldBuildError<E>(&'static str, E);
49
50impl<E> SubfieldBuildError<E> {
51    /// Wrap an error in a `SubfieldBuildError`, attaching the specified field name.
52    pub fn new(field_name: &'static str, sub_builder_error: E) -> Self {
53        SubfieldBuildError(field_name, sub_builder_error)
54    }
55
56    /// Get the field name of the sub-field that couldn't be built.
57    pub fn field_name(&self) -> &'static str {
58        self.0
59    }
60
61    /// Get the error that was returned for the sub-field
62    pub fn sub_builder_error(&self) -> &E {
63        &self.1
64    }
65
66    /// Decompose the `SubfieldBuildError` into its constituent parts
67    pub fn into_parts(self) -> (&'static str, E) {
68        (self.0, self.1)
69    }
70}
71
72impl<E> fmt::Display for SubfieldBuildError<E>
73where
74    E: fmt::Display,
75{
76    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
77        write!(f, "in {}: {}", self.0, self.1)
78    }
79}
80
81#[cfg(feature = "std")]
82// We do not implement `cause`.
83// Rust EHWG recommend that we *either* provide a cause, *or* include the information in our
84// own message.  We really want to do the latter or users without a proper error reporter
85// will get a very nugatory message.
86impl<E> Error for SubfieldBuildError<E> where E: Error {}