use citum_schema::options::TypeClass;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum TitleCategory {
Component,
Monograph,
Periodical,
Serial,
ContainerMonograph,
Default,
}
#[must_use]
pub(crate) fn title_category(ref_type: &str) -> TitleCategory {
match ref_type {
"article-journal" | "article-magazine" | "article-newspaper" | "chapter" | "entry"
| "entry-dictionary" | "entry-encyclopedia" | "paper-conference" | "post"
| "post-weblog" => TitleCategory::Component,
"book" | "thesis" | "report" => TitleCategory::Monograph,
_ => TitleCategory::Default,
}
}
#[must_use]
pub(crate) fn container_title_category(ref_type: &str) -> TitleCategory {
match ref_type {
"article-journal" | "article-magazine" | "article-newspaper" | "broadcast" => {
TitleCategory::Periodical
}
"chapter" | "paper-conference" => TitleCategory::ContainerMonograph,
_ => TitleCategory::Default,
}
}
#[must_use]
pub(crate) fn parent_serial_title_category(ref_type: &str) -> TitleCategory {
match ref_type {
"article-journal" | "article-magazine" | "article-newspaper" => TitleCategory::Periodical,
_ => TitleCategory::Serial,
}
}
#[must_use]
pub(crate) fn is_serial_parent_type(ref_type: &str) -> bool {
matches!(
ref_type,
"article-journal" | "article-magazine" | "article-newspaper" | "broadcast"
)
}
#[must_use]
pub(crate) fn matches_type_class(ref_type: &str, type_class: TypeClass) -> bool {
match type_class {
TypeClass::Legal => matches!(
ref_type,
"legal-case"
| "legal_case"
| "statute"
| "treaty"
| "regulation"
| "bill"
| "legislation"
),
TypeClass::Classical => matches!(ref_type, "classic" | "religious-text" | "religious_text"),
TypeClass::Standard => true,
}
}
#[must_use]
pub(crate) fn type_selector_aliases(ref_type: &str) -> Vec<&str> {
match ref_type {
"chapter" => vec!["chapter", "entry-dictionary"],
_ => vec![ref_type],
}
}
#[must_use]
pub(crate) fn synthesizes_doi_url(ref_type: &str) -> bool {
ref_type == "dataset"
}
#[cfg(test)]
#[allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::panic,
clippy::indexing_slicing,
clippy::todo,
clippy::unimplemented,
clippy::unreachable,
clippy::get_unwrap,
reason = "Panicking is acceptable and often desired in tests."
)]
mod tests {
use super::*;
#[test]
fn given_component_ref_type_when_title_category_then_component() {
let category = title_category("chapter");
assert_eq!(category, TitleCategory::Component);
}
#[test]
fn given_monograph_ref_type_when_title_category_then_monograph() {
let category = title_category("book");
assert_eq!(category, TitleCategory::Monograph);
}
#[test]
fn given_unclassified_ref_type_when_title_category_then_default() {
let category = title_category("dataset");
assert_eq!(category, TitleCategory::Default);
}
#[test]
fn given_periodical_ref_type_when_container_title_category_then_periodical() {
let category = container_title_category("article-journal");
assert_eq!(category, TitleCategory::Periodical);
}
#[test]
fn given_chapter_ref_type_when_container_title_category_then_container_monograph() {
let category = container_title_category("chapter");
assert_eq!(category, TitleCategory::ContainerMonograph);
}
#[test]
fn given_journal_article_when_parent_serial_title_category_then_periodical() {
let category = parent_serial_title_category("article-journal");
assert_eq!(category, TitleCategory::Periodical);
}
#[test]
fn given_book_when_parent_serial_title_category_then_serial() {
let category = parent_serial_title_category("book");
assert_eq!(category, TitleCategory::Serial);
}
#[test]
fn given_broadcast_when_parent_serial_title_category_then_serial_not_periodical() {
let category = parent_serial_title_category("broadcast");
assert_eq!(category, TitleCategory::Serial);
}
#[test]
fn given_journal_article_when_is_serial_parent_type_then_true() {
assert!(is_serial_parent_type("article-journal"));
}
#[test]
fn given_broadcast_when_is_serial_parent_type_then_true() {
assert!(is_serial_parent_type("broadcast"));
}
#[test]
fn given_book_when_is_serial_parent_type_then_false() {
assert!(!is_serial_parent_type("book"));
}
#[test]
fn given_legal_case_when_matches_legal_type_class_then_true() {
assert!(matches_type_class("legal-case", TypeClass::Legal));
}
#[test]
fn given_classic_when_matches_classical_type_class_then_true() {
assert!(matches_type_class("classic", TypeClass::Classical));
}
#[test]
fn given_classic_ref_type_when_checked_for_ancient_substring_then_absent() {
assert!(!"classic".contains("ancient"));
}
#[test]
fn given_book_when_matches_classical_type_class_then_false() {
assert!(!matches_type_class("book", TypeClass::Classical));
}
#[test]
fn given_any_ref_type_when_matches_standard_type_class_then_true() {
assert!(matches_type_class("anything", TypeClass::Standard));
}
#[test]
fn given_chapter_when_type_selector_aliases_then_includes_entry_dictionary() {
let aliases = type_selector_aliases("chapter");
assert_eq!(aliases, vec!["chapter", "entry-dictionary"]);
}
#[test]
fn given_book_when_type_selector_aliases_then_only_itself() {
let aliases = type_selector_aliases("book");
assert_eq!(aliases, vec!["book"]);
}
#[test]
fn given_dataset_when_synthesizes_doi_url_then_true() {
assert!(synthesizes_doi_url("dataset"));
}
#[test]
fn given_book_when_synthesizes_doi_url_then_false() {
assert!(!synthesizes_doi_url("book"));
}
}