Skip to main content

crossref_async/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}
40
41impl Facet {
42    /// the maximum numeric number some facets are not allowed to exceed
43    pub const MAX_VAL_NUM: usize = 100;
44
45    /// the maximum value the facets can hold
46    /// some facets are unbounded `*`, some are limited to [MAX_VAL_NUM]
47    fn max_value(&self) -> &str {
48        match self {
49            Facet::ORCID | Facet::ContainerTitle | Facet::ISSN => "100",
50            _ => "*",
51        }
52    }
53
54    /// the key name for the query fragment
55    fn as_str(&self) -> &str {
56        match self {
57            Facet::Affiliation => "affiliation",
58            Facet::FunderName => "funder-name",
59            Facet::FunderDoi => "funder-doi",
60            Facet::ORCID => "orcid",
61            Facet::ContainerTitle => "container-title",
62            Facet::Assertion => "assertion",
63            Facet::Archive => "archive",
64            Facet::UpdateType => "update-type",
65            Facet::ISSN => "issn",
66            Facet::Published => "published",
67            Facet::TypeName => "type-name",
68            Facet::License => "license",
69            Facet::CategoryName => "category-name",
70            Facet::RelationType => "relation-type",
71            Facet::AssertionGroup => "assertion-group",
72            Facet::PublisherName => "publisher-name",
73        }
74    }
75}
76
77#[derive(Debug, Clone)]
78#[allow(missing_docs)]
79pub struct FacetCount {
80    /// the targeted facet
81    pub facet: Facet,
82    /// the maximum number
83    pub count: Option<usize>,
84}
85
86impl FacetCount {
87    fn value(&self) -> String {
88        match &self.count {
89            Some(count) => match self.facet {
90                Facet::ORCID | Facet::ContainerTitle | Facet::ISSN => {
91                    if *count > Facet::MAX_VAL_NUM {
92                        Facet::MAX_VAL_NUM.to_string()
93                    } else {
94                        count.to_string()
95                    }
96                }
97                _ => count.to_string(),
98            },
99            _ => self.facet.max_value().to_string(),
100        }
101    }
102}
103
104impl ParamFragment for FacetCount {
105    fn key(&self) -> Cow<'_, str> {
106        Cow::Borrowed(self.facet.as_str())
107    }
108    fn value(&self) -> Option<Cow<'_, str>> {
109        Some(Cow::Owned(self.value()))
110    }
111}
112
113impl CrossrefQueryParam for Vec<FacetCount> {
114    fn param_key(&self) -> Cow<'_, str> {
115        Cow::Borrowed("facet")
116    }
117
118    fn param_value(&self) -> Option<Cow<'_, str>> {
119        Some(Cow::Owned(
120            self.iter()
121                .map(ParamFragment::fragment)
122                .collect::<Vec<_>>()
123                .join(","),
124        ))
125    }
126}