crossref-client 0.2.1

An async Rust client for the Crossref REST API
Documentation
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
use crate::error::Result;
use crate::query::facet::FacetCount;
use crate::query::types::Type;
use crate::query::*;
use chrono::NaiveDate;
use std::borrow::Cow;

define_keyed_enum! {
/// A field of a [`Work`](crate::Work) that a query can ask crossref to return.
///
/// Passed to [`WorksQuery::elements`]; crossref returns only what was selected,
/// so every field left out is [`None`] on the resulting `Work`. Covers all 59
/// fields `/works` accepts, which `every_selectable_element_is_accepted_by_the_api`
/// pins against what the api reports.
WorkElement {
    DOI => "DOI",
    ISBN => "ISBN",
    ISSN => "ISSN",
    URL => "URL",
    Abstract_ => "abstract",
    Accepted => "accepted",
    AlternativeId => "alternative-id",
    Approved => "approved",
    Archive => "archive",
    ArticleNumber => "article-number",
    Assertion => "assertion",
    Author => "author",
    Chair => "chair",
    ClinicalTrialNumber => "clinical-trial-number",
    ContainerTitle => "container-title",
    ContentCreated => "content-created",
    ContentDomain => "content-domain",
    Contributor => "contributor",
    Created => "created",
    Degree => "degree",
    Deposited => "deposited",
    Editor => "editor",
    Event => "event",
    Funder => "funder",
    GroupTitle => "group-title",
    Indexed => "indexed",
    IsReferencedByCount => "is-referenced-by-count",
    IssnType => "issn-type",
    Issue => "issue",
    Issued => "issued",
    License => "license",
    Link => "link",
    Member => "member",
    OriginalTitle => "original-title",
    Page => "page",
    Posted => "posted",
    Prefix => "prefix",
    Published => "published",
    PublishedOnline => "published-online",
    PublishedPrint => "published-print",
    Publisher => "publisher",
    PublisherLocation => "publisher-location",
    Reference => "reference",
    ReferencesCount => "references-count",
    Relation => "relation",
    Resource => "resource",
    Score => "score",
    ShortContainerTitle => "short-container-title",
    ShortTitle => "short-title",
    StandardsBody => "standards-body",
    Subject => "subject",
    Subtitle => "subtitle",
    Title => "title",
    Translator => "translator",
    Type => "type",
    UpdatePolicy => "update-policy",
    UpdateTo => "update-to",
    UpdatedBy => "updated-by",
    Volume => "volume",
}
}

