use serde::{Deserialize, Serialize};
#[derive(Debug, Default, Clone, PartialEq, Deserialize, Serialize)]
#[serde(rename_all(deserialize = "camelCase"))]
pub struct Name {
pub first: String,
pub middle: Option<String>,
pub last: Option<String>,
full: String,
native: Option<String>,
alternative: Vec<String>,
alternative_spoiler: Option<Vec<String>>,
user_preferred: Option<String>,
}
impl Name {
pub fn full(&self) -> String {
self.full.clone()
}
pub fn native(&self) -> Option<String> {
self.native.clone()
}
pub fn alternative(&self) -> Vec<String> {
self.alternative.clone()
}
pub fn spoiler(&self) -> Option<Vec<String>> {
self.alternative_spoiler.clone()
}
pub fn user_preferred(&self) -> Option<String> {
self.user_preferred.clone()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_full() {
let name = Name {
first: "John".to_string(),
middle: Some("Doe".to_string()),
last: Some("Smith".to_string()),
full: "John Doe Smith".to_string(),
native: Some("ジョン ドウ スミス".to_string()),
alternative: vec!["Johnny".to_string()],
alternative_spoiler: Some(vec!["J.D.".to_string()]),
user_preferred: Some("John Smith".to_string()),
};
assert_eq!(name.full(), "John Doe Smith");
}
#[test]
fn test_native() {
let name = Name {
first: "John".to_string(),
middle: Some("Doe".to_string()),
last: Some("Smith".to_string()),
full: "John Doe Smith".to_string(),
native: Some("ジョン ドウ スミス".to_string()),
alternative: vec!["Johnny".to_string()],
alternative_spoiler: Some(vec!["J.D.".to_string()]),
user_preferred: Some("John Smith".to_string()),
};
assert_eq!(name.native(), Some("ジョン ドウ スミス".to_string()));
}
#[test]
fn test_alternative() {
let name = Name {
first: "John".to_string(),
middle: Some("Doe".to_string()),
last: Some("Smith".to_string()),
full: "John Doe Smith".to_string(),
native: Some("ジョン ドウ スミス".to_string()),
alternative: vec!["Johnny".to_string()],
alternative_spoiler: Some(vec!["J.D.".to_string()]),
user_preferred: Some("John Smith".to_string()),
};
assert_eq!(name.alternative(), vec!["Johnny".to_string()]);
}
#[test]
fn test_spoiler() {
let name = Name {
first: "John".to_string(),
middle: Some("Doe".to_string()),
last: Some("Smith".to_string()),
full: "John Doe Smith".to_string(),
native: Some("ジョン ドウ スミス".to_string()),
alternative: vec!["Johnny".to_string()],
alternative_spoiler: Some(vec!["J.D.".to_string()]),
user_preferred: Some("John Smith".to_string()),
};
assert_eq!(name.spoiler(), Some(vec!["J.D.".to_string()]));
}
#[test]
fn test_user_preferred() {
let name = Name {
first: "John".to_string(),
middle: Some("Doe".to_string()),
last: Some("Smith".to_string()),
full: "John Doe Smith".to_string(),
native: Some("ジョン ドウ スミス".to_string()),
alternative: vec!["Johnny".to_string()],
alternative_spoiler: Some(vec!["J.D.".to_string()]),
user_preferred: Some("John Smith".to_string()),
};
assert_eq!(name.user_preferred(), Some("John Smith".to_string()));
}
}