Skip to content

Commit

Permalink
Restructure modules
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Zeyla Hellyer committed May 23, 2017
1 parent 9dae9e6 commit 9969be6
Show file tree
Hide file tree
Showing 77 changed files with 1,132 additions and 889 deletions.
45 changes: 36 additions & 9 deletions Cargo.toml
Expand Up @@ -14,15 +14,11 @@ version = "0.2.0"
base64 = "~0.5"
bitflags = "~0.8"
flate2 = "~0.2"
hyper = "~0.9"
lazy_static = "~0.2"
log = "~0.3"
serde = "^1.0"
serde_derive = "^1.0"
serde_json = "^1.0"
time = "~0.1"
typemap = "~0.3"
websocket = "~0.17"

[dependencies.byteorder]
optional = true
Expand All @@ -32,9 +28,18 @@ version = "1.0"
default-features = false
version = "0.2"

[dependencies.hyper]
optional = true
version = "~0.9"

[dependencies.lazy_static]
optional = true
version = "~0.2"

[dependencies.multipart]
default-features = false
features = ["client", "hyper"]
optional = true
version = "0.8"

[dependencies.opus]
Expand All @@ -46,10 +51,32 @@ default-features = false
optional = true
version = "0.0.12"

[dependencies.typemap]
optional = true
version = "~0.3"

[dependencies.websocket]
optional = true
version = "~0.17"

[features]
default = ["cache", "framework"]
cache = []
debug = []
framework = []
default = [
"builder",
"cache",
"client",
"framework",
"gateway",
"model",
"http",
"utils"
]
builder = []
cache = ["lazy_static"]
client = ["gateway", "lazy_static", "http", "typemap"]
extras = []
voice = ["byteorder", "opus", "sodiumoxide"]
framework = ["client", "model", "utils"]
gateway = ["http", "websocket"]
http = ["hyper", "lazy_static", "multipart"]
model = ["builder", "http"]
utils = []
voice = ["byteorder", "gateway", "opus", "sodiumoxide"]
36 changes: 30 additions & 6 deletions src/utils/builder/create_embed.rs → src/builder/create_embed.rs
Expand Up @@ -10,7 +10,7 @@
//!
//! Documentation for embeds can be found [here].
//!
//! [`Context::send_message`]: ../../client/struct.Context.html#method.send_message
//! [`Context::send_message`]: ../client/struct.Context.html#method.send_message
//! [`CreateEmbed`]: struct.CreateEmbed.html
//! [`ExecuteWebhook::embeds`]: struct.ExecuteWebhook.html#method.embeds
//! [here]: https://discordapp.com/developers/docs/resources/channel#embed-object
Expand All @@ -20,6 +20,8 @@ use std::default::Default;
use time::Tm;
use ::internal::prelude::*;
use ::model::Embed;

#[cfg(feature="utils")]
use ::utils::Colour;

