use serde::{Deserialize, Serialize};
use std::fmt;
use std::str::FromStr;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum CommunityConnectionsPlatform {
Discord,
Telegram,
Slack,
Whatsapp,
Unknown(String),
}
impl CommunityConnectionsPlatform {
#[allow(deprecated)]
pub fn as_str(&self) -> &str {
match self {
Self::Discord => "discord",
Self::Telegram => "telegram",
Self::Slack => "slack",
Self::Whatsapp => "whatsapp",
Self::Unknown(s) => s.as_str(),
}
}
pub fn is_known(&self) -> bool {
!matches!(self, Self::Unknown(_))
}
}
impl fmt::Display for CommunityConnectionsPlatform {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl AsRef<str> for CommunityConnectionsPlatform {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl FromStr for CommunityConnectionsPlatform {
type Err = std::convert::Infallible;
#[allow(deprecated)]
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"discord" => Self::Discord,
"telegram" => Self::Telegram,
"slack" => Self::Slack,
"whatsapp" => Self::Whatsapp,
other => Self::Unknown(other.to_string()),
})
}
}
impl From<String> for CommunityConnectionsPlatform {
fn from(s: String) -> Self {
match Self::from_str(&s) {
Ok(Self::Unknown(_)) => Self::Unknown(s),
Ok(other) => other,
}
}
}
impl From<&str> for CommunityConnectionsPlatform {
fn from(s: &str) -> Self {
Self::from_str(s).unwrap_or_else(|_| Self::Unknown(s.to_string()))
}
}
impl Serialize for CommunityConnectionsPlatform {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(self.as_str())
}
}
impl<'de> Deserialize<'de> for CommunityConnectionsPlatform {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let s = String::deserialize(deserializer)?;
Ok(Self::from(s))
}
}