use std::borrow::Cow;
use std::fmt::{self, Display};
use std::ops::Deref;
use serde::{Deserialize, Serialize};
use crate::model::guild::Member;
use crate::model::id::{ChannelId, GuildId, UserId};
use crate::model::utils::is_false;
#[derive(Clone, Debug, Eq, Hash, PartialEq, Deserialize, Serialize)]
#[serde(transparent)]
pub struct VoiceRegionId(pub(crate) Cow<'static, str>);
impl VoiceRegionId {
pub const fn from_static(id: &'static str) -> VoiceRegionId {
VoiceRegionId(Cow::Borrowed(id))
}
pub fn from_string<S: Into<String>>(id: S) -> VoiceRegionId {
VoiceRegionId(Cow::Owned(id.into()))
}
}
impl From<&'static str> for VoiceRegionId {
fn from(s: &'static str) -> Self {
VoiceRegionId(Cow::Borrowed(s))
}
}
impl From<String> for VoiceRegionId {
fn from(s: String) -> Self {
VoiceRegionId(Cow::Owned(s))
}
}
impl Display for VoiceRegionId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
impl Deref for VoiceRegionId {
type Target = str;
fn deref(&self) -> &Self::Target {
self.0.deref()
}
}
impl AsRef<str> for VoiceRegionId {
fn as_ref(&self) -> &str {
self.0.as_ref()
}
}
impl From<VoiceRegionId> for String {
fn from(id: VoiceRegionId) -> Self {
id.0.to_string()
}
}
impl From<VoiceRegionId> for Cow<'static, str> {
fn from(id: VoiceRegionId) -> Self {
id.0
}
}
impl PartialEq<str> for VoiceRegionId {
fn eq(&self, other: &str) -> bool {
self.0 == other
}
}
impl PartialEq<String> for VoiceRegionId {
fn eq(&self, other: &String) -> bool {
self.0 == other[..]
}
}
impl<'a> PartialEq<Cow<'a, str>> for VoiceRegionId {
fn eq(&self, other: &Cow<'a, str>) -> bool {
self.0[..] == other[..]
}
}
macro_rules! region_id {
($(
$(#[$attr:meta])*
const $name:ident = $id:expr;
)*) => {
#[allow(missing_docs)]
impl VoiceRegionId {
$(
$(#[$attr])*
pub const $name: VoiceRegionId = VoiceRegionId::from_static($id);
)*
}
};
}
region_id! {
#[deprecated]
const AMSTERDAM = "amsterdam";
const BRAZIL = "brazil";
#[deprecated]
const DUBAI = "dubai";
#[deprecated]
const EU_CENTRAL = "eu-central";
#[deprecated]
const EU_WEST = "eu-west";
const EUROPE = "europe";
#[deprecated]
const FRANKFURT = "frankfurt";
const HONG_KONG = "hongkong";
const INDIA = "india";
const JAPAN = "japan";
#[deprecated]
const LONDON = "london";
const RUSSIA = "russia";
const SINGAPORE = "singapore";
const SOUTH_AFRICA = "southafrica";
const SYDNEY = "sydney";
const US_CENTRAL = "us-central";
const US_EAST = "us-east";
const US_SOUTH = "us-south";
const US_WEST = "us-west";
}
#[non_exhaustive]
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct VoiceRegion {
pub id: VoiceRegionId,
pub name: String,
pub vip: bool,
pub optimal: bool,
pub deprecated: bool,
pub custom: bool,
}
#[non_exhaustive]
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct VoiceState {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub guild_id: Option<GuildId>,
pub channel_id: Option<ChannelId>,
pub user_id: UserId,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub member: Option<Member>,
pub session_id: String,
pub deaf: bool,
pub mute: bool,
pub self_deaf: bool,
pub self_mute: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub self_stream: bool,
pub suppress: bool,
}
impl_eq_fields!(VoiceState: [
guild_id,
channel_id,
user_id,
member,
session_id,
deaf,
mute,
self_deaf,
self_mute,
self_stream,
suppress,
]);