lemon_bugsnag_rs 0.1.2

A library for interacting with the BugSnag error-reporting and sessions APIs.
Documentation
use std::fmt::Display;

/// Enum describing errors that can be returned from struct builders.
/// ```
/// use lemon_bugsnag_rs::common::builder::client_builder::ClientBuilder;
/// use lemon_bugsnag_rs::common::traits::decider_trait_sync::DeciderTraitSync;
/// use lemon_bugsnag_rs::common::traits::backend_builder_trait_sync::BackendBuilderTraitSync;
/// use lemon_bugsnag_rs::common::error::ErrorType;
/// use lemon_bugsnag_rs::common::builder::error::BuilderError;
///
/// let builder = ClientBuilder::error().reqwest().blocking();
/// match builder.build() {
///     Ok(_) => {
///         // The result will not be OK, because you have not configured
///         // all required fields, such as the notifier and events fields.
///     },
///     Err(err) => {
///         match err.error_type()   {
///             ErrorType::ClientBuildError(variant) => {
///                 // Do your error handling and reporting here.
///                 match variant {
///                     BuilderError::MissingField { context, fields } => {
///                         // Handle missing fields.
///                     },
///                     BuilderError::ConflictingField { context, fields } => {
///                         // Handle conflicting fields.
///                     },
///                     _ => {
///                         // There are no other variants to handle. This is
///                         // only here to demonstrate that you would continue
///                         // processing variants as necessary.
///                     }
///                 }
///             },
///             _ => {
///                 // There are other error types, but none relevant to
///                 // this example.
///             }
///         }
///     },
/// }
/// ```
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum BuilderError {
    /// Returned when one or more required fields are missing from a
    /// builder's configuration when build() is called.
    MissingField {
        /// Context describing the place where the error happened. This could
        /// be a file name or function name, for example.
        context: String,
        /// A list of the missing fields.
        fields: Vec<String>,
    },
    /// Returned when the user has configured incompatible options on a builder.
    ConflictingField {
        /// Context describing the place where the error happened. This could
        /// be a file name or function name, for example.
        context: String,
        /// The pair of mutually exclusive fields that triggered this error.
        fields: Vec<String>,
    },
}

impl Display for BuilderError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            BuilderError::MissingField { context, fields } => {
                write!(
                    f,
                    "Struct {} is missing required field(s) {}.",
                    context,
                    fields.join(", ")
                )
            }
            BuilderError::ConflictingField { context, fields } => {
                write!(f, "Struct {} has conflicting fields. You may only specify one of {}.", context, fields.join(", "))
            }
        }
    }
}