Skip to main content

crossref_client/query/
licenses.rs

1use crate::error::Result;
2use crate::query::encode;
3use crate::query::{
4    Component, CrossrefQuery, CrossrefQueryParam, CrossrefRoute, ResourceComponent, ResultControl,
5    format_queries,
6};
7use std::borrow::Cow;
8
9impl_list_query!(
10    /// Used to construct a query that targets the licenses crossref works are
11    /// published under
12    ///
13    /// # Example
14    ///
15    /// ```no_run
16    /// use crossref_client::{LicensesQuery, ResultControl};
17    ///
18    /// let query = LicensesQuery::new("creative commons")
19    ///     .result_control(ResultControl::Rows(10));
20    /// ```
21    ///
22    /// Like `/journals`, this route takes free form query terms and paging and
23    /// nothing else -- it rejects `filter`, `sort`, `order`, `facet`, `select`
24    /// and `sample` with a `400`.
25    LicensesQuery
26);
27
28/// constructs the request payload for the `/licenses` route
29///
30/// The route lists license urls with the number of works published under each,
31/// and has no per-license or combined `works` sub-route, so a query is the only
32/// way to address it.
33#[derive(Debug, Clone)]
34pub enum Licenses {
35    /// target all licenses that match the query at `/licenses?query...`
36    Query(LicensesQuery),
37}
38
39impl CrossrefRoute for Licenses {
40    fn route(&self) -> Result<String> {
41        match self {
42            // `route` already carries its own `?` and is empty for an empty query
43            Licenses::Query(query) => Ok(format!(
44                "{}{}",
45                Component::Licenses.route()?,
46                query.route()?
47            )),
48        }
49    }
50}
51
52impl CrossrefQuery for Licenses {
53    fn resource_component(self) -> ResourceComponent {
54        ResourceComponent::Licenses(self)
55    }
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn an_empty_query_targets_the_bare_route() {
64        assert_eq!(
65            "/licenses",
66            &Licenses::Query(LicensesQuery::empty()).route().unwrap()
67        );
68    }
69
70    #[test]
71    fn terms_and_paging_render_as_separate_parameters() {
72        let query = LicensesQuery::new("creative commons").result_control(ResultControl::Rows(10));
73
74        assert_eq!(
75            "/licenses?query=creative%20commons&rows=10",
76            &Licenses::Query(query).route().unwrap()
77        );
78    }
79}