diff options
| author | Zeyla Hellyer <[email protected]> | 2017-05-22 17:02:00 -0700 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2017-05-22 17:02:00 -0700 |
| commit | 9969be60cf320797c37b317da24d9a08fd5eafa5 (patch) | |
| tree | f27bf7a57af95bbc11990b1edcea9cca99276964 /src/builder/edit_role.rs | |
| parent | Reasonably derive Debug on items (diff) | |
| download | serenity-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/builder/edit_role.rs')
| -rw-r--r-- | src/builder/edit_role.rs | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/src/builder/edit_role.rs b/src/builder/edit_role.rs new file mode 100644 index 0000000..64cc42b --- /dev/null +++ b/src/builder/edit_role.rs @@ -0,0 +1,135 @@ +use std::default::Default; +use ::internal::prelude::*; +use ::model::{Permissions, Role, permissions}; + +/// A builer to create or edit a [`Role`] for use via a number of model and +/// context methods. +/// +/// These are: +/// +/// - [`Context::create_role`] +/// - [`Context::edit_role`] +/// - [`Guild::create_role`] +/// - [`Role::edit`] +/// +/// Defaults are provided for each parameter on role creation. +/// +/// # Examples +/// +/// Create a hoisted, mentionable role named "a test role": +/// +/// ```rust,ignore +/// // assuming a `channel_id` and `guild_id` has been bound +/// let role = channel_id.create_role(guild_id, |r| r +/// .hoist(true) +/// .mentionable(true) +/// .name("a test role")); +/// ``` +/// +/// [`Context::create_role`]: ../client/struct.Context.html#method.create_role +/// [`Context::edit_role`]: ../client/struct.Context.html#method.edit_role +/// [`Guild::create_role`]: ../model/struct.Guild.html#method.create_role +/// [`Role`]: ../model/struct.Role.html +/// [`Role::edit`]: ../model/struct.Role.html#method.edit +#[derive(Clone, Debug)] +pub struct EditRole(pub JsonMap); + +impl EditRole { + /// Creates a new builder with the values of the given [`Role`]. + /// + /// [`Role`]: ../model/struct.Role.html + pub fn new(role: &Role) -> Self { + let mut map = Map::new(); + + #[cfg(feature="utils")] + { + map.insert("color".to_owned(), Value::Number(Number::from(role.colour.0))); + } + + #[cfg(not(feature="utils"))] + { + map.insert("color".to_owned(), Value::Number(Number::from(role.colour))); + } + + map.insert("hoist".to_owned(), Value::Bool(role.hoist)); + map.insert("managed".to_owned(), Value::Bool(role.managed)); + map.insert("mentionable".to_owned(), Value::Bool(role.mentionable)); + map.insert("name".to_owned(), Value::String(role.name.clone())); + map.insert("permissions".to_owned(), Value::Number(Number::from(role.permissions.bits()))); + map.insert("position".to_owned(), Value::Number(Number::from(role.position))); + + EditRole(map) + } + + /// Sets the colour of the role. + pub fn colour(mut self, colour: u64) -> Self { + self.0.insert("color".to_owned(), Value::Number(Number::from(colour))); + + self + } + + /// Whether or not to hoist the role above lower-positioned role in the user + /// list. + pub fn hoist(mut self, hoist: bool) -> Self { + self.0.insert("hoist".to_owned(), Value::Bool(hoist)); + + self + } + + /// Whether or not to make the role mentionable, notifying its users. + pub fn mentionable(mut self, mentionable: bool) -> Self { + self.0.insert("mentionable".to_owned(), Value::Bool(mentionable)); + + self + } + + /// The name of the role to set. + pub fn name(mut self, name: &str) -> Self { + self.0.insert("name".to_owned(), Value::String(name.to_owned())); + + self + } + + /// The set of permissions to assign the role. + pub fn permissions(mut self, permissions: Permissions) -> Self { + self.0.insert("permissions".to_owned(), Value::Number(Number::from(permissions.bits()))); + + self + } + + /// The position to assign the role in the role list. This correlates to the + /// role's position in the user list. + pub fn position(mut self, position: u8) -> Self { + self.0.insert("position".to_owned(), Value::Number(Number::from(position))); + + self + } +} + +impl Default for EditRole { + /// Creates a builder with default parameters. + /// + /// The defaults are: + /// + /// - **color**: 10070709 + /// - **hoist**: false + /// - **mentionable**: false + /// - **name**: new role + /// - **permissions**: the [general permissions set] + /// - **position**: 1 + /// + /// [general permissions set]: ../model/permissions/constant.PRESET_GENERAL.html + fn default() -> EditRole { + let mut map = Map::new(); + let permissions = Number::from(permissions::PRESET_GENERAL.bits()); + + map.insert("color".to_owned(), Value::Number(Number::from(10070709))); + map.insert("hoist".to_owned(), Value::Bool(false)); + map.insert("mentionable".to_owned(), Value::Bool(false)); + map.insert("name".to_owned(), Value::String("new role".to_owned())); + map.insert("permissions".to_owned(), Value::Number(permissions)); + map.insert("position".to_owned(), Value::Number(Number::from(1))); + + EditRole(map) + } +} |