define_filter! {
/// Narrows a `/works` query. Every filter is ANDed with the others.
///
/// Covers all 90 filters `/works` accepts; crossref answers an unrecognised
/// one with [`Error::ValidationFailure`](crate::Error::ValidationFailure)
/// naming the ones it does know, which is what
/// `every_works_filter_is_accepted_by_the_api` pins this list against.
///
/// Note that `location` and `reference-visibility` are *not* among them --
/// `location` belongs to [`FundersFilter`](crate::query::funders::FundersFilter).
///
/// A value carrying a `,` cannot be sent at all, whatever the filter: crossref
/// splits the `filter` parameter on it after decoding, so the query is refused
/// with [`Error::UnsendableFilterValue`](crate::Error::UnsendableFilterValue).
WorksFilter;
markers {
    /// metadata which includes one or more funder entry
    HasFunder => "has-funder",
    /// metadata where a funder entry carries a Funder Registry DOI
    HasFunderDoi => "has-funder-doi",
    /// metadata where a funder entry carries a ROR id
    HasFunderRorId => "has-funder-ror-id",
    /// metadata for records that have any affiliation information
    HasAffiliation => "has-affiliation",
    /// metadata where an affiliation carries a ROR id
    HasAffiliationRorId => "has-affiliation-ror-id",
    /// metadata carrying a ROR id anywhere
    HasRorId => "has-ror-id",
    /// metadata that includes any `<license_ref>` elements
    HasLicense => "has-license",
    /// metadata that includes any full text `<resource>` elements
    HasFullText => "has-full-text",
    /// metadata for works that have a list of references
    HasReferences => "has-references",
    /// metadata which include name of archive partner
    HasArchive => "has-archive",
    /// metadata which includes one or more ORCIDs
    HasOrcid => "has-orcid",
    /// metadata which includes one or more ORCIDs where the depositing publisher claims to have witness the ORCID owner authenticate with ORCID
    HasAuthenticatedOrcid => "has-authenticated-orcid",
    /// metadata for records which include an abstract
    HasAbstract => "has-abstract",
    /// metadata for records which include a clinical trial number
    HasClinicalTrialNumber => "has-clinical-trial-number",
    /// metadata where the publisher records a domain name location for Crossmark content
    HasContentDomain => "has-content-domain",
    /// metadata where the publisher restricts Crossmark usage to content domains
    HasDomainRestriction => "has-domain-restriction",
    /// metadata for records that either assert or are the object of a relation
    HasRelation => "has-relation",
    /// metadata for records that carry an editorial update
    HasUpdate => "has-update",
    /// metadata for records that include a link to an editorial update policy
    HasUpdatePolicy => "has-update-policy",
    /// metadata for records with any assertions
    HasAssertion => "has-assertion",
    /// metadata for records that carry award information
    HasAward => "has-award",
    /// metadata for records deposited under more than one DOI
    HasAlias => "has-alias",
    /// metadata for records that are the primary DOI of an alias group
    HasPrimeDoi => "has-prime-doi",
    /// metadata for records that describe an event
    HasEvent => "has-event",
    /// metadata for records that represent editorial updates
    IsUpdate => "is-update",
}
values {
    /// metadata which include the `id` in FundRef data
    Funder(String) => "funder",
    /// metadata belonging to a DOI owner prefix `{owner_prefix}` (e.g. 10.1016 )
    Prefix(String) => "prefix",
    /// metadata belonging to a Crossref member
    Member(String) => "member",
    /// metadata describing the DOI
    Doi(String) => "doi",
    /// metadata where record has an ISSN = the value. Format is xxxx-xxxx
    Issn(String) => "issn",
    /// metadata where record has an ISBN = the value
    Isbn(String) => "isbn",
    /// metadata where `<orcid>` element's value = the value
    Orcid(String) => "orcid",
    ///  metadata which where value of archive partner is the value
    Archive(String) => "archive",
    /// metadata records whose article or serial are mentioned in the given value.
    /// Currently the only supported value is `doaj`
    Directory(String) => "directory",
    /// metadata for records that represent editorial updates to the DOI
    Updates(String) => "updates",
    /// metadata for records with a publication title exactly with an exact match
    ContainerTitle(String) => "container-title",
    /// metadata for records with an exact matching category label.
    /// Category labels come from [this list](https://www.elsevier.com/solutions/scopus/content) published by Scopus
    CategoryName(String) => "category-name",
    /// metadata for records with an exacty matching type label
    TypeName(String) => "type-name",
    /// metadata records whose type = value.
    /// Type must be an ID value from the list of types returned by the `/types` resource
    Type(Type) => "type",
    /// metadata for records with a matching group title, as deposited for posted content
    GroupTitle(String) => "group-title",
    /// metadata where the publisher records a particular domain name as the location Crossmark content will appear
    ContentDomain(String) => "content-domain",
    /// metadata for records carrying the given clinical trial number
    ClinicalTrialNumber(String) => "clinical-trial-number",
    /// metadata for records with the given alternative ID,
    /// which may be a publisher-specific ID, or any other identifier a publisher may have provided
    AlternativeId(String) => "alternative-id",
    /// metadata for records with a given article number
    ArticleNumber(String) => "article-number",
    /// metadata carrying the given [ROR](https://ror.org) id, the identifier
    /// crossref now models affiliations and funders with
    RorId(String) => "ror-id",
    /// metadata for editorial updates of the given kind, e.g. `correction` or `retraction`
    UpdateType(String) => "update-type",
    /// metadata where a funder's DOI was asserted by the value, either
    /// `crossref` or `publisher`
    FunderDoiAssertedBy(String) => "funder-doi-asserted-by",
    /// metadata for records with a particular named assertion
    Assertion(String) => "assertion",
    /// metadata for records with an assertion in a particular group
    AssertionGroup(String) => "assertion-group",
    /// metadata for records with a matching award number.
    /// Optionally combine with `award.funder`
    AwardNumber(String) => "award.number",
    /// metadata for records with an award with matching funder.
    /// Optionally combine with `award.number`
    AwardFunder(String) => "award.funder",
    /// metadata for records with an award of at least this amount
    GteAwardAmount(u64) => "gte-award-amount",
    /// metadata for records with an award of at most this amount
    LteAwardAmount(u64) => "lte-award-amount",
    /// metadata where `<license_ref>` value equals the value
    LicenseUrl(String) => "license.url",
    /// metadata where the `<license_ref>`'s applies_to attribute is
    LicenseVersion(String) => "license.version",
    /// metadata where difference between publication date and the `<license_ref>`'s start_date attribute is <= value (in days)
    LicenseDelay(i32) => "license.delay",
    /// metadata where `<resource>` element's content_version attribute is the value
    FullTextVersion(String) => "full-text.version",
    /// metadata where `<resource>` element's content_type attribute is value (e.g. `application/pdf)`
    FullTextType(String) => "full-text.type",
    /// metadata where `<resource>` link has one of the following intended applications: `text-mining`, `similarity-checking` or `unspecified`
    FullTextApplication(String) => "full-text.application",
    /// One of the relation types from the Crossref relations schema
    /// (e.g. `is-referenced-by`, `is-parent-of`, `is-preprint-of`)
    RelationType(String) => "relation.type",
    /// Relations where the object identifier matches the identifier provided
    RelationObject(String) => "relation.object",
    /// One of the identifier types from the Crossref relations schema (e.g. `doi`, `issn`)
    RelationObjectType(String) => "relation.object-type",
    /// metadata indexed since (inclusive)
    FromIndexDate(NaiveDate) => "from-index-date",
    /// metadata indexed before (inclusive)
    UntilIndexDate(NaiveDate) => "until-index-date",
    /// metadata last (re)deposited since (inclusive)
    FromDepositDate(NaiveDate) => "from-deposit-date",
    /// metadata last (re)deposited before (inclusive)
    UntilDepositDate(NaiveDate) => "until-deposit-date",
    /// Metadata updated since (inclusive) {date}.
    /// Currently the same as `from-deposit-date`
    FromUpdateDate(NaiveDate) => "from-update-date",
    /// Metadata updated before (inclusive) {date}.
    /// Currently the same as `until-deposit-date`
    UntilUpdateDate(NaiveDate) => "until-update-date",
    /// metadata first deposited since (inclusive)
    FromCreatedDate(NaiveDate) => "from-created-date",
    /// metadata first deposited before (inclusive)
    UntilCreatedDate(NaiveDate) => "until-created-date",
    /// metadata where published date is since (inclusive)
    FromPubDate(NaiveDate) => "from-pub-date",
    /// metadata where published date is before (inclusive)
    UntilPubDate(NaiveDate) => "until-pub-date",
    /// metadata where online published date is since (inclusive)
    FromOnlinePubDate(NaiveDate) => "from-online-pub-date",
    /// metadata where online published date is before (inclusive)
    UntilOnlinePubDate(NaiveDate) => "until-online-pub-date",
    /// metadata where print published date is since (inclusive)
    FromPrintPubDate(NaiveDate) => "from-print-pub-date",
    /// metadata where print published date is before (inclusive)
    UntilPrintPubDate(NaiveDate) => "until-print-pub-date",
    /// metadata where posted date is since (inclusive)
    FromPostedDate(NaiveDate) => "from-posted-date",
    /// metadata where posted date is before (inclusive)
    UntilPostedDate(NaiveDate) => "until-posted-date",
    /// metadata where accepted date is since (inclusive)
    FromAcceptedDate(NaiveDate) => "from-accepted-date",
    /// metadata where accepted date is before (inclusive)
    UntilAcceptedDate(NaiveDate) => "until-accepted-date",
    /// metadata where approved date is since (inclusive)
    FromApprovedDate(NaiveDate) => "from-approved-date",
    /// metadata where approved date is before (inclusive)
    UntilApprovedDate(NaiveDate) => "until-approved-date",
    /// metadata where an award was made since (inclusive)
    FromAwardedDate(NaiveDate) => "from-awarded-date",
    /// metadata where an award was made before (inclusive)
    UntilAwardedDate(NaiveDate) => "until-awarded-date",
    /// metadata where issued date is since (inclusive)
    FromIssuedDate(NaiveDate) => "from-issued-date",
    /// metadata where issued date is before (inclusive)
    UntilIssuedDate(NaiveDate) => "until-issued-date",
    /// metadata for events starting since (inclusive)
    FromEventStartDate(NaiveDate) => "from-event-start-date",
    /// metadata for events starting before (inclusive)
    UntilEventStartDate(NaiveDate) => "until-event-start-date",
    /// metadata for events ending since (inclusive)
    FromEventEndDate(NaiveDate) => "from-event-end-date",
    /// metadata for events ending before (inclusive)
    UntilEventEndDate(NaiveDate) => "until-event-end-date",
}
}

