diff options
| author | Austin Hellyer <[email protected]> | 2016-11-13 08:22:24 -0800 |
|---|---|---|
| committer | Austin Hellyer <[email protected]> | 2016-11-13 08:22:24 -0800 |
| commit | f633d1c9603079f584f4f715b308b33c0750ad3a (patch) | |
| tree | f77bc0b630731676be880b11c24b90cffef8c537 /src/utils/builder/edit_role.rs | |
| parent | Don't overflow on message length check (diff) | |
| download | serenity-f633d1c9603079f584f4f715b308b33c0750ad3a.tar.xz serenity-f633d1c9603079f584f4f715b308b33c0750ad3a.zip | |
Move the builders to the utils
The builders aren't a large enough portion of the library to deserve
their own root-level module, so move them to the `utils` module.
Additionally, split them into separate files, as the library will be
receiving more builders and the single-file pattern was getting rather
large.
Diffstat (limited to 'src/utils/builder/edit_role.rs')
| -rw-r--r-- | src/utils/builder/edit_role.rs | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/src/utils/builder/edit_role.rs b/src/utils/builder/edit_role.rs new file mode 100644 index 0000000..f4fa7ef --- /dev/null +++ b/src/utils/builder/edit_role.rs @@ -0,0 +1,104 @@ +use serde_json::builder::ObjectBuilder; +use std::default::Default; +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`] +/// - [`LiveGuild::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 you are in a `context` and a `guild_id` has been bound +/// let role = context.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 +/// [`LiveGuild::create_role`]: ../model/struct.LiveGuild.html#method.create_role +/// [`Role`]: ../model/struct.Role.html +/// [`Role::edit`]: ../model/struct.Role.html#method.edit +pub struct EditRole(pub ObjectBuilder); + +impl EditRole { + /// Creates a new builder with the values of the given [`Role`]. + pub fn new(role: &Role) -> Self { + EditRole(ObjectBuilder::new() + .insert("color", role.colour) + .insert("hoist", role.hoist) + .insert("managed", role.managed) + .insert("mentionable", role.mentionable) + .insert("name", &role.name) + .insert("permissions", role.permissions.bits()) + .insert("position", role.position)) + } + + /// Sets the colour of the role. + pub fn colour(self, colour: u64) -> Self { + EditRole(self.0.insert("color", colour)) + } + + /// Whether or not to hoist the role above lower-positioned role in the user + /// list. + pub fn hoist(self, hoist: bool) -> Self { + EditRole(self.0.insert("hoist", hoist)) + } + + /// Whether or not to make the role mentionable, notifying its users. + pub fn mentionable(self, mentionable: bool) -> Self { + EditRole(self.0.insert("mentionable", mentionable)) + } + + /// The name of the role to set. + pub fn name(self, name: &str) -> Self { + EditRole(self.0.insert("name", name)) + } + + /// The set of permissions to assign the role. + pub fn permissions(self, permissions: Permissions) -> Self { + EditRole(self.0.insert("permissions", permissions.bits())) + } + + /// The position to assign the role in the role list. This correlates to the + /// role's position in the user list. + pub fn position(self, position: u8) -> Self { + EditRole(self.0.insert("position", position)) + } +} + +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/fn.general.html + fn default() -> EditRole { + EditRole(ObjectBuilder::new() + .insert("color", 10070709) + .insert("hoist", false) + .insert("mentionable", false) + .insert("name", String::from("new role")) + .insert("permissions", permissions::general().bits()) + .insert("position", 1)) + } +} |