scryfall/
util.rs

1//! Module containing utility functions and structs.
2use once_cell::sync::Lazy;
3use serde::{Deserialize, Deserializer};
4use url::Url;
5
6pub(crate) mod array_stream_reader;
7
8/// The [scryfall](https://scryfall.com/docs/api) endpoint.
9pub static ROOT_URL: Lazy<Url> = Lazy::new(|| Url::parse("https://api.scryfall.com/").unwrap());
10/// The [cards](https://scryfall.com/docs/api/cards) endpoint.
11pub static CARDS_URL: Lazy<Url> = Lazy::new(|| ROOT_URL.join("cards/").unwrap());
12/// The [sets](https://scryfall.com/docs/api/sets) endpoint.
13pub static SETS_URL: Lazy<Url> = Lazy::new(|| ROOT_URL.join("sets/").unwrap());
14/// The [bulk-data](https://scryfall.com/docs/api/bulk-data) endpoint.
15pub static BULK_DATA_URL: Lazy<Url> = Lazy::new(|| ROOT_URL.join("bulk-data/").unwrap());
16/// The [catalog](https://scryfall.com/docs/api/catalogs) endpoint.
17pub static CATALOG_URL: Lazy<Url> = Lazy::new(|| ROOT_URL.join("catalog/").unwrap());
18
19/// The [rulings](https://scryfall.com/docs/api/rulings) path segment, which goes on the end of a
20/// card URL.
21pub const API_RULING: &str = "rulings/";
22
23/// Function for use with `#[serde(deserialize_with)]` and a field that's
24/// Option<T>. If deserialization fails, use `None` as the field's value and
25/// don't cause an error.
26pub fn deserialize_or_none<'de, D: Deserializer<'de>, T: Deserialize<'de>>(
27    deserializer: D,
28) -> Result<Option<T>, D::Error> {
29    T::deserialize(deserializer).map(Some).or(Ok(None))
30}