define_field_queries! {
    /// titles, including the subtitle
    Title => "title" / title,
    /// the title of the containing work, aka `publication.name`
    ContainerTitle => "container-title" / container_title,
    /// author given and family names
    Author => "author" / author,
    /// editor given and family names
    Editor => "editor" / editor,
    /// chair given and family names
    Chair => "chair" / chair,
    /// translator given and family names
    Translator => "translator" / translator,
    /// author, editor, chair and translator given and family names
    Contributor => "contributor" / contributor,
    /// bibliographic information, useful for citation look up. Includes
    /// titles, authors, ISSNs and publication years
    Bibliographic => "bibliographic" / bibliographic,
    /// contributor affiliations
    Affiliation => "affiliation" / affiliation,
    /// the degree a dissertation was awarded for
    Degree => "degree" / degree,
    /// the description deposited for posted content
    Description => "description" / description,
    /// the short form of an event's name
    EventAcronym => "event-acronym" / event_acronym,
    /// where an event was held
    EventLocation => "event-location" / event_location,
    /// an event's name
    EventName => "event-name" / event_name,
    /// who sponsored an event
    EventSponsor => "event-sponsor" / event_sponsor,
    /// an event's theme
    EventTheme => "event-theme" / event_theme,
    /// the name of a funding body, for works whose funder carries no registry DOI
    FunderName => "funder-name" / funder_name,
    /// where the publisher is located
    PublisherLocation => "publisher-location" / publisher_location,
    /// the publisher's name
    PublisherName => "publisher-name" / publisher_name,
    /// the short form of a standards body's name
    StandardsBodyAcronym => "standards-body-acronym" / standards_body_acronym,
    /// the name of the body that issued a standard
    StandardsBodyName => "standards-body-name" / standards_body_name,
}

