use crate::error::{Error, Result};
use crate::query::works::{WorksCombiner, WorksIdentQuery};
use crate::query::{Component, CrossrefQuery, CrossrefRoute, ResourceComponent};
use serde::{Deserialize, Serialize};
use std::str::FromStr;
macro_rules! define_types {
($($variant:ident => $id:literal / $label:literal,)*) => {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Deserialize, Serialize)]
#[serde(tag = "id")]
pub enum Type {
$(
#[doc = concat!("`", $id, "`, which crossref labels ", $label)]
#[serde(rename = $id)]
$variant,
)*
}
impl Type {
pub const ALL: &'static [Type] = &[$(Type::$variant,)*];
pub fn label(&self) -> &'static str {
match self { $(Type::$variant => $label,)* }
}
pub fn id(&self) -> &'static str {
match self { $(Type::$variant => $id,)* }
}
}
impl FromStr for Type {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
match s {
$($id => Ok(Type::$variant),)*
name => Err(Error::InvalidTypeName { name: name.to_string() }),
}
}
}
};
}
define_types! {
BookSection => "book-section" / "Book Section",
Monograph => "monograph" / "Monograph",
ReportComponent => "report-component" / "Report Component",
Report => "report" / "Report",
PeerReview => "peer-review" / "Peer Review",
BookTrack => "book-track" / "Book Track",
JournalArticle => "journal-article" / "Journal Article",
BookPart => "book-part" / "Part",
Other => "other" / "Other",
Book => "book" / "Book",
JournalVolume => "journal-volume" / "Journal Volume",
BookSet => "book-set" / "Book Set",
ReferenceEntry => "reference-entry" / "Reference Entry",
ProceedingsArticle => "proceedings-article" / "Proceedings Article",
Journal => "journal" / "Journal",
Component => "component" / "Component",
BookChapter => "book-chapter" / "Book Chapter",
ProceedingsSeries => "proceedings-series" / "Proceedings Series",
ReportSeries => "report-series" / "Report Series",
Proceedings => "proceedings" / "Proceedings",
Database => "database" / "Database",
Standard => "standard" / "Standard",
ReferenceBook => "reference-book" / "Reference Book",
PostedContent => "posted-content" / "Posted Content",
JournalIssue => "journal-issue" / "Journal Issue",
Dissertation => "dissertation" / "Dissertation",
Grant => "grant" / "Grant",
Dataset => "dataset" / "Dataset",
BookSeries => "book-series" / "Book Series",
EditedBook => "edited-book" / "Edited Book",
}
#[derive(Debug, Clone)]
pub enum Types {
All,
Identifier(String),
Works(WorksIdentQuery),
}
impl CrossrefRoute for Types {
fn route(&self) -> Result<String> {
match self {
Types::All => Component::Types.route(),
Types::Identifier(s) => Ok(format!("{}/{}", Component::Types.route()?, s)),
Types::Works(combined) => Self::combined_route(combined),
}
}
}
impl CrossrefQuery for Types {
fn resource_component(self) -> ResourceComponent {
ResourceComponent::Types(self)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_type_deserializes_from_the_record_the_api_sends() {
let section = r#"{
"id": "book-section",
"label": "Book Section"
}"#;
let ref_type: Type = serde_json::from_str(section).unwrap();
assert_eq!(Type::BookSection, ref_type);
}
#[test]
fn every_type_round_trips_through_from_str() {
for type_ in Type::ALL {
assert_eq!(*type_, Type::from_str(type_.id()).expect("a known type"));
}
}
#[test]
fn a_type_crossref_no_longer_registers_is_not_a_type() {
assert!(Type::from_str("standard-series").is_err());
}
}