/// A builder to create a fake [`Embed`] object, for use with the
Expand All @@ -30,8 +32,8 @@ use ::utils::Colour;
/// Refer to the documentation for [`Context::send_message`] for a very in-depth
/// example on how to use this.
///
/// [`Context::send_message`]: ../../client/struct.Context.html#method.send_message
/// [`Embed`]: ../../model/struct.Embed.html
/// [`Context::send_message`]: ../client/struct.Context.html#method.send_message
/// [`Embed`]: ../model/struct.Embed.html
/// [`ExecuteWebhook::embeds`]: struct.ExecuteWebhook.html#method.embeds
#[derive(Clone, Debug)]
pub struct CreateEmbed(pub Map<String, Value>);
Expand All @@ -57,17 +59,39 @@ impl CreateEmbed {
/// This is an alias of [`colour`].
///
/// [`colour`]: #method.colour
#[cfg(feature="utils")]
#[inline]
pub fn color<C: Into<Colour>>(self, colour: C) -> Self {
self.colour(colour.into())
}

/// Set the colour of the left-hand side of the embed.
#[cfg(feature="utils")]
pub fn colour<C: Into<Colour>>(mut self, colour: C) -> Self {
self.0.insert("color".to_owned(), Value::Number(Number::from(colour.into().0 as u64)));

CreateEmbed(self.0)
}

/// Set the colour of the left-hand side of the embed.
///
/// This is an alias of [`colour`].
///
/// [`colour`]: #method.colour
#[cfg(not(feature="utils"))]
#[inline]
pub fn color(self, colour: u32) -> Self {
self.colour(colour)
}

/// Set the colour of the left-hand side of the embed.
#[cfg(not(feature="utils"))]
pub fn colour(mut self, colour: u32) -> Self {
self.0.insert("color".to_owned(), Value::Number(Number::from(colour)));

CreateEmbed(self.0)
}

/// Set the description of the embed.
///
/// **Note**: This can't be longer than 2048 characters.
Expand Down Expand Up @@ -269,7 +293,7 @@ impl From<Embed> for CreateEmbed {
///
/// Requires that you specify a [`name`].
///
/// [`Embed`]: ../../model/struct.Embed.html
/// [`Embed`]: ../model/struct.Embed.html
/// [`CreateEmbed::author`]: struct.CreateEmbed.html#method.author
/// [`name`]: #method.name
#[derive(Clone, Debug, Default)]
Expand Down Expand Up @@ -304,7 +328,7 @@ impl CreateEmbedAuthor {
/// This does not require any field be set. `inline` is set to `true` by
/// default.
///
/// [`Embed`]: ../../model/struct.Embed.html
/// [`Embed`]: ../model/struct.Embed.html
/// [`CreateEmbed::field`]: struct.CreateEmbed.html#method.field
#[derive(Clone, Debug)]
pub struct CreateEmbedField(pub Map<String, Value>);
Expand Down Expand Up @@ -348,7 +372,7 @@ impl Default for CreateEmbedField {
///
/// This does not require any field be set.
///
/// [`Embed`]: ../../model/struct.Embed.html
/// [`Embed`]: ../model/struct.Embed.html
/// [`CreateEmbed::footer`]: struct.CreateEmbed.html#method.footer
#[derive(Clone, Debug, Default)]
pub struct CreateEmbedFooter(pub Map<String, Value>);
Expand Down
Expand Up @@ -26,8 +26,8 @@ use ::internal::prelude::*;
/// });
/// ```
///
/// [`Context::create_invite`]: ../../client/struct.Context.html#method.create_invite
/// [`RichInvite`]: ../../model/struct.Invite.html
/// [`Context::create_invite`]: ../client/struct.Context.html#method.create_invite
/// [`RichInvite`]: ../model/struct.Invite.html
#[derive(Clone, Debug)]
pub struct CreateInvite(pub JsonMap);

Expand Down
@@ -1,7 +1,7 @@
use super::CreateEmbed;
use ::internal::prelude::*;

/// A builder to specify the contents of an [`rest::send_message`] request,
/// A builder to specify the contents of an [`http::send_message`] request,
/// primarily meant for use through [`Context::send_message`].
///
/// There are two situations where different field requirements are present:
Expand Down Expand Up @@ -31,11 +31,11 @@ use ::internal::prelude::*;
/// .description("With a description")));
/// ```
///
/// [`Context::say`]: ../../client/struct.Context.html#method.say
/// [`Context::send_message`]: ../../client/struct.Context.html#method.send_message
/// [`Context::say`]: ../client/struct.Context.html#method.say
/// [`Context::send_message`]: ../client/struct.Context.html#method.send_message
/// [`content`]: #method.content
/// [`embed`]: #method.embed
/// [`rest::send_message`]: ../../client/rest/fn.send_message.html
/// [`http::send_message`]: ../http/fn.send_message.html
#[derive(Clone, Debug)]
pub struct CreateMessage(pub Map<String, Value>);

Expand Down Expand Up @@ -75,7 +75,7 @@ impl Default for CreateMessage {
/// Creates a map for sending a [`Message`], setting [`tts`] to `false` by
/// default.
///
/// [`Message`]: ../../model/struct.Message.html
/// [`Message`]: ../model/struct.Message.html
/// [`tts`]: #method.tts
fn default() -> CreateMessage {
let mut map = Map::default();
Expand Down
Expand Up @@ -31,7 +31,7 @@ impl EditChannel {
///
/// This is for [voice] channels only.
///
/// [voice]: ../../model/enum.ChannelType.html#variant.Voice
/// [voice]: ../model/enum.ChannelType.html#variant.Voice
pub fn bitrate(mut self, bitrate: u64) -> Self {
self.0.insert("bitrate".to_owned(), Value::Number(Number::from(bitrate)));

Expand Down Expand Up @@ -60,7 +60,7 @@ impl EditChannel {
///
/// This is for [text] channels only.
///
/// [text]: ../../model/enum.ChannelType.html#variant.Text
/// [text]: ../model/enum.ChannelType.html#variant.Text
pub fn topic(mut self, topic: &str) -> Self {
self.0.insert("topic".to_owned(), Value::String(topic.to_owned()));

Expand All @@ -71,7 +71,7 @@ impl EditChannel {
///
/// This is for [voice] channels only.
///
/// [voice]: ../../model/enum.ChannelType.html#variant.Voice
/// [voice]: ../model/enum.ChannelType.html#variant.Voice
pub fn user_limit(mut self, user_limit: u64) -> Self {
self.0.insert("user_limit".to_owned(), Value::Number(Number::from(user_limit)));

Expand Down
21 changes: 10 additions & 11 deletions src/utils/builder/edit_guild.rs → src/builder/edit_guild.rs
Expand Up @@ -7,10 +7,10 @@ use ::model::{ChannelId, Region, UserId, VerificationLevel};
/// **Note**: Editing a guild requires that the current user have the
/// [Manage Guild] permission.
///
/// [`Context::edit_guild`]: ../../client/struct.Context.html
/// [`Guild`]: ../../model/struct.Guild.html
/// [`LiveGuild::edit`]: ../../model/struct.LiveGuild.html#method.edit
/// [Manage Guild]: ../../model/permissions/constant.MANAGE_GUILD.html
/// [`Context::edit_guild`]: ../client/struct.Context.html
/// [`Guild`]: ../model/struct.Guild.html
/// [`LiveGuild::edit`]: ../model/struct.LiveGuild.html#method.edit
/// [Manage Guild]: ../model/permissions/constant.MANAGE_GUILD.html
#[derive(Clone, Debug, Default)]
pub struct EditGuild(pub Map<String, Value>);

Expand Down Expand Up @@ -60,7 +60,7 @@ impl EditGuild {
/// let _ = guild.edit(|g| g.icon(base64_icon));
/// ```
///
/// [`utils::read_image`]: ../../utils/fn.read_image.html
/// [`utils::read_image`]: ../utils/fn.read_image.html
pub fn icon(mut self, icon: Option<&str>) -> Self {
self.0.insert("icon".to_owned(), icon.map_or_else(|| Value::Null, |x| Value::String(x.to_owned())));

Expand Down Expand Up @@ -101,7 +101,7 @@ impl EditGuild {
/// }
/// ```
///
/// [`Region::UsWest`]: ../../model/enum.Region.html#variant.UsWest
/// [`Region::UsWest`]: ../model/enum.Region.html#variant.UsWest
pub fn region(mut self, region: Region) -> Self {
self.0.insert("region".to_owned(), Value::String(region.name().to_owned()));

Expand All @@ -113,8 +113,8 @@ impl EditGuild {
/// Requires that the guild have the [`InviteSplash`] feature enabled.
/// You can check this through a guild's [`features`] list.
///
/// [`InviteSplash`]: ../../model/enum.Feature.html#variant.InviteSplash
/// [`features`]: ../../model/struct.LiveGuild.html#structfield.features
/// [`InviteSplash`]: ../model/enum.Feature.html#variant.InviteSplash
/// [`features`]: ../model/struct.LiveGuild.html#structfield.features
pub fn splash(mut self, splash: Option<&str>) -> Self {
let splash = splash.map_or(Value::Null, |x| Value::String(x.to_owned()));

Expand All @@ -129,7 +129,6 @@ impl EditGuild {
/// Refer to the documentation for [`VerificationLevel`] for more
/// information on each variant.
///
/// [`VerificationLevel`]: ../../model/enum.VerificationLevel.html
///
/// # Examples
///
Expand All @@ -152,8 +151,8 @@ impl EditGuild {
/// }
/// ```
///
/// [`VerificationLevel`]: ../../model/enum.VerificationLevel.html
/// [`VerificationLevel::High`]: ../../model/enum.VerificationLevel.html#variant.High
/// [`VerificationLevel`]: ../model/enum.VerificationLevel.html
/// [`VerificationLevel::High`]: ../model/enum.VerificationLevel.html#variant.High
pub fn verification_level<V>(mut self, verification_level: V) -> Self
where V: Into<VerificationLevel> {
let num = Value::Number(Number::from(verification_level.into().num()));
Expand Down
16 changes: 8 additions & 8 deletions src/utils/builder/edit_member.rs → src/builder/edit_member.rs
Expand Up @@ -4,9 +4,9 @@ use ::internal::prelude::*;
/// A builder which edits the properties of a [`Member`], to be used in
/// conjunction with [`Context::edit_member`] and [`Member::edit`].
///
/// [`Context::edit_member`]: ../../client/struct.Context.html#method.edit_member
/// [`Member`]: ../../model/struct.Member.html
/// [`Member::edit`]: ../../model/struct.Member.html#method.edit
/// [`Context::edit_member`]: ../client/struct.Context.html#method.edit_member
/// [`Member`]: ../model/struct.Member.html
/// [`Member::edit`]: ../model/struct.Member.html#method.edit
#[derive(Clone, Debug, Default)]
pub struct EditMember(pub JsonMap);

Expand All @@ -15,7 +15,7 @@ impl EditMember {
///
/// Requires the [Deafen Members] permission.
///
/// [Deafen Members]: ../../model/permissions/constant.DEAFEN_MEMBERS.html
/// [Deafen Members]: ../model/permissions/constant.DEAFEN_MEMBERS.html
pub fn deafen(mut self, deafen: bool) -> Self {
self.0.insert("deaf".to_owned(), Value::Bool(deafen));

Expand All @@ -26,7 +26,7 @@ impl EditMember {
///
/// Requires the [Mute Members] permission.
///
/// [Mute Members]: ../../model/permissions/constant.MUTE_MEMBERS.html
/// [Mute Members]: ../model/permissions/constant.MUTE_MEMBERS.html
pub fn mute(mut self, mute: bool) -> Self {
self.0.insert("mute".to_owned(), Value::Bool(mute));

Expand All @@ -38,7 +38,7 @@ impl EditMember {
///
/// Requires the [Manage Nicknames] permission.
///
/// [Manage Nicknames]: ../../model/permissions/constant.MANAGE_NICKNAMES.html
/// [Manage Nicknames]: ../model/permissions/constant.MANAGE_NICKNAMES.html
pub fn nickname(mut self, nickname: &str) -> Self {
self.0.insert("nick".to_owned(), Value::String(nickname.to_owned()));

Expand All @@ -49,7 +49,7 @@ impl EditMember {
///
/// Requires the [Manage Roles] permission to modify.
///
/// [Manage Roles]: ../../model/permissions/constant.MANAGE_ROLES.html
/// [Manage Roles]: ../model/permissions/constant.MANAGE_ROLES.html
pub fn roles(mut self, roles: &[RoleId]) -> Self {
let role_ids = roles.iter().map(|x| Value::Number(Number::from(x.0))).collect();

Expand All @@ -62,7 +62,7 @@ impl EditMember {
///
/// Requires the [Move Members] permission.
///
/// [Move Members]: ../../model/permissions/constant.MOVE_MEMBERS.html
/// [Move Members]: ../model/permissions/constant.MOVE_MEMBERS.html
pub fn voice_channel<C: Into<ChannelId>>(mut self, channel_id: C) -> Self {
self.0.insert("channel_id".to_owned(), Value::Number(Number::from(channel_id.into().0)));

Expand Down
Expand Up @@ -3,7 +3,7 @@ use ::internal::prelude::*;
/// A builder to edit the current user's settings, to be used in conjunction
/// with [`Context::edit_profile`].
///
/// [`Context::edit_profile`]: ../../client/struct.Context.html#method.edit_profile
/// [`Context::edit_profile`]: ../client/struct.Context.html#method.edit_profile
#[derive(Clone, Debug, Default)]
pub struct EditProfile(pub JsonMap);

Expand Down

0 comments on commit 9969be6

Please sign in to comment.