mirror of https://git.meli.delivery/meli/meli
melib/imap: capability types WIP
Signed-off-by: Manos Pitsidianakis <manos@pitsidianak.is>imap-capabilities
parent
60cccdde2d
commit
250892a365
@ -0,0 +1,113 @@
|
||||
//
|
||||
// meli
|
||||
//
|
||||
// Copyright 2024 Emmanouil Pitsidianakis <manos@pitsidianak.is>
|
||||
//
|
||||
// This file is part of meli.
|
||||
//
|
||||
// meli is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// meli is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with meli. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
// SPDX-License-Identifier: EUPL-1.2 OR GPL-3.0-or-later
|
||||
|
||||
use serde::{de::DeserializeOwned, ser::Serialize};
|
||||
|
||||
pub trait Capability:
|
||||
Clone
|
||||
+ Copy
|
||||
+ std::fmt::Debug
|
||||
+ PartialEq
|
||||
+ Eq
|
||||
+ std::hash::Hash
|
||||
+ Serialize
|
||||
+ DeserializeOwned
|
||||
+ AsRef<str>
|
||||
+ Send
|
||||
+ Sync
|
||||
{
|
||||
const NAME: &'static str;
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! _impl_imap_capability {
|
||||
($(#[$outer:meta])*$ident:ident : $key:literal) => {
|
||||
$crate::_impl_imap_capability! {
|
||||
$(#[$outer])*$ident: $key, name: $key
|
||||
}
|
||||
};
|
||||
($(#[$outer:meta])*$ident:ident : $key:literal, name: $name:literal) => {
|
||||
$(#[$outer])*
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
|
||||
pub struct $ident;
|
||||
|
||||
impl $crate::imap::capabilities::Capability for $ident {
|
||||
const NAME: &'static str = $name;
|
||||
}
|
||||
|
||||
impl $ident {
|
||||
pub const fn name() -> &'static str {
|
||||
<Self as $crate::imap::capabilities::Capability>::NAME
|
||||
}
|
||||
}
|
||||
|
||||
impl ::serde::ser::Serialize for $ident {
|
||||
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
|
||||
where
|
||||
S: ::serde::ser::Serializer,
|
||||
{
|
||||
serializer.serialize_str(<Self as $crate::imap::capabilities::Capability>::NAME)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> ::serde::de::Deserialize<'de> for $ident {
|
||||
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
|
||||
where
|
||||
D: ::serde::de::Deserializer<'de>,
|
||||
{
|
||||
if <&'_ str>::deserialize(deserializer)? == <Self as $crate::imap::capabilities::Capability>::NAME {
|
||||
return Ok(Self);
|
||||
}
|
||||
|
||||
Err(::serde::de::Error::custom(concat!("Expected string with value \"", $key, '"')))
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<str> for $ident {
|
||||
fn as_ref(&self) -> &'static str {
|
||||
<Self as $crate::imap::capabilities::Capability>::NAME
|
||||
}
|
||||
}
|
||||
|
||||
impl ::std::fmt::Display for $ident {
|
||||
fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
|
||||
write!(fmt, "IMAP {} Capability", Self::name())
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
_impl_imap_capability! { Idle: "IDLE" }
|
||||
_impl_imap_capability! { Condstore: "CONDSTORE" }
|
||||
_impl_imap_capability! { Deflate: "COMPRESS=DEFLATE" }
|
||||
_impl_imap_capability! { OAuth2: "AUTH=OAUTH2" }
|
||||
_impl_imap_capability! { XOAuth2: "AUTH=XOAUTH2" }
|
||||
_impl_imap_capability! { Enable: "ENABLE" }
|
||||
_impl_imap_capability! { IMAP4Rev1: "IMAP4REV1" }
|
||||
_impl_imap_capability! { ListExtended: "LIST-EXTENDED" }
|
||||
_impl_imap_capability! { ListStatus: "LIST-STATUS" }
|
||||
_impl_imap_capability! { LiteralPlus: "LITERAL+" }
|
||||
_impl_imap_capability! { Login: "LOGIN" }
|
||||
_impl_imap_capability! { LoginDisabled: "LOGINDISABLED" }
|
||||
_impl_imap_capability! { Move: "MOVE" }
|
||||
_impl_imap_capability! { SpecialUse: "SPECIAL-USE" }
|
||||
_impl_imap_capability! { Unselect: "UNSELECT" }
|
Loading…
Reference in New Issue