1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
use crate::error::{Error, Result};
use crate::query::facet::FacetCount;
pub use crate::query::funders::{Funders, FundersQuery};
pub use crate::query::journals::Journals;
pub use crate::query::members::{Members, MembersQuery};
pub use crate::query::prefixes::Prefixes;
pub use crate::query::types::{Type, Types};
use crate::query::works::{Works, WorksFilter};
pub use crate::query::works::{WorksCombined, WorksQuery};
use chrono::NaiveDate;
use core::fmt::Debug;
use serde::Serialize;
use std::borrow::Cow;
use std::fmt;

/// Helper trait for unified interface
pub trait CrossrefParams {
    /// the filter applied
    type Filter: Filter;
    /// all string queries
    fn query_terms(&self) -> &[String];
    /// the filters this object can use
    fn filters(&self) -> &[Self::Filter];
    /// the sort if set
    fn sort(&self) -> Option<&Sort>;
    /// the order if set
    fn order(&self) -> Option<&Order>;
    /// all facets this objects addresses
    fn facets(&self) -> &[FacetCount];
    /// the configured result control, if any
    fn result_control(&self) -> Option<&ResultControl>;
}

// TODO extract Query impl into separate trait

macro_rules! impl_common_query {
    ($i:ident, $filter:ident) => {
        /// Each query parameter is ANDed
        #[derive(Debug, Clone, Default, Serialize, Deserialize)]
        pub struct $i {
            /// search by non specific query
            pub queries: Vec<String>,
            /// filter to apply while querying
            pub filter: Vec<$filter>,
            /// sort results by a certain field and
            pub sort: Option<Sort>,
            /// set the sort order to `asc` or `desc`
            pub order: Option<Order>,
            /// enable facet information in responses
            pub facets: Vec<FacetCount>,
            /// deep page through `/works` result sets
            pub result_control: Option<ResultControl>,
        }

        impl $i {
            /// alias for creating an empty default element
            pub fn empty() -> Self {
                $i::default()
            }

            /// alias for creating an new default element
            pub fn new() -> Self {
                $i::default()
            }

            /// add a new free form query
            pub fn query(mut self, query: &str) -> Self {
                self.queries.push(query.to_string());
                self
            }
            /// add a new filter to the query
            pub fn filter(mut self, filter: $filter) -> Self {
                self.filter.push(filter);
                self
            }

            /// set sort option to the query
            pub fn sort(mut self, sort: Sort) -> Self {
                self.sort = Some(sort);
                self
            }

            /// set order to asc
            pub fn order_asc(mut self) -> Self {
                self.order = Some(Order::Asc);
                self
            }
            /// set order to desc
            pub fn order_desc(mut self) -> Self {
                self.order = Some(Order::Desc);
                self
            }

            /// set order option to query
            pub fn order(mut self, order: Order) -> Self {
                self.order = Some(order);
                self
            }

            /// add another facet to query
            pub fn facet(mut self, facet: FacetCount) -> Self {
                self.facets.push(facet);
                self
            }

            /// set result control option to query
            pub fn result_control(mut self, result_control: ResultControl) -> Self {
                self.result_control = Some(result_control);
                self
            }
        }

        impl CrossrefParams for $i {
            type Filter = $filter;

            fn query_terms(&self) -> &[String] {
                &self.queries
            }
            fn filters(&self) -> &[Self::Filter] {
                &self.filter
            }
            fn sort(&self) -> Option<&Sort> {
                self.sort.as_ref()
            }
            fn order(&self) -> Option<&Order> {
                self.order.as_ref()
            }
            fn facets(&self) -> &[FacetCount] {
                &self.facets
            }
            fn result_control(&self) -> Option<&ResultControl> {
                self.result_control.as_ref()
            }
        }

        impl CrossrefRoute for $i {
            fn route(&self) -> Result<String> {
                let mut params = Vec::new();
                if !self.queries.is_empty() {
                    params.push(Cow::Owned(format!(
                        "query={}",
                        format_queries(&self.queries)
                    )));
                }
                if !self.filter.is_empty() {
                    params.push(self.filter.param());
                }
                if !self.facets.is_empty() {
                    params.push(self.facets.param());
                }
                if let Some(sort) = &self.sort {
                    params.push(sort.param());
                }
                if let Some(order) = &self.order {
                    params.push(order.param());
                }
                if let Some(rc) = &self.result_control {
                    params.push(rc.param());
                }
                Ok(params.join("&"))
            }
        }
    };
}

/// provides types to filter facets
pub mod facet;
/// provides support to query the `/funders` route
pub mod funders;
/// provides support to query the `/funders` route
pub mod journals;
/// provides support to query the `/journals` route
pub mod members;
/// provides support to query the `/members` route
pub mod prefixes;
/// provides support to query the `/prefixes` route
pub mod types;
/// provides support to query the `/types` route
pub mod works;

/// represents the visibility of an crossref item
#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
#[allow(missing_docs)]
pub enum Visibility {
    Open,
    Limited,
    Closed,
}

