Skip to content

Commit

Permalink
Convert Colour to be a tuple struct
Browse files Browse the repository at this point in the history
The struct only has one field (`value`) anyway.
  • Loading branch information
Austin Hellyer committed Jan 13, 2017
1 parent 60f56a4 commit a8acd61
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/model/guild.rs
Expand Up @@ -1069,7 +1069,7 @@ impl Member {
.collect::<Vec<&Role>>();
roles.sort_by(|a, b| b.cmp(a));

roles.iter().find(|r| r.colour.value != default.value).map(|r| r.colour)
roles.iter().find(|r| r.colour.0 != default.0).map(|r| r.colour)
}

/// Calculates the member's display name.
Expand Down
2 changes: 1 addition & 1 deletion src/utils/builder/create_embed.rs
Expand Up @@ -63,7 +63,7 @@ impl CreateEmbed {

/// Set the colour of the left-hand side of the embed.
pub fn colour<C: Into<Colour>>(mut self, colour: C) -> Self {
self.0.insert("color".to_owned(), Value::U64(colour.into().value as u64));
self.0.insert("color".to_owned(), Value::U64(colour.into().0 as u64));

CreateEmbed(self.0)
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/builder/edit_role.rs
Expand Up @@ -37,7 +37,7 @@ 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.value)
.insert("color", role.colour.0)
.insert("hoist", role.hoist)
.insert("managed", role.managed)
.insert("mentionable", role.mentionable)
Expand Down
37 changes: 15 additions & 22 deletions src/utils/colour.rs
Expand Up @@ -65,11 +65,7 @@ macro_rules! colour {
/// [`dark_teal`]: #method.dark_teal
/// [`get_g`]: #method.get_g
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct Colour {
/// The raw inner 32-bit unsigned integer value of this Colour. This is
/// worked with to generate values such as the red component value.
pub value: u32,
}
pub struct Colour(pub u32);

impl Colour {
/// Generates a new Colour with the given integer value set.
Expand All @@ -88,10 +84,9 @@ impl Colour {
/// ```
///
/// [`get_tuple`]: #method.get_tuple
#[inline]
pub fn new(value: u32) -> Colour {
Colour {
value: value,
}
Colour(value)
}

/// Generates a new Colour from an RGB value, creating an inner u32
Expand All @@ -104,8 +99,8 @@ impl Colour {
/// ```rust
/// use serenity::utils::Colour;
///
/// assert!(Colour::from_rgb(255, 0, 0).value == 0xFF0000);
/// assert!(Colour::from_rgb(217, 23, 211).value == 0xD917D3);
/// assert!(Colour::from_rgb(255, 0, 0).0 == 0xFF0000);
/// assert!(Colour::from_rgb(217, 23, 211).0 == 0xD917D3);
/// ```
///
/// And you can then retrieve those same RGB values via its methods:
Expand All @@ -125,14 +120,14 @@ impl Colour {
uint = (uint << 8) | (g as u32);
uint = (uint << 8) | (b as u32);

Colour::new(uint)
Colour(uint)
}

#[doc(hidden)]
pub fn decode(value: Value) -> Result<Colour> {
match value {
Value::U64(v) => Ok(Colour::new(v as u32)),
Value::I64(v) => Ok(Colour::new(v as u32)),
Value::U64(v) => Ok(Colour(v as u32)),
Value::I64(v) => Ok(Colour(v as u32)),
other => Err(Error::Decode("Expected valid colour", other)),
}
}
Expand All @@ -147,7 +142,7 @@ impl Colour {
/// assert_eq!(Colour::new(6573123).get_r(), 100);
/// ```
pub fn get_r(&self) -> u8 {
((self.value >> 16) & 255) as u8
((self.0 >> 16) & 255) as u8
}

/// Returns the green RGB component of this Colour.
Expand All @@ -160,7 +155,7 @@ impl Colour {
/// assert_eq!(Colour::new(6573123).get_g(), 76);
/// ```
pub fn get_g(&self) -> u8 {
((self.value >> 8) & 255) as u8
((self.0 >> 8) & 255) as u8
}

/// Returns the blue RGB component of this Colour.
Expand All @@ -172,7 +167,7 @@ impl Colour {
///
/// assert_eq!(Colour::new(6573123).get_b(), 67);
pub fn get_b(&self) -> u8 {
(self.value & 255) as u8
(self.0 & 255) as u8
}

/// Returns a tuple of the red, green, and blue components of this Colour.
Expand Down Expand Up @@ -211,7 +206,7 @@ impl From<i32> for Colour {
/// assert_eq!(Colour::from(0xDEA584).get_tuple(), (222, 165, 132));
/// ```
fn from(value: i32) -> Colour {
Colour::new(value as u32)
Colour(value as u32)
}
}

Expand All @@ -228,7 +223,7 @@ impl From<u32> for Colour {
/// assert_eq!(Colour::from(6573123u32).get_r(), 100);
/// ```
fn from(value: u32) -> Colour {
Colour::new(value)
Colour(value)
}
}

Expand All @@ -245,7 +240,7 @@ impl From<u64> for Colour {
/// assert_eq!(Colour::from(6573123u64).get_r(), 100);
/// ```
fn from(value: u64) -> Colour {
Colour::new(value as u32)
Colour(value as u32)
}
}

Expand Down Expand Up @@ -309,8 +304,6 @@ colour! {
impl Default for Colour {
/// Creates a default value for a `Colour`, setting the inner value to `0`.
fn default() -> Colour {
Colour {
value: 0,
}
Colour(0)
}
}
20 changes: 10 additions & 10 deletions tests/test_colour.rs
Expand Up @@ -5,16 +5,16 @@ use std::u32;

#[test]
fn new() {
assert_eq!(Colour::new(1).value, 1);
assert_eq!(Colour::new(u32::MIN).value, u32::MIN);
assert_eq!(Colour::new(u32::MAX).value, u32::MAX);
assert_eq!(Colour::new(1).0, 1);
assert_eq!(Colour::new(u32::MIN).0, u32::MIN);
assert_eq!(Colour::new(u32::MAX).0, u32::MAX);
}

#[test]
fn from_rgb() {
assert_eq!(Colour::from_rgb(255, 0, 0).value, 0xFF0000);
assert_eq!(Colour::from_rgb(0, 255, 0).value, 0x00FF00);
assert_eq!(Colour::from_rgb(0, 0, 255).value, 0x0000FF);
assert_eq!(Colour::from_rgb(255, 0, 0).0, 0xFF0000);
assert_eq!(Colour::from_rgb(0, 255, 0).0, 0x00FF00);
assert_eq!(Colour::from_rgb(0, 0, 255).0, 0x0000FF);
}

#[test]
Expand All @@ -39,12 +39,12 @@ fn get_tuple() {

#[test]
fn default() {
assert_eq!(Colour::default().value, 0);
assert_eq!(Colour::default().0, 0);
}

#[test]
fn from() {
assert_eq!(Colour::from(7i32).value, 7);
assert_eq!(Colour::from(7u32).value, 7);
assert_eq!(Colour::from(7u64).value, 7);
assert_eq!(Colour::from(7i32).0, 7);
assert_eq!(Colour::from(7u32).0, 7);
assert_eq!(Colour::from(7u64).0, 7);
}

0 comments on commit a8acd61

Please sign in to comment.