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
/// This macro allows to crete enums with string values. The syntax for this is like specifying a
/// enum with numerics values, just with strings instead of numbers.
/// Every created enum has a `Custom(String)`
/// field which can be used to represent custom values of the enums purpose (in case the enum
/// holds some static variables, like [`crunchyroll::categories::Category`], and Crunchyroll
/// decides to add a additional variable) which reduces the chance of breaking something.
///
/// The generated enum implements [`std::fmt::Display`] (for a representation of the values),
/// [`Default`] (which is `<name>::Custom("")`), [`From<String>`] (checks if the given string
/// matches a value representation; if not `<name>::Custom("")`) and [`serde::Serialize`] as well
/// as [`serde::Deserialize`] for http actions.
macro_rules! enum_values {
($(#[$attribute:meta])* $v:vis enum $name:ident { $($field:ident = $value:expr)* }) => {
$(
#[$attribute]
)*
#[derive(Clone, Debug, Eq, PartialEq)]
$v enum $name {
$(
$field
),*,
Custom(String)
}
impl std::fmt::Display for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let value = match self {
$(
$name::$field => $value
),*,
$name::Custom(raw) => raw
};
write!(f, "{}", value)
}
}
impl Default for $name {
fn default() -> Self {
$name::Custom("".to_string())
}
}
impl From<String> for $name {
fn from(value: String) -> Self {
for (v, f) in [$(($value, $name::$field)),*] {
if value.eq_ignore_ascii_case(v) {
return f;
}
}
$name::Custom(value)
}
}
impl<'de> serde::Deserialize<'de> for $name {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where D: serde::Deserializer<'de>
{
Ok(Self::from(String::deserialize(deserializer)?))
}
}
impl serde::Serialize for $name {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> where S: serde::ser::Serializer {
serializer.serialize_str(self.to_string().as_str())
}
}
};
}
/// This macro creates a struct which is internal primarily used to specify request options for
/// specific endpoints.
///
/// # Examples
///
/// ```
/// use crunchyroll_rs::options;
///
/// options! {
/// PaginationOptions;
/// limit(u32, "n") = Some(20)
/// start(u32, "start") = None
/// }
/// ```
///
/// Produces the following struct implementation.
///
/// ```
/// pub struct PaginationOptions {
/// limit: Option<u32>,
/// start: Option<u32>
/// }
///
/// impl Default for PaginationOptions {
/// fn default() -> Self {
/// Self {
/// limit: Some(20),
/// start: None
/// }
/// }
/// }
///
/// impl PaginationOptions {
/// pub fn limit(mut self, value: u32) -> PaginationOptions {
/// self.limit = Some(value);
/// self
/// }
///
/// pub fn start(mut self, value: u32) -> PaginationOptions {
/// self.start = Some(value);
/// self
/// }
///
/// pub(crate) fn into_query(self) -> Vec<(String, String)> {
/// let encoded = serde_urlencoded::to_string([
/// ("n", if let Some(field) = self.limit { Some(serde_json::to_value(field).unwrap()) } else { None }),
/// ("start", if let Some(field) = self.start { Some(serde_json::to_value(field).unwrap()) } else { None })
/// ]).unwrap();
/// }
/// }
/// ```
macro_rules! options {
($name:ident; $($(#[$attribute:meta])* $field:ident($t:ty, $query_name:literal) = $default:expr),*) => {
#[derive(Clone, Debug)]
pub struct $name {
$(
$(
#[$attribute]
)*
$field: Option<$t>
),*
}
impl Default for $name {
fn default() -> Self {
Self {
$(
$field: $default
),*
}
}
}
impl $name {
$(
pub fn $field(mut self, value: $t) -> $name {
self.$field = Some(value);
self
}
)*
#[allow(dead_code)]
pub(crate) fn into_query(self) -> Vec<(String, String)> {
crate::internal::serde::query_to_urlencoded([
$(
($query_name, if let Some(field) = self.$field {
Some(serde_json::to_value(field).unwrap())
} else { None })
),*
].to_vec()).unwrap()
}
}
}
}
pub(crate) use enum_values;
pub(crate) use options;