build_deftly/
lib.rs

1use std::fmt;
2
3#[doc(hidden)]
4pub use derive_deftly;
5
6mod derive_builder;
7
8//pub use derive_builder::derive_deftly_template_Builder;
9
10pub mod prelude {
11    pub use crate::derive_builder::derive_deftly_template_Builder;
12}
13
14#[derive(Clone, Debug)]
15pub struct UninitializedFieldError {
16    field_name: &'static str,
17}
18
19impl UninitializedFieldError {
20    pub fn new(field_name: &'static str) -> Self {
21        Self { field_name }
22    }
23    pub fn field_name(&self) -> &'static str {
24        self.field_name
25    }
26}
27
28impl fmt::Display for UninitializedFieldError {
29    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
30        write!(f, "Field not initialized: {}", self.field_name)
31    }
32}
33impl std::error::Error for UninitializedFieldError {}
34
35/// Runtime error used when a sub-field's `build` method failed.
36#[derive(Debug, Clone)]
37pub struct SubfieldBuildError<E>(&'static str, E);
38
39impl<E> SubfieldBuildError<E> {
40    /// Wrap an error in a `SubfieldBuildError`, attaching the specified field name.
41    pub fn new(field_name: &'static str, sub_builder_error: E) -> Self {
42        SubfieldBuildError(field_name, sub_builder_error)
43    }
44
45    /// Get the field name of the sub-field that couldn't be built.
46    pub fn field_name(&self) -> &'static str {
47        self.0
48    }
49
50    /// Get the error that was returned for the sub-field
51    pub fn sub_builder_error(&self) -> &E {
52        &self.1
53    }
54
55    /// Decompose the `SubfieldBuildError` into its constituent parts
56    pub fn into_parts(self) -> (&'static str, E) {
57        (self.0, self.1)
58    }
59}
60
61impl<E> fmt::Display for SubfieldBuildError<E>
62where
63    E: fmt::Display,
64{
65    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
66        write!(f, "in {}: {}", self.0, self.1)
67    }
68}
69
70#[doc(hidden)]
71#[macro_export]
72macro_rules! strip_option {
73    { $(std::option::)? Option < $t:ty > } => { $t }
74}