aboutsummaryrefslogtreecommitdiff
path: root/src/internal/macros.rs
diff options
context:
space:
mode:
authorZeyla Hellyer <[email protected]>2017-05-22 17:02:00 -0700
committerZeyla Hellyer <[email protected]>2017-05-22 17:02:00 -0700
commit9969be60cf320797c37b317da24d9a08fd5eafa5 (patch)
treef27bf7a57af95bbc11990b1edcea9cca99276964 /src/internal/macros.rs
parentReasonably derive Debug on items (diff)
downloadserenity-9969be60cf320797c37b317da24d9a08fd5eafa5.tar.xz
serenity-9969be60cf320797c37b317da24d9a08fd5eafa5.zip
Restructure modules
Modules are now separated into a fashion where the library can be used for most use cases, without needing to compile the rest. The core of serenity, with no features enabled, contains only the struct (model) definitions, constants, and prelude. Models do not have most functions compiled in, as that is separated into the `model` feature. The `client` module has been split into 3 modules: `client`, `gateway`, and `http`. `http` contains functions to interact with the REST API. `gateway` contains the Shard to interact with the gateway, requiring `http` for retrieving the gateway URL. `client` requires both of the other features and acts as an abstracted interface over both the gateway and REST APIs, handling the event loop. The `builder` module has been separated from `utils`, and can now be optionally compiled in. It and the `http` feature are required by the `model` feature due to a large number of methods requiring access to them. `utils` now contains a number of utilities, such as the Colour struct, the `MessageBuilder`, and mention parsing functions. Each of the original `ext` modules are still featured, with `cache` not requiring any feature to be enabled, `framework` requiring the `client`, `model`, and `utils`, and `voice` requiring `gateway`. In total the features and their requirements are: - `builder`: none - `cache`: none - `client`: `gateway`, `http` - `framework`: `client`, `model`, `utils` - `gateway`: `http` - `http`: none - `model`: `builder`, `http` - `utils`: none - `voice`: `gateway` The default features are `builder`, `cache`, `client`, `framework`, `gateway`, `model`, `http`, and `utils`. To help with forwards compatibility, modules have been re-exported from their original locations.
Diffstat (limited to 'src/internal/macros.rs')
-rw-r--r--src/internal/macros.rs170
1 files changed, 170 insertions, 0 deletions
diff --git a/src/internal/macros.rs b/src/internal/macros.rs
new file mode 100644
index 0000000..0a54d8f
--- /dev/null
+++ b/src/internal/macros.rs
@@ -0,0 +1,170 @@
+//! A set of macros for easily working with internals.
+
+macro_rules! request {
+ ($route:expr, $method:ident($body:expr), $url:expr, $($rest:tt)*) => {{
+ let client = HyperClient::new();
+ request($route, || client
+ .$method(&format!(api!($url), $($rest)*))
+ .body(&$body))?
+ }};
+ ($route:expr, $method:ident($body:expr), $url:expr) => {{
+ let client = HyperClient::new();
+ request($route, || client
+ .$method(api!($url))
+ .body(&$body))?
+ }};
+ ($route:expr, $method:ident, $url:expr, $($rest:tt)*) => {{
+ let client = HyperClient::new();
+ request($route, || client
+ .$method(&format!(api!($url), $($rest)*)))?
+ }};
+ ($route:expr, $method:ident, $url:expr) => {{
+ let client = HyperClient::new();
+ request($route, || client
+ .$method(api!($url)))?
+ }};
+}
+
+macro_rules! cdn {
+ ($e:expr) => {
+ concat!("https://cdn.discordapp.com", $e)
+ };
+ ($e:expr, $($rest:tt)*) => {
+ format!(cdn!($e), $($rest)*)
+ };
+}
+
+macro_rules! base {
+ ($e:expr) => {
+ concat!("https://discordapp.com", $e)
+ };
+ ($e:expr, $($rest:tt)*) => {
+ format!(base!($e), $($rest)*)
+ };
+}
+
+macro_rules! api {
+ ($e:expr) => {
+ concat!("https://discordapp.com/api/v6", $e)
+ };
+ ($e:expr, $($rest:tt)*) => {
+ format!(api!($e), $($rest)*)
+ };
+}
+
+macro_rules! status {
+ ($e:expr) => {
+ concat!("https://status.discordapp.com/api/v2", $e)
+ }
+}
+
+// Enable/disable check for cache
+#[cfg(feature="cache")]
+macro_rules! feature_cache {
+ ($enabled:block else $disabled:block) => {
+ {
+ $enabled
+ }
+ }
+}
+
+#[cfg(not(feature="cache"))]
+macro_rules! feature_cache {
+ ($enabled:block else $disabled:block) => {
+ {
+ $disabled
+ }
+ }
+}
+
+// Enable/disable check for framework
+#[cfg(feature="framework")]
+macro_rules! feature_framework {
+ ($enabled:block else $disabled:block) => {
+ {
+ $enabled
+ }
+ }
+}
+
+#[cfg(not(feature="framework"))]
+macro_rules! feature_framework {
+ ($enabled:block else $disabled:block) => {
+ {
+ $disabled
+ }
+ }
+}
+
+// Enable/disable check for voice
+#[cfg(feature="voice")]
+macro_rules! feature_voice {
+ ($enabled:block else $disabled:block) => {
+ {
+ $enabled
+ }
+ }
+}
+
+#[cfg(not(feature="voice"))]
+macro_rules! feature_voice {
+ ($enabled:block else $disabled:block) => {
+ {
+ $disabled
+ }
+ }
+}
+
+macro_rules! enum_number {
+ (#[$attr_:meta] $name:ident { $(#[$attr:meta] $variant:ident = $value:expr, )* }) => {
+ #[$attr_]
+ #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
+ pub enum $name {
+ $(
+ #[$attr]
+ $variant = $value,
+ )*
+ }
+
+ impl ::serde::Serialize for $name {
+ fn serialize<S>(&self, serializer: S) -> ::std::result::Result<S::Ok, S::Error>
+ where S: ::serde::Serializer
+ {
+ // Serialize the enum as a u64.
+ serializer.serialize_u64(*self as u64)
+ }
+ }
+
+ impl<'de> ::serde::Deserialize<'de> for $name {
+ fn deserialize<D>(deserializer: D) -> ::std::result::Result<Self, D::Error>
+ where D: ::serde::Deserializer<'de>
+ {
+ struct Visitor;
+
+ impl<'de> ::serde::de::Visitor<'de> for Visitor {
+ type Value = $name;
+
+ fn expecting(&self, formatter: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
+ formatter.write_str("positive integer")
+ }
+
+ fn visit_u64<E>(self, value: u64) -> ::std::result::Result<$name, E>
+ where E: ::serde::de::Error
+ {
+ // Rust does not come with a simple way of converting a
+ // number to an enum, so use a big `match`.
+ match value {
+ $( $value => Ok($name::$variant), )*
+ _ => Err(E::custom(
+ format!("unknown {} value: {}",
+ stringify!($name), value))),
+ }
+ }
+ }
+
+ // Deserialize the enum from a u64.
+ deserializer.deserialize_u64(Visitor)
+ }
+ }
+ }
+}