use chrono::NaiveDate;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Deserialize, Serialize, sqlx::FromRow)]
pub struct Official {
pub birth: NaiveDate,
pub birth_country: Option<String>,
pub birth_country_sources: Vec<String>,
pub birth_sources: Vec<String>,
pub first_name: String,
pub first_name_sources: Vec<String>,
pub id: Uuid,
pub is_in_office: bool,
pub last_name: String,
pub last_name_sources: Vec<String>,
pub middle_name: Option<String>,
pub middle_name_sources: Vec<String>,
}
impl Official {
pub fn get_full_name(&self) -> String {
let middle_name_string = match &self.middle_name {
Some(name) => {
format!("{0} ", name)
}
None => String::from(""),
};
format!(
"{0} {1}{2}",
self.first_name, middle_name_string, self.last_name
)
} }
#[cfg(test)]
mod tests {
use super::*;
#[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(),
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(),
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(),
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(),
middle_name: None,
middle_name_sources: Vec::new(),
};
assert_eq!(official.get_full_name(), String::from("Austin Farrell"));
} }