use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, Eq, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Operator {
Channel,
Channels,
Stream,
Streams,
Topic,
Dm,
#[serde(rename = "dm-including")]
DmIncluding,
Sender,
Has,
Is,
#[serde(rename = "-is")]
NotIs,
Near,
}
impl Operator {
pub const CHANNEL: &str = "channel";
pub const CHANNELS: &str = "channels";
pub const STREAM: &str = "stream";
pub const STREAMS: &str = "streams";
pub const TOPIC: &str = "topic";
pub const DM: &str = "dm";
pub const DM_INCLUDING: &str = "dm-including";
pub const SENDER: &str = "sender";
pub const HAS: &str = "has";
pub const IS: &str = "is";
pub const NOT_IS: &str = "-is";
pub const NEAR: &str = "near";
pub const fn new() -> Self {
Self::Channel
}
pub const fn as_str(&self) -> &'static str {
match self {
Self::Channel => Self::CHANNEL,
Self::Channels => Self::CHANNELS,
Self::Stream => Self::STREAM,
Self::Streams => Self::STREAMS,
Self::Topic => Self::TOPIC,
Self::Dm => Self::DM,
Self::DmIncluding => Self::DM_INCLUDING,
Self::Sender => Self::SENDER,
Self::Has => Self::HAS,
Self::Is => Self::IS,
Self::NotIs => Self::NOT_IS,
Self::Near => Self::NEAR,
}
}
}
impl Default for Operator {
fn default() -> Self {
Self::new()
}
}
impl From<Operator> for &'static str {
fn from(val: Operator) -> Self {
val.as_str()
}
}
impl From<Operator> for String {
fn from(val: Operator) -> Self {
val.as_str().to_string()
}
}
impl std::fmt::Display for Operator {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.as_str())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_filter_operator() {
[
Operator::Channel,
Operator::Channels,
Operator::Stream,
Operator::Streams,
Operator::Topic,
Operator::Dm,
Operator::DmIncluding,
Operator::Sender,
Operator::Has,
Operator::Is,
Operator::NotIs,
Operator::Near,
]
.into_iter()
.for_each(|op| {
let op_json = format!(r#""{op}""#);
assert_eq!(serde_json::to_string(&op).unwrap(), op_json);
assert_eq!(serde_json::from_str::<Operator>(&op_json).unwrap(), op);
});
}
}