impl CrossrefQueryParam for FieldQuery {
    fn params(&self) -> Vec<(Cow<'_, str>, Cow<'_, str>)> {
        vec![(
            Cow::Borrowed(self.name()),
            Cow::Owned(format_query(self.value())),
        )]
    }
}

/// limits from where and how many `Work` items should be returned
#[derive(Debug, Clone)]
pub enum WorkResultControl {
    /// use the standard ResultControl available for all components
    Standard(ResultControl),
    /// If you are expecting results beyond 10K, then use a cursor to deep page through the results
    Cursor {
        /// the cursor token provided by crossref when initially set to a value of `*`
        token: Option<String>,
        /// limit the results
        rows: Option<usize>,
    },
}

impl WorkResultControl {
    /// set a cursor with `*` value, a new cursor will be provided in the `next-cursor` field of the result
    pub fn new_cursor() -> Self {
        WorkResultControl::Cursor {
            token: None,
            rows: None,
        }
    }

    /// create a new Cursor with only a token value
    pub fn cursor(token: &str) -> Self {
        WorkResultControl::Cursor {
            token: Some(token.to_string()),
            rows: None,
        }
    }
}

impl Default for WorkResultControl {
    fn default() -> Self {
        WorkResultControl::new_cursor()
    }
}

impl CrossrefQueryParam for WorkResultControl {
    fn params(&self) -> Vec<(Cow<'_, str>, Cow<'_, str>)> {
        match self {
            WorkResultControl::Standard(standard) => standard.params(),
            WorkResultControl::Cursor { token, rows } => {
                let mut params = vec![(
                    Cow::Borrowed("cursor"),
                    Cow::Borrowed(token.as_deref().unwrap_or("*")),
                )];
                if let Some(rows) = rows {
                    params.push((Cow::Borrowed("rows"), Cow::Owned(rows.to_string())));
                }
                params
            }
        }
    }
}
///
/// Retrieve a publication by DOI
///
/// # Example
///
/// ```no_run
/// use crossref_client::Works;
///
/// let works = Works::doi("10.1037/0003-066X.59.1.29");
/// ```
///
/// Target the agency of a specific publication, where the str supplied is corresponded to the publication's DOI
///
/// # Example
///
/// ```no_run
/// use crossref_client::Works;
///
/// let works = Works::agency_for_doi("10.1037/0003-066X.59.1.29");
/// ```
#[derive(Debug, Clone)]
pub enum Works {
    /// target a Work by a specific id
    Identifier(String),
    /// target Works by a query
    Query(WorksQuery),
    /// return the registration agency for a DOI
    Agency(String),
}

impl Works {
    /// create a new `Works::Identifier` by converting `doi` to a `String`
    pub fn doi(doi: &str) -> Self {
        Works::Identifier(doi.to_string())
    }
    /// create a new `Works::Agency` targeting the registration agency for the DOI
    pub fn agency_for_doi(doi: &str) -> Self {
        Works::Agency(doi.to_string())
    }
}

impl CrossrefRoute for Works {
    fn route(&self) -> Result<String> {
        match self {
            Works::Identifier(s) => Ok(format!("{}/{}", Component::Works.route()?, s)),
            Works::Agency(s) => Ok(format!("{}/{}/agency", Component::Works.route()?, s)),
            Works::Query(query) => query.route(),
        }
    }
}

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

/// Wraps queries that target `WorkList`, either directly or combined
#[derive(Debug, Clone)]
pub enum WorkListQuery {
    /// Target `Works` directly
    Works(WorksQuery),
    /// Target the corresponding `Works` of a specific `Component`
    Combined {
        /// the component whose works are being targeted
        primary_component: WorksComponent,
        /// the component id, and the query to narrow its works with
        ident: WorksIdentQuery,
    },
}

impl WorkListQuery {
    /// the underlying `WorksQuery`
    pub fn query(&self) -> &WorksQuery {
        match self {
            WorkListQuery::Works(query) => query,
            WorkListQuery::Combined { ident, .. } => &ident.query,
        }
    }

    /// mut reference to the underlying `Worksquery`
    pub fn query_mut(&mut self) -> &mut WorksQuery {
        match self {
            WorkListQuery::Works(query) => query,
            WorkListQuery::Combined { ident, .. } => &mut ident.query,
        }
    }
}

impl From<WorksQuery> for WorkListQuery {
    fn from(val: WorksQuery) -> Self {
        WorkListQuery::Works(val)
    }
}

