whos_your_daddy_common 0.2.0

Common source code for the Who's Your Daddy projects, like the Enumerator and Presenter.
Documentation
//! Defines the `Official` structure and methods.
use chrono::NaiveDate;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

/// The `Official` structure maintains data about a single government official.
/// This struct is designed to match the definition of a row in the backing
/// PostgreSQL database's `people` table.
#[derive(Debug, Deserialize, Serialize, sqlx::FromRow)]
pub struct Official {
    /// The `birth` field indicates the `Official`'s date of birth in `YYYY-MM-DD`
    /// format.
    pub birth: NaiveDate,

    /// The country in which the `Official` was born.
    pub birth_country: Option<String>,

    /// Sources of information for the `birth_country` field.
    pub birth_country_sources: Vec<String>,

    /// Sources of information for the `birth` field.
    pub birth_sources: Vec<String>,

    /// This field indicates the individual's first name.
    pub first_name: String,

    /// Sources of information for the `first_name` field.
    pub first_name_sources: Vec<String>,

    /// Unique identifier for the official
    pub id: Uuid,

    /// Flag to indicate whether the official is currently serving in office.
    pub is_in_office: bool,

    /// This field indicates the individual's last name.
    pub last_name: String,

    /// Sources of information for the `last_name` field.
    pub last_name_sources: Vec<String>,

    /// This field indicates the individual's middle name, which may or may
    /// not exist.
    pub middle_name: Option<String>,

    /// Sources of information for the `middle_name` field.
    pub middle_name_sources: Vec<String>,
} // Official

impl Official {
    /// This function constructs a `String` representing the `Official`'s full
    /// name from the `Official`'s name members.
    ///
    /// # Returns
    /// This function returns a `String` object containing the result of
    /// concatenating the `Official`'s name members.
    pub fn get_full_name(&self) -> String {
        let middle_name_string = match &self.middle_name {
            Some(name) => {
                // If there's a middle name we will construct the string to
                // include a trailing space so that the format macro below
                // will still build the name nicely.
                format!("{0} ", name)
            }
            None => String::from(""),
        };

        // Build and return the name string.  The second and third
        // names are squished together so that if there is no middle name
        // the first and last names are not separated by two spaces, only
        // one.
        format!(
            "{0} {1}{2}",
            self.first_name, middle_name_string, self.last_name
        )
    } // get_full_name
}

#[cfg(test)]
mod tests {
    use super::*;

    /// This function tests the `Official` struct's `get_full_name` method.
    #[test]
    fn test_official_get_full_name() {
        let official = Official {
            birth: NaiveDate::from_ymd_opt(1997, 8, 6).unwrap(),
            birth_country: Some(String::from("USA")),
            birth_country_sources: Vec::new(),
            birth_sources: Vec::new(),
            // branch: Some(Branch::Executive),
            first_name: String::from("Austin"),
            first_name_sources: Vec::new(),
            id: Uuid::new_v4(),
            is_in_office: true,
            last_name: String::from("Farrell"),
            last_name_sources: Vec::new(),
            // level: Some(GovernmentLevel::Federal),
            middle_name: Some(String::from("Michael")),
            middle_name_sources: Vec::new(),
        };

        assert_eq!(
            official.get_full_name(),
            String::from("Austin Michael Farrell")
        );

        let official = Official {
            birth: NaiveDate::from_ymd_opt(1997, 8, 6).unwrap(),
            birth_country: Some(String::from("USA")),
            birth_country_sources: Vec::new(),
            birth_sources: Vec::new(),
            // branch: Some(Branch::Legislative),
            first_name: String::from("Austin"),
            first_name_sources: Vec::new(),
            id: Uuid::new_v4(),
            is_in_office: true,
            last_name: String::from("Farrell"),
            last_name_sources: Vec::new(),
            // level: Some(GovernmentLevel::State),
            middle_name: None,
            middle_name_sources: Vec::new(),
        };

        assert_eq!(official.get_full_name(), String::from("Austin Farrell"));
    } // test_official_get_full_name
}