Skip to main content

crossref_client/query/
facet.rs

1use crate::query::{CrossrefQueryParam, ParamFragment};
2use std::borrow::Cow;
3
4/// all available facets that can be set as filter in a query
5#[derive(Debug, Clone)]
6pub enum Facet {
7    /// Author affiliation
8    Affiliation,
9    /// Funder literal name as deposited alongside DOIs
10    FunderName,
11    /// Funder DOI
12    FunderDoi,
13    /// Contributor ORCID
14    ORCID,
15    /// Work container title, such as journal title, or book title
16    ContainerTitle,
17    /// Custom Crossmark assertion name
18    Assertion,
19    /// Archive location
20    Archive,
21    /// Significant update type
22    UpdateType,
23    /// Journal ISSN (any - print, electronic, link)
24    ISSN,
25    /// Earliest year of publication
26    Published,
27    /// Work type name, such as `journal-article` or `book-chapter`
28    TypeName,
29    /// License URI of work
30    License,
31    /// Category name of work
32    CategoryName,
33    /// Relation type described by work or described by another work with work as object
34    RelationType,
35    /// Custom Crossmark assertion group name
36    AssertionGroup,
37    /// Publisher name of work
38    PublisherName,
39    /// Journal issue number
40    JournalIssue,
41    /// Journal volume number
42    JournalVolume,
43}
44
45impl Facet {
46    /// the maximum numeric number some facets are not allowed to exceed
47    pub const MAX_VAL_NUM: usize = 100;
48
49    /// the maximum value the facets can hold
50    /// some facets are unbounded `*`, some are limited to [MAX_VAL_NUM]
51    fn max_value(&self) -> &str {
52        match self {
53            Facet::ORCID | Facet::ContainerTitle | Facet::ISSN => "100",
54            _ => "*",
55        }
56    }
57
58    /// the key name for the query fragment
59    fn as_str(&self) -> &str {
60        match self {
61            Facet::Affiliation => "affiliation",
62            Facet::FunderName => "funder-name",
63            Facet::FunderDoi => "funder-doi",
64            Facet::ORCID => "orcid",
65            Facet::ContainerTitle => "container-title",
66            Facet::Assertion => "assertion",
67            Facet::Archive => "archive",
68            Facet::UpdateType => "update-type",
69            Facet::ISSN => "issn",
70            Facet::Published => "published",
71            Facet::TypeName => "type-name",
72            Facet::License => "license",
73            Facet::CategoryName => "category-name",
74            Facet::RelationType => "relation-type",
75            Facet::AssertionGroup => "assertion-group",
76            Facet::PublisherName => "publisher-name",
77            Facet::JournalIssue => "journal-issue",
78            Facet::JournalVolume => "journal-volume",
79        }
80    }
81}
82
83#[derive(Debug, Clone)]
84/// how many values of one facet to ask crossref to count
85pub struct FacetCount {
86    /// the targeted facet
87    pub facet: Facet,
88    /// the maximum number
89    pub count: Option<usize>,
90}
91
92impl FacetCount {
93    fn value(&self) -> String {
94        match &self.count {
95            Some(count) => match self.facet {
96                Facet::ORCID | Facet::ContainerTitle | Facet::ISSN => {
97                    if *count > Facet::MAX_VAL_NUM {
98                        Facet::MAX_VAL_NUM.to_string()
99                    } else {
100                        count.to_string()
101                    }
102                }
103                _ => count.to_string(),
104            },
105            _ => self.facet.max_value().to_string(),
106        }
107    }
108}
109
110impl ParamFragment for FacetCount {
111    fn key(&self) -> Cow<'_, str> {
112        Cow::Borrowed(self.facet.as_str())
113    }
114    fn value(&self) -> Option<Cow<'_, str>> {
115        Some(Cow::Owned(self.value()))
116    }
117}
118
119impl CrossrefQueryParam for Vec<FacetCount> {
120    /// facets share the `facet` key and are concat with `,`
121    fn params(&self) -> Vec<(Cow<'_, str>, Cow<'_, str>)> {
122        vec![(
123            Cow::Borrowed("facet"),
124            Cow::Owned(
125                self.iter()
126                    .map(ParamFragment::fragment)
127                    .collect::<Vec<_>>()
128                    .join(","),
129            ),
130        )]
131    }
132}