lemon_bugsnag_rs 0.1.2

A library for interacting with the BugSnag error-reporting and sessions APIs.
Documentation
use serde::Serialize;

/// Struct corresponding to the `events -> user` key in the BugSnag
/// error-reporting API payload. Information about effected user.
///
/// ```
/// use lemon_bugsnag_rs::error::payload::events::User;
///
/// let user = User {
///     id: "1".to_string().into(),
///     name: "John Doe".to_string().into(),
///     email: "jdoe@google.com".to_string().into()
/// };
/// ```
#[derive(Clone, Debug, Serialize, Eq, PartialEq)]
pub struct User {
    /// id of the user
    pub id: Option<String>,
    /// name of the user
    pub name: Option<String>,
    /// email of the user
    pub email: Option<String>,
}

#[cfg_attr(docsrs, doc(cfg(feature = "optional-builders")))]
#[cfg(feature = "optional-builders")]
impl User {
    /// Retrieve a [UserBuilder].
    ///
    /// ```
    /// use lemon_bugsnag_rs::error::payload::events::User;
    ///
    /// let mut ub = User::builder();
    /// ```
    pub fn builder() -> UserBuilder {
        UserBuilder {
            user: User { id: None, name: None, email: None },
        }
    }
}

/// Builder used to generate a [User] struct.
///
/// ```
/// use lemon_bugsnag_rs::error::payload::events::User;
///
/// // Generally, you get the builder this way, rather than creating an
/// // UserBuilder struct manually.
/// let mut ub = User::builder();
/// ```
#[cfg_attr(docsrs, doc(cfg(feature = "optional-builders")))]
#[cfg(feature = "optional-builders")]
pub struct UserBuilder {
    /// The user struct
    user: User,
}

#[cfg_attr(docsrs, doc(cfg(feature = "optional-builders")))]
#[cfg(feature = "optional-builders")]
impl UserBuilder {
    /// Set the id field of the User struct.
    ///
    /// ```
    /// use lemon_bugsnag_rs::error::payload::events::User;
    ///
    /// let mut tb = User::builder();
    ///
    /// // Give the id a value.
    /// tb.set_id("1");
    ///
    /// // Unset the id.
    /// tb.set_id(None);
    /// ```
    pub fn set_id<'a>(&mut self, id: impl Into<Option<&'a str>>) -> &mut Self {
        self.user.id = id.into().map(|item| item.into());

        self
    }

    /// Set the name field of the User struct.
    ///
    /// ```
    /// use lemon_bugsnag_rs::error::payload::events::User;
    ///
    /// let mut tb = User::builder();
    ///
    /// // Give the name a value.
    /// tb.set_name("1");
    ///
    /// // Unset the name.
    /// tb.set_name(None);
    /// ```
    pub fn set_name<'a>(
        &mut self,
        name: impl Into<Option<&'a str>>,
    ) -> &mut Self {
        self.user.name = name.into().map(|item| item.into());

        self
    }

    /// Set the email field of the User struct.
    ///
    /// ```
    /// use lemon_bugsnag_rs::error::payload::events::User;
    ///
    /// let mut tb = User::builder();
    ///
    /// // Give the email a value.
    /// tb.set_email("dev@null.nowhere");
    ///
    /// // Unset the email.
    /// tb.set_email(None);
    /// ```
    pub fn set_email<'a>(
        &mut self,
        email: impl Into<Option<&'a str>>,
    ) -> &mut Self {
        self.user.email = email.into().map(|item| item.into());

        self
    }

    /// Generate an [User] struct using the options you set using the
    /// builder.
    ///
    /// ```
    /// use lemon_bugsnag_rs::error::payload::events::User;
    ///
    /// let mut ub = User::builder();
    ///
    /// ub.set_id("12")
    ///     .set_name("User #1")
    ///     .set_email("user1@our.domain");
    ///
    /// let user = ub.build();
    /// ```
    pub fn build(&self) -> User {
        self.user.clone()
    }
}

#[cfg(test)]
mod test {
    #[cfg(feature = "optional-builders")]
    #[test]
    pub fn test_error_payload_events_user_builder() {
        use crate::error::payload::events::User;

        let test_user = User {
            id: "1".to_string().into(),
            name: "John Doe".to_string().into(),
            email: "jdoe@email.com".to_string().into(),
        };

        let user = User::builder()
            .set_id(test_user.id.clone().unwrap().as_str())
            .set_name(test_user.name.clone().unwrap().as_str())
            .set_email(test_user.email.clone().unwrap().as_str())
            .build();

        assert_eq!(test_user, user);
    }
}