impl Visibility {
    /// str identifier
    pub fn as_str(&self) -> &str {
        match self {
            Visibility::Open => "open",
            Visibility::Limited => "limited",
            Visibility::Closed => "closed",
        }
    }
}

/// Determines how results should be sorted
#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize)]
pub enum Order {
    /// list results in ascending order
    Asc,
    /// list results in descending order
    Desc,
}

impl Order {
    /// the key name for the order parameter
    pub fn as_str(&self) -> &str {
        match self {
            Order::Asc => "asc",
            Order::Desc => "desc",
        }
    }
}

impl CrossrefQueryParam for Order {
    fn param_key(&self) -> Cow<str> {
        Cow::Borrowed("order")
    }

    fn param_value(&self) -> Option<Cow<str>> {
        Some(Cow::Borrowed(self.as_str()))
    }
}

/// Results from a list response can be sorted by applying the sort and order parameters.
#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize)]
pub enum Sort {
    /// Sort by relevance score
    Score,
    /// Sort by date of most recent change to metadata. Currently the same as `Deposited`
    Updated,
    /// Sort by time of most recent deposit
    Deposited,
    /// Sort by time of most recent index
    Indexed,
    /// Sort by publication date
    Published,
    /// Sort by print publication date
    PublishedPrint,
    /// Sort by online publication date
    PublishedOnline,
    /// Sort by issued date (earliest known publication date)
    Issued,
    /// Sort by number of times this DOI is referenced by other Crossref DOIs
    IsReferencedByCount,
    /// Sort by number of references included in the references section of the document identified by this DOI
    ReferenceCount,
}

impl Sort {
    /// the key name for the filter element
    pub fn as_str(&self) -> &str {
        match self {
            Sort::Score => "score",
            Sort::Updated => "updated",
            Sort::Deposited => "deposited",
            Sort::Indexed => "indexed",
            Sort::Published => "published",
            Sort::PublishedPrint => "published-print",
            Sort::PublishedOnline => "published-online",
            Sort::Issued => "issued",
            Sort::IsReferencedByCount => "is-reference-by-count",
            Sort::ReferenceCount => "reference-count",
        }
    }
}

impl CrossrefQueryParam for Sort {
    fn param_key(&self) -> Cow<str> {
        Cow::Borrowed("sort")
    }

    fn param_value(&self) -> Option<Cow<str>> {
        Some(Cow::Borrowed(self.as_str()))
    }
}

/// tells crossref how many items shall be returned or where to start
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ResultControl {
    /// limits the returned items per page
    Rows(usize),
    /// sets an offset where crossref begins to retrieve items
    /// high offsets (~10k) result in long response times
    Offset(usize),
    /// combines rows and offset: limit returned items per page, starting at the offset
    RowsOffset {
        /// row limit
        rows: usize,
        /// where to start
        offset: usize,
    },
    /// return random results
    Sample(usize),
}

impl CrossrefQueryParam for ResultControl {
    fn param_key(&self) -> Cow<str> {
        match self {
            ResultControl::Rows(_) => Cow::Borrowed("rows"),
            ResultControl::Offset(_) => Cow::Borrowed("offset"),
            ResultControl::RowsOffset { rows, .. } => Cow::Owned(format!("rows={}", rows)),
            ResultControl::Sample(_) => Cow::Borrowed("sample"),
        }
    }

    fn param_value(&self) -> Option<Cow<str>> {
        match self {
            ResultControl::Rows(r) | ResultControl::Offset(r) | ResultControl::Sample(r) => {
                Some(Cow::Owned(r.to_string()))
            }
            ResultControl::RowsOffset { offset, .. } => {
                Some(Cow::Owned(format!("offset={}", offset)))
            }
        }
    }
}

/// Major resource components supported by the Crossref API
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Component {
    /// returns a list of all works (journal articles, conference proceedings, books, components, etc), 20 per page
    Works,
    /// returns a list of all funders in the [Funder Registry](https://github.com/Crossref/open-funder-registry)
    Funders,
    /// returns a list of all Crossref members (mostly publishers)
    Prefixes,
    /// returns a list of valid work types
    Members,
    /// return a list of licenses applied to works in Crossref metadata
    Types,
    /// return a list of journals in the Crossref database
    Journals,
}

impl Component {
    /// identifier for the component route
    pub fn as_str(&self) -> &str {
        match self {
            Component::Works => "works",
            Component::Funders => "funders",
            Component::Prefixes => "prefixes",
            Component::Members => "members",
            Component::Types => "types",
            Component::Journals => "journals",
        }
    }
}

impl CrossrefRoute for Component {
    fn route(&self) -> Result<String> {
        Ok(format!("/{}", self.as_str()))
    }
}

#[allow(missing_docs)]
pub struct WorksRequest {
    primary_component: Component,
    query: WorksQuery,
    id: Option<String>,
}

impl CrossrefRoute for WorksRequest {
    fn route(&self) -> Result<String> {
        unimplemented!()
    }
}