impl<T: ToString> From<T> for WorkListQuery {
    fn from(term: T) -> Self {
        WorkListQuery::Works(WorksQuery::new(term))
    }
}

impl CrossrefRoute for WorkListQuery {
    fn route(&self) -> Result<String> {
        match self {
            WorkListQuery::Works(query) => query.route(),
            WorkListQuery::Combined {
                primary_component,
                ident,
            } => Ok(format!(
                "{}/{}{}",
                primary_component.route()?,
                ident.id,
                ident.query.route()?
            )),
        }
    }
}

impl CrossrefQuery for WorkListQuery {
    fn resource_component(self) -> ResourceComponent {
        match self {
            WorkListQuery::Works(query) => ResourceComponent::Works(Works::Query(query)),
            WorkListQuery::Combined {
                primary_component,
                ident,
            } => match primary_component {
                WorksComponent::Funders => ResourceComponent::Funders(Funders::Works(ident)),
                WorksComponent::Journals => ResourceComponent::Journals(Journals::Works(ident)),
                WorksComponent::Members => ResourceComponent::Members(Members::Works(ident)),
                WorksComponent::Prefixes => ResourceComponent::Prefixes(Prefixes::Works(ident)),
                WorksComponent::Types => ResourceComponent::Types(Types::Works(ident)),
            },
        }
    }
}

/// Target `Works` as secondary resource component
///
/// # Example
///
/// ```no_run
/// use crossref_client::{WorksIdentQuery, WorksQuery};
///
/// let combined = WorksIdentQuery::new("100000015", WorksQuery::new("ontologies"));
///
/// ```
/// Is equal to create a `WorksIdentQuery` from a `WorksQuery`
///
/// ```no_run
/// use crossref_client::WorksQuery;
///
/// let combined = WorksQuery::new("ontologies").into_ident("100000015");
///
/// ```
/// helper struct to capture an id for a `Component` other than `/works` and an additional query for the `/works` route
#[derive(Debug, Clone)]
pub struct WorksIdentQuery {
    /// the id of an component item
    pub id: String,
    /// the query to filter the works results
    pub query: WorksQuery,
}

impl WorksIdentQuery {
    /// create a new Ident Query for the `id`
    pub fn new<T: Into<String>>(id: T, query: WorksQuery) -> Self {
        WorksIdentQuery {
            id: id.into(),
            query,
        }
    }
}

/// Trait to determine that the type can be used in a combined query
pub trait WorksCombiner {
    /// the primary component of this type
    fn primary_component() -> WorksComponent;

    /// construct a new type
    fn ident_query(ident: WorksIdentQuery) -> Self;

    /// the combined crossref route
    fn combined_route(ident: &WorksIdentQuery) -> Result<String> {
        Ok(format!(
            "{}/{}{}",
            Self::primary_component().route()?,
            ident.id,
            ident.query.route()?
        ))
    }

    /// create a new combined `WorkListQuery` with the primary component
    fn work_list_query(ident: WorksIdentQuery) -> WorkListQuery {
        WorkListQuery::Combined {
            primary_component: Self::primary_component(),
            ident,
        }
    }
}

macro_rules! impl_combiner {
    ($($name:ident,)*) => {
        $(
        impl WorksCombiner for $name {
            fn primary_component() -> WorksComponent {
                WorksComponent::$name
            }

            fn ident_query(ident: WorksIdentQuery) -> Self {
                $name::Works(ident)
            }
        }
        )+
    };
}

