Skip to main content

crossref_client/query/
funders.rs

1use crate::error::Result;
2use crate::query::works::{WorksCombiner, WorksIdentQuery};
3use crate::query::*;
4use std::borrow::Cow;
5
6define_filter! {
7/// Narrows a `/funders` query.
8///
9/// `location` is the only filter this route accepts. It used to also sit on
10/// [`WorksFilter`](crate::WorksFilter), where `/works` rejected it with a `400`.
11FundersFilter;
12markers {}
13values {
14    /// funders located in the named country
15    Location(String) => "location",
16}
17}
18
19impl_list_query!(
20    /// Used to construct a query that targets crossref `Funder` elements
21    ///
22    /// # Example
23    ///
24    /// ```no_run
25    /// use crossref_client::{FundersFilter, FundersQuery, ResultControl};
26    ///
27    /// let query = FundersQuery::new("NSF")
28    ///     .filter(FundersFilter::Location("Norway".to_string()))
29    ///     .result_control(ResultControl::Rows(10));
30    /// ```
31    ///
32    /// The route takes terms, one filter and paging. It used to also offer
33    /// `sort`, `order` and `facet`, which crossref answers with a `400`.
34    FundersQuery,
35    filter = FundersFilter
36);
37
38/// constructs the request payload for the `/funders` route
39#[derive(Debug, Clone)]
40pub enum Funders {
41    /// target a specific funder at `/funder/{id}`
42    Identifier(String),
43    /// target all funders that match the query at `/funders?query...`
44    Query(FundersQuery),
45    /// target a `Work` for a specific funder at `/funders/{id}/works?query..`
46    Works(WorksIdentQuery),
47}
48
49impl CrossrefRoute for Funders {
50    fn route(&self) -> Result<String> {
51        match self {
52            Funders::Identifier(s) => Ok(format!("{}/{}", Component::Funders.route()?, s)),
53            // `route` already carries its own `?` and is empty for an empty query
54            Funders::Query(query) => {
55                Ok(format!("{}{}", Component::Funders.route()?, query.route()?))
56            }
57            Funders::Works(combined) => Self::combined_route(combined),
58        }
59    }
60}
61
62impl CrossrefQuery for Funders {
63    fn resource_component(self) -> ResourceComponent {
64        ResourceComponent::Funders(self)
65    }
66}
67
68#[cfg(test)]
69mod tests {
70    use super::*;
71
72    #[test]
73    fn terms_filter_and_paging_render_as_separate_parameters() {
74        let query = FundersQuery::new("NSF")
75            .filter(FundersFilter::Location("Norway".to_string()))
76            .result_control(ResultControl::Rows(10));
77
78        assert_eq!(
79            "/funders?query=NSF&filter=location:Norway&rows=10",
80            &Funders::Query(query).route().unwrap()
81        );
82    }
83
84    #[test]
85    fn an_empty_query_targets_the_bare_route() {
86        assert_eq!(
87            "/funders",
88            &Funders::Query(FundersQuery::empty()).route().unwrap()
89        );
90    }
91}