1use crate::query::{CrossrefQueryParam, ParamFragment};
2use std::borrow::Cow;
3
4#[derive(Debug, Clone)]
6pub enum Facet {
7 Affiliation,
9 FunderName,
11 FunderDoi,
13 ORCID,
15 ContainerTitle,
17 Assertion,
19 Archive,
21 UpdateType,
23 ISSN,
25 Published,
27 TypeName,
29 License,
31 CategoryName,
33 RelationType,
35 AssertionGroup,
37 PublisherName,
39}
40
41impl Facet {
42 pub const MAX_VAL_NUM: usize = 100;
44
45 fn max_value(&self) -> &str {
48 match self {
49 Facet::ORCID | Facet::ContainerTitle | Facet::ISSN => "100",
50 _ => "*",
51 }
52 }
53
54 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 pub facet: Facet,
82 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}