whos_your_daddy_common 0.1.2

Common source code for the Who's Your Daddy projects, like the Enumerator and Presenter.
Documentation
//! Defines the `Official` structure along with supporting enums that describe
//! government levels and branches.
use chrono::NaiveDate;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

/// This array contains valid United States state strings.
const VALID_STATES: &[&str] = &[
    "AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA",
    "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD",
    "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ",
    "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC",
    "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY",
    "DC", "PR", "VI", "GU", "AS", "MP",
];

/// Represents the level of government for an elected official.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum GovernmentLevel {
    /// Federal level (e.g., President, Senators, Representatives)
    Federal,
    /// State level (e.g., Governor, State Legislators)
    State,
    /// County level (e.g., County Commissioners, Sheriffs)
    County,
    /// Municipal level (e.g., Mayors, City Council Members)
    Municipal,
    /// Special districts (e.g., School Board, Water District)
    SpecialDistrict,
}

/// Represents the branch of government.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Branch {
    /// Executive branch
    Executive,
    /// Legislative branch
    Legislative,
    /// Judicial branch
    Judicial,
}

/// 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)]
#[cfg_attr(feature = "postgres", derive(sqlx::FromRow))]
pub struct Official {
    /// The `birth` field indicates the Official's date of birth in `YYYY-MM-DD`
    /// format.
    pub birth: Option<NaiveDate>,

    // Branch of government
    // pub branch:         Option<Branch>,

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

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

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

    // Level of government
    // pub level:          Option<GovernmentLevel>,

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

    // State (2-letter abbreviation, e.g., "CA")
    // pub state:          Option<String>,

    // Term end date
    // pub term_end:       Option<DateTime<Utc>>,

    // Term start date
    // pub term_start:     Option<DateTime<Utc>>,

    // Official title/position (e.g., "U.S. Senator", "Governor")
    // pub title:          Option<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

    /// This method creates a new `Official` structure with only the
    /// `first_name`, `last_name`, and `id` fields initialized.
    ///
    /// # Parameters
    /// ## `first_name`
    /// New value for the `first_name` field.
    /// 
    /// ## `last_name`
    /// New value for the `last_name` field.
    ///
    /// # Returns
    /// This function returns a new `Official` struct.
    pub fn new(first_name: &str, last_name: &str) -> Self {
        Official {
            birth: None,
            // branch: None,
            first_name: String::from(first_name),
            id: Uuid::new_v4(),
            last_name: String::from(last_name),
            // level: None,
            middle_name: None,
            // state: None,
            // term_end: None,
            // term_start: None,
            // title: None
        }
    } // new
}

#[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: Some(NaiveDate::from_ymd_opt(1997, 8, 6).unwrap()),
            // branch: Some(Branch::Executive),
            first_name: String::from("Austin"),
            id: Uuid::new_v4(),
            last_name: String::from("Farrell"),
            // level: Some(GovernmentLevel::Federal),
            middle_name: Some(String::from("Michael")),
            // state: Some(String::from("CA")),
            // term_end: None,
            // term_start: None,
            // title: Some(String::from("Senator")),
        };

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

        let official = Official {
            birth: Some(NaiveDate::from_ymd_opt(1997, 8, 6).unwrap()),
            // branch: Some(Branch::Legislative),
            first_name: String::from("Austin"),
            id: Uuid::new_v4(),
            last_name: String::from("Farrell"),
            // level: Some(GovernmentLevel::State),
            middle_name: None,
            // state: Some(String::from("TX")),
            // term_end: None,
            // term_start: None,
            // title: Some(String::from("Representative")),
        };

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