pub mod announce;
pub mod api;
pub mod channel;
pub mod emoji;
pub mod gateway;
pub mod guild;
pub mod message;
pub mod permission;
pub mod robot;
pub mod schedule;
pub mod user;
pub use announce::*;
pub use api::*;
pub use channel::*;
pub use emoji::*;
pub use gateway::*;
pub use message::*;
pub use permission::*;
pub use robot::*;
pub use schedule::*;
pub use user::*;
pub use guild::{Guild, Member, Role};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
pub type Snowflake = String;
pub type Timestamp = DateTime<Utc>;
pub trait HasId {
fn id(&self) -> Option<&Snowflake>;
fn id_string(&self) -> String {
self.id().cloned().unwrap_or_default()
}
}
pub trait HasName {
fn name(&self) -> &str;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(from = "u8", into = "u8")]
#[repr(u8)]
pub enum ChannelType {
Text = 0,
Voice = 2,
Category = 4,
Announcement = 5,
Thread = 10,
Live = 12,
Application = 13,
Forum = 15,
Unknown(u8),
}
impl From<u8> for ChannelType {
fn from(value: u8) -> Self {
match value {
0 => Self::Text,
2 => Self::Voice,
4 => Self::Category,
5 => Self::Announcement,
10 => Self::Thread,
12 => Self::Live,
13 => Self::Application,
15 => Self::Forum,
other => Self::Unknown(other),
}
}
}
impl From<ChannelType> for u8 {
fn from(channel_type: ChannelType) -> Self {
match channel_type {
ChannelType::Text => 0,
ChannelType::Voice => 2,
ChannelType::Category => 4,
ChannelType::Announcement => 5,
ChannelType::Thread => 10,
ChannelType::Live => 12,
ChannelType::Application => 13,
ChannelType::Forum => 15,
ChannelType::Unknown(value) => value,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(from = "u8", into = "u8")]
#[repr(u8)]
pub enum MessageType {
Default = 0,
System = 1,
Unknown(u8),
}
impl From<u8> for MessageType {
fn from(value: u8) -> Self {
match value {
0 => Self::Default,
1 => Self::System,
other => Self::Unknown(other),
}
}
}
impl From<MessageType> for u8 {
fn from(message_type: MessageType) -> Self {
match message_type {
MessageType::Default => 0,
MessageType::System => 1,
MessageType::Unknown(value) => value,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Color(pub u32);
impl Color {
pub const fn from_rgb(r: u8, g: u8, b: u8) -> Self {
Self(((r as u32) << 16) | ((g as u32) << 8) | (b as u32))
}
pub const fn from_hex(hex: u32) -> Self {
Self(hex)
}
pub const fn r(self) -> u8 {
((self.0 >> 16) & 0xFF) as u8
}
pub const fn g(self) -> u8 {
((self.0 >> 8) & 0xFF) as u8
}
pub const fn b(self) -> u8 {
(self.0 & 0xFF) as u8
}
pub const fn hex(self) -> u32 {
self.0
}
pub const RED: Color = Color::from_rgb(255, 0, 0);
pub const GREEN: Color = Color::from_rgb(0, 255, 0);
pub const BLUE: Color = Color::from_rgb(0, 0, 255);
pub const WHITE: Color = Color::from_rgb(255, 255, 255);
pub const BLACK: Color = Color::from_rgb(0, 0, 0);
pub const YELLOW: Color = Color::from_rgb(255, 255, 0);
pub const CYAN: Color = Color::from_rgb(0, 255, 255);
pub const MAGENTA: Color = Color::from_rgb(255, 0, 255);
}
impl std::fmt::Display for Color {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "#{:06X}", self.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_channel_type_conversion() {
assert_eq!(ChannelType::from(0), ChannelType::Text);
assert_eq!(u8::from(ChannelType::Text), 0);
assert_eq!(ChannelType::from(99), ChannelType::Unknown(99));
assert_eq!(u8::from(ChannelType::Unknown(99)), 99);
}
#[test]
fn test_color() {
let red = Color::from_rgb(255, 0, 0);
assert_eq!(red.r(), 255);
assert_eq!(red.g(), 0);
assert_eq!(red.b(), 0);
assert_eq!(red.hex(), 0xFF0000);
let color = Color::from_hex(0x123456);
assert_eq!(color.r(), 0x12);
assert_eq!(color.g(), 0x34);
assert_eq!(color.b(), 0x56);
assert_eq!(format!("{}", Color::RED), "#FF0000");
}
}