/// bundles all available crossref api endpoints
///
///
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ResourceComponent {
    /// returns a list of all works (journal articles, conference proceedings, books, components, etc), 20 per page
    Works(Works),
    /// returns a list of all funders in the [Funder Registry](https://github.com/Crossref/open-funder-registry)
    Funders(Funders),
    /// returns a list of all Crossref members (mostly publishers)
    Prefixes(Prefixes),
    /// returns a list of valid work types
    Members(Members),
    /// return a list of licenses applied to works in Crossref metadata
    Types(Types),
    /// return a list of journals in the Crossref database
    Journals(Journals),
}

impl ResourceComponent {
    /// the starting crossref component that in the route `/{primary_component}/{id}/works`
    pub fn primary_component(&self) -> Component {
        match self {
            ResourceComponent::Works(_) => Component::Works,
            ResourceComponent::Funders(_) => Component::Funders,
            ResourceComponent::Prefixes(_) => Component::Prefixes,
            ResourceComponent::Members(_) => Component::Members,
            ResourceComponent::Types(_) => Component::Types,
            ResourceComponent::Journals(_) => Component::Journals,
        }
    }
}

impl fmt::Display for ResourceComponent {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.route().map_err(|_| fmt::Error)?)
    }
}

impl CrossrefRoute for ResourceComponent {
    fn route(&self) -> Result<String> {
        match self {
            ResourceComponent::Works(c) => c.route(),
            ResourceComponent::Funders(c) => c.route(),
            ResourceComponent::Prefixes(c) => c.route(),
            ResourceComponent::Members(c) => c.route(),
            ResourceComponent::Types(c) => c.route(),
            ResourceComponent::Journals(c) => c.route(),
        }
    }
}

impl CrossrefQuery for ResourceComponent {
    fn resource_component(self) -> ResourceComponent {
        self
    }
}

/// Helper trait to mark filters in the query string
pub trait Filter: ParamFragment {}

impl<T: Filter> CrossrefQueryParam for Vec<T> {
    /// always use `filter` as the key
    fn param_key(&self) -> Cow<str> {
        Cow::Borrowed("filter")
    }

    /// filters are multi value and values are concat with `,`
    fn param_value(&self) -> Option<Cow<str>> {
        Some(Cow::Owned(
            self.iter()
                .map(ParamFragment::fragment)
                .collect::<Vec<_>>()
                .join(","),
        ))
    }
}

/// represents a key value pair inside a multi value query string parameter
pub trait ParamFragment {
    /// the key, or name, of the fragment
    fn key(&self) -> Cow<str>;

    /// the value of the fragment, if any
    fn value(&self) -> Option<Cow<str>>;

    /// key and value are concat using `:`
    fn fragment(&self) -> Cow<str> {
        if let Some(val) = self.value() {
            Cow::Owned(format!("{}:{}", self.key(), val))
        } else {
            self.key()
        }
    }
}

/// a trait used to capture parameters for the query string of the crossref api
pub trait CrossrefQueryParam {
    /// the key name of the parameter in the query string
    fn param_key(&self) -> Cow<str>;
    /// the value of the parameter, if any
    fn param_value(&self) -> Option<Cow<str>>;
    /// constructs the full parameter for the query string by combining the key and value
    fn param(&self) -> Cow<str> {
        if let Some(val) = self.param_value() {
            Cow::Owned(format!("{}={}", self.param_key(), val))
        } else {
            self.param_key()
        }
    }
}

impl<T: AsRef<str>> CrossrefQueryParam for (T, T) {
    fn param_key(&self) -> Cow<str> {
        Cow::Borrowed(self.0.as_ref())
    }

    fn param_value(&self) -> Option<Cow<str>> {
        Some(Cow::Borrowed(self.1.as_ref()))
    }
}

/// represents elements that constructs parts of the crossref request url
pub trait CrossrefRoute {
    /// constructs the route for the crossref api
    fn route(&self) -> Result<String>;
}

impl<T: CrossrefQueryParam> CrossrefRoute for AsRef<[T]> {
    fn route(&self) -> Result<String> {
        Ok(self
            .as_ref()
            .iter()
            .map(CrossrefQueryParam::param)
            .collect::<Vec<_>>()
            .join("&"))
    }
}

/// root level trait to construct full crossref api request urls
pub trait CrossrefQuery: CrossrefRoute {
    /// the resource component endpoint this route targets
    fn resource_component(self) -> ResourceComponent;

    /// constructs the full request url by concating the `base_path` with the `route`
    fn to_url(&self, base_path: &str) -> Result<String> {
        Ok(format!("{}{}", base_path, self.route()?))
    }

    //    fn to_json(&self) -> Result<Value> {
    //        unimplemented!()
    //    }
}

/// formats the topic for crossref by replacing all whitespaces whit `+`
pub(crate) fn format_query<T: AsRef<str>>(topic: T) -> String {
    topic
        .as_ref()
        .split_whitespace()
        .collect::<Vec<_>>()
        .join("+")
}

/// formats the individual topics of a query into the format crossref expects
/// returns a single String consisting of all words combined by '+'
pub(crate) fn format_queries<T: AsRef<str>>(topics: &[T]) -> String {
    topics
        .iter()
        .map(format_query)
        .collect::<Vec<_>>()
        .join("+")
}