use jmap_types::impl_string_enum;
use serde::Serialize;
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum ContactPresenceFilter {
Online,
Away,
Busy,
Invisible,
Offline,
}
impl TryFrom<jmap_chat_types::Presence> for ContactPresenceFilter {
type Error = jmap_chat_types::Presence;
fn try_from(p: jmap_chat_types::Presence) -> Result<Self, Self::Error> {
match p {
jmap_chat_types::Presence::Online => Ok(ContactPresenceFilter::Online),
jmap_chat_types::Presence::Away => Ok(ContactPresenceFilter::Away),
jmap_chat_types::Presence::Busy => Ok(ContactPresenceFilter::Busy),
jmap_chat_types::Presence::Invisible => Ok(ContactPresenceFilter::Invisible),
jmap_chat_types::Presence::Offline => Ok(ContactPresenceFilter::Offline),
other => Err(other),
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum QuotaScope {
Account,
Domain,
Global,
Other(String),
}
impl QuotaScope {
pub fn as_str(&self) -> &str {
match self {
Self::Account => "account",
Self::Domain => "domain",
Self::Global => "global",
Self::Other(s) => s.as_str(),
}
}
}
impl_string_enum!(QuotaScope, "a QuotaScope wire string",
"account" => Account,
"domain" => Domain,
"global" => Global,
);
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum QuotaResourceType {
Count,
Octets,
Other(String),
}
impl QuotaResourceType {
pub fn as_str(&self) -> &str {
match self {
Self::Count => "count",
Self::Octets => "octets",
Self::Other(s) => s.as_str(),
}
}
}
impl_string_enum!(QuotaResourceType, "a QuotaResourceType wire string",
"count" => Count,
"octets" => Octets,
);
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub enum ChatMemberRole {
Admin,
Member,
Other(String),
}
impl ChatMemberRole {
pub fn as_str(&self) -> &str {
match self {
Self::Admin => "admin",
Self::Member => "member",
Self::Other(s) => s.as_str(),
}
}
}
impl_string_enum!(ChatMemberRole, "a ChatMemberRole wire string",
"admin" => Admin,
"member" => Member,
);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn quota_scope_unknown_round_trips_via_other() {
let raw = r#""siteCustom-tier-A""#;
let parsed: QuotaScope = serde_json::from_str(raw).expect("must deserialize");
assert_eq!(parsed, QuotaScope::Other("siteCustom-tier-A".to_owned()));
assert_eq!(serde_json::to_string(&parsed).unwrap(), raw);
}
#[test]
fn quota_resource_type_unknown_round_trips_via_other() {
let raw = r#""vendorUnit-decibels""#;
let parsed: QuotaResourceType = serde_json::from_str(raw).expect("must deserialize");
assert_eq!(
parsed,
QuotaResourceType::Other("vendorUnit-decibels".to_owned())
);
assert_eq!(serde_json::to_string(&parsed).unwrap(), raw);
}
#[test]
fn chat_member_role_unknown_round_trips_via_other() {
let raw = r#""moderator""#;
let parsed: ChatMemberRole = serde_json::from_str(raw).expect("must deserialize");
assert_eq!(parsed, ChatMemberRole::Other("moderator".to_owned()));
assert_eq!(serde_json::to_string(&parsed).unwrap(), raw);
}
#[test]
fn quota_scope_canonical_variants_round_trip() {
let cases: &[(&str, QuotaScope)] = &[
(r#""account""#, QuotaScope::Account),
(r#""domain""#, QuotaScope::Domain),
(r#""global""#, QuotaScope::Global),
];
for (raw, expected) in cases {
let parsed: QuotaScope = serde_json::from_str(raw).expect("must deserialize");
assert_eq!(
&parsed, expected,
"wire {raw} must deserialise to {expected:?}"
);
assert_eq!(
serde_json::to_string(&parsed).unwrap(),
*raw,
"wire {raw} must round-trip"
);
}
}
}