Skip to main content

crossref_client/query/
journals.rs

1use crate::error::Result;
2use crate::query::encode;
3use crate::query::works::{WorksCombiner, WorksIdentQuery};
4use crate::query::{
5    Component, CrossrefQuery, CrossrefQueryParam, CrossrefRoute, ResourceComponent, ResultControl,
6    format_queries,
7};
8use std::borrow::Cow;
9
10impl_list_query!(
11    /// Used to construct a query that targets crossref `Journal` elements
12    ///
13    /// # Example
14    ///
15    /// ```no_run
16    /// use crossref_client::{JournalsQuery, ResultControl};
17    ///
18    /// let query = JournalsQuery::new("Economic Geography")
19    ///     .result_control(ResultControl::Rows(10));
20    /// ```
21    ///
22    /// `/journals` is one of the narrowest list routes: it takes free form
23    /// query terms and paging, and rejects `filter`, `sort`, `order`, `facet`,
24    /// `select` and `sample` with a `400`. The query type mirrors that rather
25    /// than offering options the route cannot honour.
26    JournalsQuery
27);
28
29/// constructs the request payload for the `/journals` route
30#[derive(Debug, Clone)]
31pub enum Journals {
32    /// target a specific journal at `/journals/{id}`
33    Identifier(String),
34    /// target a `Work` for a specific journal at `/journals/{id}/works?query..`
35    Works(WorksIdentQuery),
36    /// target all journals that match the query at `/journals?query...`
37    Query(JournalsQuery),
38}
39
40impl CrossrefRoute for Journals {
41    fn route(&self) -> Result<String> {
42        match self {
43            Journals::Identifier(s) => Ok(format!("{}/{}", Component::Journals.route()?, s)),
44            // `route` already carries its own `?` and is empty for an empty query
45            Journals::Query(query) => Ok(format!(
46                "{}{}",
47                Component::Journals.route()?,
48                query.route()?
49            )),
50            Journals::Works(combined) => Self::combined_route(combined),
51        }
52    }
53}
54
55impl CrossrefQuery for Journals {
56    fn resource_component(self) -> ResourceComponent {
57        ResourceComponent::Journals(self)
58    }
59}
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    #[test]
66    fn an_empty_query_targets_the_bare_route() {
67        assert_eq!(
68            "/journals",
69            &Journals::Query(JournalsQuery::empty()).route().unwrap()
70        );
71    }
72
73    #[test]
74    fn terms_and_paging_render_as_separate_parameters() {
75        let query =
76            JournalsQuery::new("Economic Geography").result_control(ResultControl::RowsOffset {
77                rows: 10,
78                offset: 20,
79            });
80
81        assert_eq!(
82            "/journals?query=Economic%20Geography&rows=10&offset=20",
83            &Journals::Query(query).route().unwrap()
84        );
85    }
86}