impl_combiner!(Journals, Funders, Members, Prefixes, Types,);

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

    /// creates an new `WorksQuery` with the desired sample size that will result in
    /// a request for random dois
    pub fn random(len: usize) -> Self {
        WorksQuery::default().sample(len)
    }

    /// Convenience method to create a new `WorksQuery` with a term directly
    pub fn new<T: ToString>(query: T) -> Self {
        WorksQuery::empty().query(query)
    }

    /// Ask for `len` random works instead of a search, or for a search if
    /// given [`None`]. Crossref ignores every other parameter when it is set.
    pub fn sample(mut self, len: impl Into<Option<usize>>) -> Self {
        self.sample = len.into();
        self
    }

    /// add a new free form query
    pub fn query<T: ToString>(mut self, query: T) -> Self {
        self.free_form_queries.push(query.to_string());
        self
    }

    /// Create a new query for the topics renear+ontologies
    ///
    /// # Example
    ///
    /// ```no_run
    /// use crossref_client::WorksQuery;
    ///
    /// let query = WorksQuery::default().queries(&["renear", "ontologies"]);
    /// ```
    /// add a bunch of free form query terms
    pub fn queries<T: ToString>(mut self, queries: &[T]) -> Self {
        self.free_form_queries
            .extend(queries.iter().map(T::to_string));
        self
    }

    /// add a new field query form query
    pub fn field_query(mut self, query: FieldQuery) -> Self {
        self.field_queries.push(query);
        self
    }

    /// Narrow the response to the given fields.
    ///
    /// Crossref returns only what is selected, so every field left out is
    /// [`None`] on the resulting [`Work`](crate::Work).
    ///
    /// [`WorkElement::DOI`] is always sent, whether or not it is asked for: it
    /// is the one field [`Work`](crate::Work) requires, so a response without
    /// it could not be read back at all.
    pub fn elements(mut self, element: Vec<WorkElement>) -> Self {
        self.elements.extend(element);
        self
    }

    /// ```no_run
    /// use crossref_client::{FieldQuery,WorksQuery};
    ///
    /// let query = WorksQuery::default().field_queries(vec![FieldQuery::title("room at the bottom"), FieldQuery::author("richard feynman")]);
    /// ```
    /// add a bunch of free form query terms
    pub fn field_queries(mut self, queries: Vec<FieldQuery>) -> Self {
        self.field_queries.extend(queries);
        self
    }

    /// add a new filter to the query
    pub fn filter(mut self, filter: WorksFilter) -> Self {
        self.filter.push(filter);
        self
    }

    /// Sort the results by a field, or by relevance if given [`None`].
    pub fn sort(mut self, sort: impl Into<Option<Sort>>) -> Self {
        self.sort = sort.into();
        self
    }

    /// Order the results, or leave the order to crossref if given [`None`].
    pub fn order(mut self, order: impl Into<Option<Order>>) -> Self {
        self.order = order.into();
        self
    }

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

    /// set the cursor for result control deep paging
    pub fn next_cursor(mut self, cursor: &str) -> Self {
        let rows = match self.result_control {
            Some(WorkResultControl::Standard(ResultControl::Rows(rows))) => Some(rows),
            _ => None,
        };
        self.result_control = Some(WorkResultControl::Cursor {
            token: Some(cursor.to_string()),
            rows,
        });
        self
    }

    /// set an empty cursor
    pub fn new_cursor(mut self) -> Self {
        self.result_control = Some(WorkResultControl::new_cursor());
        self
    }
    /// Limit the results, or take crossref's default page if given [`None`].
    pub fn result_control(mut self, result_control: impl Into<Option<WorkResultControl>>) -> Self {
        self.result_control = result_control.into();
        self
    }

    /// Wrap the query in a combined query.
    ///
    /// # Example
    /// Create a Funders Query that targets all works of a funder with id `funder id`.
    ///
    /// ```no_run
    /// # use crossref_client::{WorksQuery, Funders};
    /// let funders: Funders = WorksQuery::default().into_combined("funder id");
    /// ```
    pub fn into_combined<W: WorksCombiner>(self, id: &str) -> W {
        W::ident_query(self.into_ident(id))
    }

    /// Bind the query to a specific id of a primary endpoint element
    pub fn into_ident(self, id: &str) -> WorksIdentQuery {
        WorksIdentQuery::new(id, self)
    }

    /// wrap this query in new `WorkListQuery` that targets the `/works` route of a primary component with an id.
    /// The query will evaluate to the same as [`WorksQuery::into_combined`]
    ///
    /// # Example
    ///
    /// Create a query that targets all `Works` of a funder with id `funder id`
    ///
    /// ```no_run
    /// # use crossref_client::{WorksQuery, Funders};
    /// let query = WorksQuery::default()
    ///     .into_combined_query::<Funders>("funder id");
    ///
    /// ```
    pub fn into_combined_query<W: WorksCombiner>(self, id: &str) -> WorkListQuery {
        W::work_list_query(self.into_ident(id))
    }
}

/// Used to construct a query that targets crossref `Works` elements
///
/// # Example
///
/// ```no_run
/// use crossref_client::{Order, WorksQuery};
///
/// // create a new query for topcis machine+learning ordered desc
/// let query = WorksQuery::new("machine learning").order(Order::Desc);
/// ```
///
/// Each query parameter is ANDed
#[derive(Debug, Clone, Default)]
pub struct WorksQuery {
    /// search by non specific query
    pub free_form_queries: Vec<String>,
    /// match only particular fields of metadata
    pub field_queries: Vec<FieldQuery>,
    /// filter to apply while querying
    pub filter: Vec<WorksFilter>,
    /// sort results by a certain field and
    pub sort: Option<Sort>,
    /// set the sort order to `asc` or `desc`
    pub order: Option<Order>,
    /// elements to return
    pub elements: Vec<WorkElement>,
    /// enable facet information in responses
    pub facets: Vec<FacetCount>,
    /// deep page through `/works` result sets
    pub result_control: Option<WorkResultControl>,
    /// request random dois
    /// if set all other parameters are ignored
    pub sample: Option<usize>,
}

