zulip-api 0.0.8

Client library for interacting with the Zulip API
Documentation
use serde::{Deserialize, Serialize};

/// Represents a filter operator used for searching and getting messages.
#[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 {
    /// Represents the string value of the channel [Operator].
    pub const CHANNEL: &str = "channel";
    /// Represents the string value of the channels [Operator].
    pub const CHANNELS: &str = "channels";
    /// Represents the string value of the stream [Operator].
    pub const STREAM: &str = "stream";
    /// Represents the string value of the streams [Operator].
    pub const STREAMS: &str = "streams";
    /// Represents the string value of the topic [Operator].
    pub const TOPIC: &str = "topic";
    /// Represents the string value of the dm [Operator].
    pub const DM: &str = "dm";
    /// Represents the string value of the dm-including [Operator].
    pub const DM_INCLUDING: &str = "dm-including";
    /// Represents the string value of the sender [Operator].
    pub const SENDER: &str = "sender";
    /// Represents the string value of the has [Operator].
    pub const HAS: &str = "has";
    /// Represents the string value of the is [Operator].
    pub const IS: &str = "is";
    /// Represents the string value of the not-is [Operator].
    pub const NOT_IS: &str = "-is";
    /// Represents the string value of the near [Operator].
    pub const NEAR: &str = "near";

    /// Creates a new [Operator].
    pub const fn new() -> Self {
        Self::Channel
    }

    /// Gets the [Operator] string representation.
    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);
        });
    }
}