use crate::error::Result;
pub use crate::query::funders::{Funders, FundersQuery};
pub use crate::query::journals::{Journals, JournalsQuery};
pub use crate::query::licenses::{Licenses, LicensesQuery};
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;
pub use crate::query::works::{WorksIdentQuery, WorksQuery};
use core::fmt::Debug;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::fmt;
use std::str::FromStr;
pub(crate) trait FilterValue {
fn render(&self) -> Cow<'_, str>;
}
impl FilterValue for String {
fn render(&self) -> Cow<'_, str> {
Cow::Borrowed(self)
}
}
impl FilterValue for i32 {
fn render(&self) -> Cow<'_, str> {
Cow::Owned(self.to_string())
}
}
impl FilterValue for u64 {
fn render(&self) -> Cow<'_, str> {
Cow::Owned(self.to_string())
}
}
impl FilterValue for chrono::NaiveDate {
fn render(&self) -> Cow<'_, str> {
Cow::Owned(self.format("%Y-%m-%d").to_string())
}
}
impl FilterValue for types::Type {
fn render(&self) -> Cow<'_, str> {
Cow::Borrowed(self.id())
}
}
macro_rules! define_filter {
(
$(#[$meta:meta])*
$name:ident;
markers { $($(#[$m_doc:meta])* $m_variant:ident => $m_key:literal,)* }
values { $($(#[$v_doc:meta])* $v_variant:ident($v_ty:ty) => $v_key:literal,)* }
) => {
$(#[$meta])*
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum $name {
$($(#[$m_doc])* $m_variant,)*
$($(#[$v_doc])* $v_variant($v_ty),)*
}
impl $name {
pub const ALL_NAMES: &'static [&'static str] = &[$($m_key,)* $($v_key,)*];
pub fn name(&self) -> &'static str {
match self {
$($name::$m_variant => $m_key,)*
$($name::$v_variant(_) => $v_key,)*
}
}
}
impl $crate::query::ParamFragment for $name {
fn key(&self) -> ::std::borrow::Cow<'_, str> {
::std::borrow::Cow::Borrowed(self.name())
}
fn value(&self) -> Option<::std::borrow::Cow<'_, str>> {
match self {
$($name::$m_variant => Some(::std::borrow::Cow::Borrowed("true")),)*
$($name::$v_variant(value) => {
Some($crate::query::FilterValue::render(value))
})*
}
}
}
impl $crate::query::Filter for $name {}
};
}
macro_rules! impl_list_query {
($(#[$meta:meta])* $name:ident $(, filter = $filter:ident)?) => {
$(#[$meta])*
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct $name {
/// search by non specific query
pub queries: Vec<String>,
$(
pub filter: Vec<$filter>,
)?
pub result_control: Option<ResultControl>,
}
impl $name {
$(
/// add a new filter to the query
pub fn filter(mut self, filter: $filter) -> Self {
self.filter.push(filter);
self
}
)?
pub fn empty() -> Self {
Self::default()
}
pub fn new<T: ToString>(query: T) -> Self {
Self::empty().query(query)
}
pub fn query<T: ToString>(mut self, query: T) -> Self {
self.queries.push(query.to_string());
self
}
pub fn queries<T: ToString>(mut self, queries: &[T]) -> Self {
self.queries.extend(queries.iter().map(T::to_string));
self
}
pub fn result_control(
mut self,
result_control: impl Into<Option<ResultControl>>,
) -> Self {
self.result_control = result_control.into();
self
}
}
impl CrossrefRoute for $name {
fn route(&self) -> Result<String> {
let mut params: Vec<(Cow<'_, str>, Cow<'_, str>)> = Vec::new();
if !self.queries.is_empty() {
params.push((
Cow::Borrowed("query"),
Cow::Owned(format_queries(&self.queries)),
));
}
$(
let filters: &[$filter] = &self.filter;
if !filters.is_empty() {
$crate::query::reject_unsendable_filters(filters)?;
params.extend(self.filter.params());
}
)?
if let Some(rc) = &self.result_control {
params.extend(rc.params());
}
Ok(encode::query_string(¶ms))
}
}
};
}
macro_rules! define_keyed_enum {
(
$(#[$meta:meta])*
$name:ident { $($(#[$doc:meta])* $variant:ident => $key:literal,)* }
) => {
$(#[$meta])*
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum $name {
$(
#[doc = concat!("the `", $key, "` field")]
$(#[$doc])*
$variant,
)*
}
impl $name {
pub fn name(&self) -> &'static str {
match self { $($name::$variant => $key,)* }
}
pub const ALL: &'static [$name] = &[$($name::$variant,)*];
}
};
}
macro_rules! define_field_queries {
($($(#[$doc:meta])* $variant:ident => $key:literal / $ctor:ident,)*) => {
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FieldQuery {
$(
#[doc = concat!("matches against `", $key, "`: ")]
$(#[$doc])*
$variant(String),
)*
}
impl FieldQuery {
pub const ALL_FIELDS: &'static [&'static str] = &[$($key,)*];
pub fn name(&self) -> &'static str {
match self { $(FieldQuery::$variant(_) => concat!("query.", $key),)* }
}
pub fn field(&self) -> &'static str {
match self { $(FieldQuery::$variant(_) => $key,)* }
}
pub fn value(&self) -> &str {
match self { $(FieldQuery::$variant(value) => value,)* }
}
$(
#[doc = concat!("a query against `", $key, "`")]
pub fn $ctor(value: impl Into<String>) -> Self {
FieldQuery::$variant(value.into())
}
)*
}
};
}
pub(crate) mod encode;
pub mod facet;
pub mod funders;
pub mod journals;
pub mod licenses;
pub mod members;
pub mod prefixes;
pub mod types;
pub mod works;
#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum Visibility {
Open,
Limited,
Closed,
}
impl Visibility {
pub fn as_str(&self) -> &str {
match self {
Visibility::Open => "open",
Visibility::Limited => "limited",
Visibility::Closed => "closed",
}
}
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Order {
Asc,
Desc,
}
impl Order {
pub fn as_str(&self) -> &str {
match self {
Order::Asc => "asc",
Order::Desc => "desc",
}
}
}
impl FromStr for Order {
type Err = String;
fn from_str(s: &str) -> ::std::result::Result<Self, Self::Err> {
match s {
"asc" => Ok(Order::Asc),
"desc" => Ok(Order::Desc),
other => Err(format!("Unable to convert {} to Order", other)),
}
}
}
impl CrossrefQueryParam for Order {
fn params(&self) -> Vec<(Cow<'_, str>, Cow<'_, str>)> {
vec![(Cow::Borrowed("order"), Cow::Borrowed(self.as_str()))]
}
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Sort {
Score,
Updated,
Deposited,
Indexed,
Published,
PublishedPrint,
PublishedOnline,
Issued,
IsReferencedByCount,
ReferenceCount,
Created,
Relevance,
}
impl Sort {
pub const ALL: &'static [Sort] = &[
Sort::Score,
Sort::Updated,
Sort::Deposited,
Sort::Indexed,
Sort::Published,
Sort::PublishedPrint,
Sort::PublishedOnline,
Sort::Issued,
Sort::IsReferencedByCount,
Sort::ReferenceCount,
Sort::Created,
Sort::Relevance,
];
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-referenced-by-count",
Sort::ReferenceCount => "references-count",
Sort::Created => "created",
Sort::Relevance => "relevance",
}
}
}
impl FromStr for Sort {
type Err = String;
fn from_str(s: &str) -> ::std::result::Result<Self, Self::Err> {
match s {
"score" => Ok(Sort::Score),
"updated" => Ok(Sort::Updated),
"deposited" => Ok(Sort::Deposited),
"indexed" => Ok(Sort::Indexed),
"published" => Ok(Sort::Published),
"published-print" => Ok(Sort::PublishedPrint),
"published-online" => Ok(Sort::PublishedOnline),
"issued" => Ok(Sort::Issued),
"is-referenced-by-count" => Ok(Sort::IsReferencedByCount),
"references-count" => Ok(Sort::ReferenceCount),
"created" => Ok(Sort::Created),
"relevance" => Ok(Sort::Relevance),
other => Err(format!("Unable to convert {} to Sort", other)),
}
}
}
impl CrossrefQueryParam for Sort {
fn params(&self) -> Vec<(Cow<'_, str>, Cow<'_, str>)> {
vec![(Cow::Borrowed("sort"), Cow::Borrowed(self.as_str()))]
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ResultControl {
Rows(usize),
Offset(usize),
RowsOffset {
rows: usize,
offset: usize,
},
Sample(usize),
}
impl CrossrefQueryParam for ResultControl {
fn params(&self) -> Vec<(Cow<'_, str>, Cow<'_, str>)> {
match self {
ResultControl::Rows(rows) => {
vec![(Cow::Borrowed("rows"), Cow::Owned(rows.to_string()))]
}
ResultControl::Offset(offset) => {
vec![(Cow::Borrowed("offset"), Cow::Owned(offset.to_string()))]
}
ResultControl::RowsOffset { rows, offset } => vec![
(Cow::Borrowed("rows"), Cow::Owned(rows.to_string())),
(Cow::Borrowed("offset"), Cow::Owned(offset.to_string())),
],
ResultControl::Sample(sample) => {
vec![(Cow::Borrowed("sample"), Cow::Owned(sample.to_string()))]
}
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Component {
Works,
Funders,
Prefixes,
Members,
Types,
Journals,
Licenses,
}
impl Component {
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",
Component::Licenses => "licenses",
}
}
}
impl CrossrefRoute for Component {
fn route(&self) -> Result<String> {
Ok(format!("/{}", self.as_str()))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WorksComponent {
Funders,
Prefixes,
Members,
Types,
Journals,
}
impl WorksComponent {
pub fn component(&self) -> Component {
match self {
WorksComponent::Funders => Component::Funders,
WorksComponent::Prefixes => Component::Prefixes,
WorksComponent::Members => Component::Members,
WorksComponent::Types => Component::Types,
WorksComponent::Journals => Component::Journals,
}
}
}
impl CrossrefRoute for WorksComponent {
fn route(&self) -> Result<String> {
self.component().route()
}
}
#[derive(Debug, Clone)]
pub enum ResourceComponent {
Works(Works),
Funders(Funders),
Prefixes(Prefixes),
Members(Members),
Types(Types),
Journals(Journals),
Licenses(Licenses),
}
impl ResourceComponent {
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,
ResourceComponent::Licenses(_) => Component::Licenses,
}
}
}
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(),
ResourceComponent::Licenses(c) => c.route(),
}
}
}
impl CrossrefQuery for ResourceComponent {
fn resource_component(self) -> ResourceComponent {
self
}
}
pub trait Filter: ParamFragment {}
impl<T: Filter> CrossrefQueryParam for Vec<T> {
fn params(&self) -> Vec<(Cow<'_, str>, Cow<'_, str>)> {
vec![(
Cow::Borrowed("filter"),
Cow::Owned(
self.iter()
.map(ParamFragment::fragment)
.collect::<Vec<_>>()
.join(","),
),
)]
}
}
pub trait ParamFragment {
fn key(&self) -> Cow<'_, str>;
fn value(&self) -> Option<Cow<'_, str>>;
fn fragment(&self) -> Cow<'_, str> {
if let Some(val) = self.value() {
Cow::Owned(format!("{}:{}", self.key(), val))
} else {
self.key()
}
}
}
pub trait CrossrefQueryParam {
fn params(&self) -> Vec<(Cow<'_, str>, Cow<'_, str>)>;
}
impl<T: AsRef<str>> CrossrefQueryParam for (T, T) {
fn params(&self) -> Vec<(Cow<'_, str>, Cow<'_, str>)> {
vec![(
Cow::Borrowed(self.0.as_ref()),
Cow::Borrowed(self.1.as_ref()),
)]
}
}
pub trait CrossrefRoute {
fn route(&self) -> Result<String>;
}
pub trait CrossrefQuery: CrossrefRoute + Clone {
fn resource_component(self) -> ResourceComponent;
fn to_url(&self, base_path: &str) -> Result<String> {
Ok(format!("{}{}", base_path, self.route()?))
}
}
pub(crate) fn reject_unsendable_filters<T: Filter>(filters: &[T]) -> Result<()> {
for filter in filters {
match filter.value() {
Some(value) if value.contains(',') => {
return Err(crate::Error::UnsendableFilterValue {
filter: filter.key().into_owned(),
value: value.into_owned(),
});
}
_ => (),
}
}
Ok(())
}
pub(crate) fn format_query<T: AsRef<str>>(topic: T) -> String {
topic
.as_ref()
.split_whitespace()
.collect::<Vec<_>>()
.join(" ")
}
pub(crate) fn format_queries<T: AsRef<str>>(topics: &[T]) -> String {
topics
.iter()
.flat_map(|topic| topic.as_ref().split_whitespace())
.collect::<Vec<_>>()
.join(" ")
}
#[cfg(test)]
fn assert_matches_api(kind: &str, ours: &[&str], api: &[&str]) {
use std::collections::BTreeSet;
let mine: BTreeSet<&str> = ours.iter().copied().collect();
assert_eq!(ours.len(), mine.len(), "duplicate {kind} in this crate");
let theirs: BTreeSet<&str> = api.iter().copied().collect();
assert_eq!(api.len(), theirs.len(), "duplicate {kind} in the api list");
let rejected: Vec<_> = mine.difference(&theirs).collect();
assert!(
rejected.is_empty(),
"{kind} this crate sends that crossref answers with a 400: {rejected:?}"
);
let unreachable: Vec<_> = theirs.difference(&mine).collect();
assert!(
unreachable.is_empty(),
"{kind} crossref accepts that this crate cannot express: {unreachable:?}"
);
}
#[cfg(test)]
mod tests {
use super::*;
use crate::query::funders::FundersFilter;
use crate::query::members::MembersFilter;
use crate::query::works::{FieldQuery, WorkElement, WorksFilter};
const API_SORT_FIELDS: &[&str] = &[
"score",
"created",
"issued",
"indexed",
"is-referenced-by-count",
"relevance",
"published",
"published-print",
"published-online",
"updated",
"references-count",
"deposited",
];
#[test]
fn every_sort_key_is_accepted_by_the_api() {
let names: Vec<&str> = Sort::ALL.iter().map(Sort::as_str).collect();
assert_matches_api("/works sort fields", &names, API_SORT_FIELDS);
}
#[test]
fn sort_round_trips_through_from_str() {
for sort in Sort::ALL {
assert_eq!(Ok(*sort), Sort::from_str(sort.as_str()));
}
}
#[test]
fn order_round_trips_through_from_str() {
for order in [Order::Asc, Order::Desc] {
assert_eq!(Ok(order), Order::from_str(order.as_str()));
}
}
const API_WORKS_FILTERS: &[&str] = &[
"alternative-id",
"archive",
"article-number",
"assertion",
"assertion-group",
"award.funder",
"award.number",
"category-name",
"clinical-trial-number",
"container-title",
"content-domain",
"directory",
"doi",
"from-accepted-date",
"from-approved-date",
"from-awarded-date",
"from-created-date",
"from-deposit-date",
"from-event-end-date",
"from-event-start-date",
"from-index-date",
"from-issued-date",
"from-online-pub-date",
"from-posted-date",
"from-print-pub-date",
"from-pub-date",
"from-update-date",
"full-text.application",
"full-text.type",
"full-text.version",
"funder",
"funder-doi-asserted-by",
"group-title",
"gte-award-amount",
"has-abstract",
"has-affiliation",
"has-affiliation-ror-id",
"has-alias",
"has-archive",
"has-assertion",
"has-authenticated-orcid",
"has-award",
"has-clinical-trial-number",
"has-content-domain",
"has-domain-restriction",
"has-event",
"has-full-text",
"has-funder",
"has-funder-doi",
"has-funder-ror-id",
"has-license",
"has-orcid",
"has-prime-doi",
"has-references",
"has-relation",
"has-ror-id",
"has-update",
"has-update-policy",
"is-update",
"isbn",
"issn",
"license.delay",
"license.url",
"license.version",
"lte-award-amount",
"member",
"orcid",
"prefix",
"relation.object",
"relation.object-type",
"relation.type",
"ror-id",
"type",
"type-name",
"until-accepted-date",
"until-approved-date",
"until-awarded-date",
"until-created-date",
"until-deposit-date",
"until-event-end-date",
"until-event-start-date",
"until-index-date",
"until-issued-date",
"until-online-pub-date",
"until-posted-date",
"until-print-pub-date",
"until-pub-date",
"until-update-date",
"update-type",
"updates",
];
const API_FIELD_QUERIES: &[&str] = &[
"affiliation",
"author",
"bibliographic",
"chair",
"container-title",
"contributor",
"degree",
"description",
"editor",
"event-acronym",
"event-location",
"event-name",
"event-sponsor",
"event-theme",
"funder-name",
"publisher-location",
"publisher-name",
"standards-body-acronym",
"standards-body-name",
"title",
"translator",
];
const API_WORK_ELEMENTS: &[&str] = &[
"DOI",
"ISBN",
"ISSN",
"URL",
"abstract",
"accepted",
"alternative-id",
"approved",
"archive",
"article-number",
"assertion",
"author",
"chair",
"clinical-trial-number",
"container-title",
"content-created",
"content-domain",
"contributor",
"created",
"degree",
"deposited",
"editor",
"event",
"funder",
"group-title",
"indexed",
"is-referenced-by-count",
"issn-type",
"issue",
"issued",
"license",
"link",
"member",
"original-title",
"page",
"posted",
"prefix",
"published",
"published-online",
"published-print",
"publisher",
"publisher-location",
"reference",
"references-count",
"relation",
"resource",
"score",
"short-container-title",
"short-title",
"standards-body",
"subject",
"subtitle",
"title",
"translator",
"type",
"update-policy",
"update-to",
"updated-by",
"volume",
];
const API_FUNDERS_FILTERS: &[&str] = &["location"];
const API_MEMBERS_FILTERS: &[&str] = &["prefix", "backfile-doi-count", "current-doi-count"];
#[test]
fn every_works_filter_is_accepted_by_the_api() {
assert_matches_api("/works filters", WorksFilter::ALL_NAMES, API_WORKS_FILTERS);
}
#[test]
fn every_field_query_is_accepted_by_the_api() {
assert_matches_api(
"/works field queries",
FieldQuery::ALL_FIELDS,
API_FIELD_QUERIES,
);
}
#[test]
fn every_selectable_element_is_accepted_by_the_api() {
let names: Vec<&str> = WorkElement::ALL.iter().map(WorkElement::name).collect();
assert_matches_api("/works select elements", &names, API_WORK_ELEMENTS);
}
#[test]
fn every_funders_filter_is_accepted_by_the_api() {
assert_matches_api(
"/funders filters",
FundersFilter::ALL_NAMES,
API_FUNDERS_FILTERS,
);
}
#[test]
fn every_members_filter_is_accepted_by_the_api() {
assert_matches_api(
"/members filters",
MembersFilter::ALL_NAMES,
API_MEMBERS_FILTERS,
);
}
#[test]
fn a_marker_filter_renders_as_true_and_a_value_filter_as_its_value() {
let filters = vec![
WorksFilter::HasOrcid,
WorksFilter::AlternativeId("abc".to_string()),
WorksFilter::LicenseDelay(30),
WorksFilter::GteAwardAmount(1_000),
];
assert_eq!(
vec![(
Cow::Borrowed("filter"),
Cow::Owned(
"has-orcid:true,alternative-id:abc,license.delay:30,gte-award-amount:1000"
.to_string()
)
)],
filters.params()
);
}
}