/// Renders `elements` into a `select` value, with [`WorkElement::DOI`] in it.
///
/// A [`Work`](crate::Work) requires a DOI -- it is the identifier every other
/// field hangs off, and the one thing crossref assigns rather than accepting
/// from the depositing member -- so a page selected without it deserializes
/// into nothing at all. Sending it regardless costs one field per work and is
/// the difference between a narrowed response and an [`Error::Serde`] the
/// caller cannot act on.
///
/// [`Error::Serde`]: crate::Error::Serde
fn select_value(elements: &[WorkElement]) -> String {
    let selected = elements.iter().map(WorkElement::name);

    if elements.contains(&WorkElement::DOI) {
        selected.collect::<Vec<_>>().join(",")
    } else {
        std::iter::once(WorkElement::DOI.name())
            .chain(selected)
            .collect::<Vec<_>>()
            .join(",")
    }
}

impl CrossrefRoute for WorksQuery {
    fn route(&self) -> Result<String> {
        let mut params: Vec<(Cow<'_, str>, Cow<'_, str>)> = Vec::new();

        if let Some(sample) = self.sample {
            params.push((Cow::Borrowed("sample"), Cow::Owned(sample.to_string())));
            return Ok(format!(
                "{}{}",
                Component::Works.route()?,
                encode::query_string(&params)
            ));
        }

        if !self.free_form_queries.is_empty() {
            params.push((
                Cow::Borrowed("query"),
                Cow::Owned(format_queries(&self.free_form_queries)),
            ));
        }
        params.extend(
            self.field_queries
                .iter()
                .flat_map(CrossrefQueryParam::params),
        );
        if !self.filter.is_empty() {
            reject_unsendable_filters(&self.filter)?;
            params.extend(self.filter.params());
        }
        if !self.elements.is_empty() {
            params.push((
                Cow::Borrowed("select"),
                Cow::Owned(select_value(&self.elements)),
            ));
        }
        if !self.facets.is_empty() {
            params.extend(self.facets.params());
        }
        if let Some(sort) = &self.sort {
            params.extend(sort.params());
        }
        if let Some(order) = &self.order {
            params.extend(order.params());
        }
        if let Some(rc) = &self.result_control {
            params.extend(rc.params());
        }

        Ok(format!(
            "{}{}",
            Component::Works.route()?,
            encode::query_string(&params)
        ))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// `from_name` is how a caller that learns a filter at runtime reaches the
    /// same enum a caller writing one out reaches, so every kind of value a
    /// filter carries has to read back from the way it renders.
    #[test]
    fn a_filter_reads_back_from_the_name_and_value_it_renders_as() {
        let read_back = |filter: &WorksFilter| {
            let value = ParamFragment::value(filter);
            WorksFilter::from_name(filter.name(), value.as_deref()).unwrap()
        };

        for filter in [
            WorksFilter::HasAbstract,
            WorksFilter::Doi("10.1016/j.marpol.2021.104855".to_string()),
            WorksFilter::FromPubDate(NaiveDate::from_ymd_opt(2021, 1, 1).unwrap()),
            WorksFilter::LicenseDelay(30),
            WorksFilter::GteAwardAmount(1_000_000),
            WorksFilter::TypeName("journal-article".to_string()),
        ] {
            assert_eq!(filter, read_back(&filter));
        }
    }

    #[test]
    fn a_marker_is_asked_for_as_present_and_nothing_else() {
        assert_eq!(
            Ok(WorksFilter::HasAbstract),
            WorksFilter::from_name("has-abstract", None)
        );
        assert_eq!(
            Ok(WorksFilter::HasAbstract),
            WorksFilter::from_name("has-abstract", Some("true"))
        );
        assert_eq!(
            Err(FilterParseError::MarkerWithValue {
                name: "has-abstract".to_string()
            }),
            WorksFilter::from_name("has-abstract", Some("false"))
        );
    }

    #[test]
    fn a_filter_that_narrows_by_a_value_is_not_a_filter_without_one() {
        assert_eq!(
            Err(FilterParseError::MissingValue {
                name: "issn".to_string()
            }),
            WorksFilter::from_name("issn", None)
        );
    }

    /// The parse is where a value crossref would refuse is caught, so a date
    /// that is not a date costs no request.
    #[test]
    fn a_value_the_filter_cannot_narrow_by_is_refused_here() {
        assert_eq!(
            Err(FilterParseError::UnreadableValue {
                name: "from-pub-date".to_string(),
                value: "last tuesday".to_string(),
            }),
            WorksFilter::from_name("from-pub-date", Some("last tuesday"))
        );
        assert_eq!(
            Err(FilterParseError::UnreadableValue {
                name: "license.delay".to_string(),
                value: "a while".to_string(),
            }),
            WorksFilter::from_name("license.delay", Some("a while"))
        );
    }

    #[test]
    fn a_name_no_filter_goes_by_is_refused_with_the_name_that_was_asked_for() {
        assert_eq!(
            Err(FilterParseError::UnknownName {
                name: "has-astract".to_string()
            }),
            WorksFilter::from_name("has-astract", None)
        );
        // `location` belongs to /funders, and /works answers it with a 400
        assert!(WorksFilter::from_name("location", Some("Norway")).is_err());
    }

    #[test]
    fn every_filter_the_route_takes_is_reachable_by_name() {
        for name in WorksFilter::ALL_NAMES {
            // one value of every kind a filter can carry, since which kind
            // goes with which name is the thing under test
            let filter = WorksFilter::from_name(name, Some("2021-01-01"))
                .or_else(|_| WorksFilter::from_name(name, Some("1")))
                .or_else(|_| WorksFilter::from_name(name, Some("journal-article")))
                .or_else(|_| WorksFilter::from_name(name, None))
                .unwrap_or_else(|err| panic!("`{name}` is named by nothing: {err}"));

            assert_eq!(*name, filter.name());
        }
    }

    #[test]
    fn serialize_works_ident() {
        let works = Works::doi("10.1037/0003-066X.59.1.29");

        assert_eq!("/works/10.1037/0003-066X.59.1.29", &works.route().unwrap())
    }

    #[test]
    fn sample_query_keeps_the_works_route() {
        // a bare `sample=n` used to be returned without the `/works?` prefix,
        // which concatenated straight onto the base url
        let route = WorksQuery::random(10).route().unwrap();

        assert_eq!("/works?sample=10", &route);

        let query: WorkListQuery = WorksQuery::random(10).into();
        assert_eq!(
            "https://api.crossref.org/works?sample=10",
            &query.to_url("https://api.crossref.org").unwrap()
        );
    }

    #[test]
    fn filter_names_have_no_stray_whitespace() {
        let route = WorksQuery::empty()
            .filter(WorksFilter::HasClinicalTrialNumber)
            .route()
            .unwrap();

        assert_eq!("/works?filter=has-clinical-trial-number:true", &route);
    }

    #[test]
    fn cursor_with_rows_renders_two_parameters() {
        let query = WorksQuery::empty().result_control(WorkResultControl::Cursor {
            token: Some("abc".to_string()),
            rows: Some(20),
        });

        assert_eq!("/works?cursor=abc&rows=20", &query.route().unwrap());
    }

    #[test]
    fn new_cursor_renders_the_wildcard_token() {
        let query = WorksQuery::empty().new_cursor();

        assert_eq!("/works?cursor=*", &query.route().unwrap());
    }

    #[test]
    fn a_query_term_can_no_longer_inject_a_parameter() {
        // `/works?query=R&D` reaches crossref as `query=R` plus a stray `D`
        assert_eq!(
            "/works?query=R%26D",
            &WorksQuery::new("R&D").route().unwrap()
        );
    }

    #[test]
    fn filter_and_field_query_values_are_encoded_too() {
        let route = WorksQuery::empty()
            .field_query(FieldQuery::container_title("Ecology & Evolution"))
            .filter(WorksFilter::ContainerTitle("Q&A".to_string()))
            .route()
            .unwrap();

        assert_eq!(
            "/works?query.container-title=Ecology%20%26%20Evolution&filter=container-title:Q%26A",
            &route
        );
    }

    #[test]
    fn an_empty_query_targets_the_bare_works_route() {
        assert_eq!("/works", &WorksQuery::empty().route().unwrap());
    }

    #[test]
    fn selected_elements_render_as_one_comma_separated_parameter() {
        let route = WorksQuery::empty()
            .elements(vec![WorkElement::DOI, WorkElement::Title])
            .route()
            .unwrap();

        assert_eq!("/works?select=DOI,title", &route);
    }

    #[test]
    fn a_select_that_omits_the_doi_still_asks_for_it() {
        // `Work` requires a DOI, so `select=title` would come back as a page of
        // works none of which deserialize
        let route = WorksQuery::empty()
            .elements(vec![WorkElement::Title])
            .route()
            .unwrap();

        assert_eq!("/works?select=DOI,title", &route);
    }

    #[test]
    fn a_filter_value_with_a_comma_in_it_is_refused() {
        // crossref splits the `filter` value on `,` after decoding it, so this
        // would arrive as `container-title:A` plus a filter called ` B`
        let error = WorksQuery::empty()
            .filter(WorksFilter::ContainerTitle("A, B".to_string()))
            .route()
            .expect_err("crossref cannot read this back");

        let crate::Error::UnsendableFilterValue { filter, value } = error else {
            panic!("expected an unsendable filter value, got {error:?}");
        };
        assert_eq!("container-title", filter);
        assert_eq!("A, B", value);
    }

    #[test]
    fn rows_offset_renders_two_parameters() {
        let query = WorksQuery::empty().result_control(WorkResultControl::Standard(
            ResultControl::RowsOffset {
                rows: 10,
                offset: 20,
            },
        ));

        assert_eq!("/works?rows=10&offset=20", &query.route().unwrap());
    }

    #[test]
    fn referenced_by_count_uses_the_crossref_spelling() {
        // `is-reference-by-count` is rejected by the api with a 400
        assert_eq!("is-referenced-by-count", Sort::IsReferencedByCount.as_str());
    }
}