use serde_json::{Map, Value};
use crate::{getter, permissions::Permission, snowflake::Snowflake};
#[derive(Debug, Clone)]
pub struct Color(u8, u8, u8);
impl Color {
pub fn from_hex(hex: u32) -> Self {
Self(
(hex & 0xFF) as u8,
((hex & 0xFF00) >> 8) as u8,
((hex & 0xFF0000) >> 16) as u8,
)
}
}
#[derive(Debug, Clone)]
pub struct Role {
id: Snowflake,
name: String,
color: Color,
hoist: bool,
icon_url: Option<String>,
position: u64,
mentionable: bool,
permissions: Vec<Permission>,
}
impl Role {
pub(crate) fn from_json(json: &Map<String, Value>) -> Self {
Self {
id: json["id"].as_str().unwrap().parse().unwrap(),
color: Color::from_hex(json["color"].as_u64().unwrap() as u32),
hoist: json["hoist"].as_bool().unwrap(),
icon_url: if let Some(icon_hash) = json["icon"].as_str() {
Some(format!(
"https://cdn.discordapp.com/role-icons/{role_id}/{role_icon}.png",
role_id = json["id"].as_str().unwrap(),
role_icon = icon_hash
))
} else {
None
},
name: json["name"].as_str().unwrap().to_string(),
position: json["position"].as_u64().unwrap(),
mentionable: json["mentionable"].as_bool().unwrap(),
permissions: Permission::get_permissions(
json["permissions"].as_str().unwrap().parse().unwrap(),
),
}
}
getter!(&id -> Snowflake);
getter!(&name -> String);
getter!(&color -> Color);
getter!(hoist -> bool);
getter!(&icon_url -> Option<String>);
getter!(position -> u64);
getter!(mentionable -> bool);
getter!(&permissions -> Vec<Permission>);
}