crossref_client/query/
facet.rs1use 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 JournalIssue,
41 JournalVolume,
43}
44
45impl Facet {
46 pub const MAX_VAL_NUM: usize = 100;
48
49 fn max_value(&self) -> &str {
52 match self {
53 Facet::ORCID | Facet::ContainerTitle | Facet::ISSN => "100",
54 _ => "*",
55 }
56 }
57
58 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)]
84pub struct FacetCount {
86 pub facet: Facet,
88 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 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}