use super::key::Somebody;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum RelationType {
Adoption,
Recognition,
CandidateParent,
GodParent,
FosterParent,
}
impl RelationType {
pub const KEYWORDS: &'static [&'static str] = &["adop", "reco", "cand", "godp", "fost"];
#[must_use]
pub fn from_keyword(keyword: &str) -> Option<Self> {
match keyword.strip_suffix(':').unwrap_or(keyword) {
"adop" => Some(Self::Adoption),
"reco" => Some(Self::Recognition),
"cand" => Some(Self::CandidateParent),
"godp" => Some(Self::GodParent),
"fost" => Some(Self::FosterParent),
_ => None,
}
}
#[must_use]
pub fn keyword(self) -> &'static str {
match self {
Self::Adoption => "adop",
Self::Recognition => "reco",
Self::CandidateParent => "cand",
Self::GodParent => "godp",
Self::FosterParent => "fost",
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Relation {
pub relation_type: RelationType,
pub father: Option<Somebody>,
pub mother: Option<Somebody>,
pub sources: String,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn keywords_round_trip_in_both_forms() {
for keyword in RelationType::KEYWORDS {
let bare = RelationType::from_keyword(keyword).expect("bare form");
let colon = RelationType::from_keyword(&format!("{keyword}:")).expect("colon form");
assert_eq!(bare, colon);
assert_eq!(bare.keyword(), *keyword);
}
assert_eq!(RelationType::from_keyword("nope"), None);
}
}