1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//! The IMAP COMPRESS Extension
//!
//! This extension defines a new type ...
//!
//! * [`CompressionAlgorithm`]
//!
//! ... and extends ...
//!
//! * the [`Capability`](crate::response::Capability) enum with a new variant [`Capability::Compress`](crate::response::Capability#variant.Compress),
//! * the [`Command`](crate::command::Command) enum with a new variant [`Command::Compress`](crate::command::Command#variant.Compress), and
//! * the [`Code`](crate::response::Code) enum with a new variant [`Code::CompressionActive`](crate::response::Code#variant.CompressionActive).

use std::fmt::{Display, Formatter};

#[cfg(feature = "arbitrary")]
use arbitrary::Arbitrary;
use bounded_static_derive::ToStatic;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::{
    command::CommandBody,
    core::Atom,
    error::{ValidationError, ValidationErrorKind},
};

impl<'a> CommandBody<'a> {
    /// <div class="warning">
    /// This extension must only be used when the server advertised support for it sending the COMPRESS* capability.
    /// </div>
    pub fn compress(algorithm: CompressionAlgorithm) -> Self {
        CommandBody::Compress { algorithm }
    }
}

#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, Hash, ToStatic)]
#[non_exhaustive]
pub enum CompressionAlgorithm {
    Deflate,
}

impl Display for CompressionAlgorithm {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_str(match self {
            Self::Deflate => "DEFLATE",
        })
    }
}

impl<'a> TryFrom<&'a str> for CompressionAlgorithm {
    type Error = ValidationError;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        match value.to_ascii_lowercase().as_ref() {
            "deflate" => Ok(Self::Deflate),
            _ => Err(ValidationError::new(ValidationErrorKind::Invalid)),
        }
    }
}

impl<'a> TryFrom<&'a [u8]> for CompressionAlgorithm {
    type Error = ValidationError;

    fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
        match value.to_ascii_lowercase().as_slice() {
            b"deflate" => Ok(Self::Deflate),
            _ => Err(ValidationError::new(ValidationErrorKind::Invalid)),
        }
    }
}

impl<'a> TryFrom<Atom<'a>> for CompressionAlgorithm {
    type Error = ValidationError;

    fn try_from(atom: Atom<'a>) -> Result<Self, Self::Error> {
        match atom.as_ref().to_ascii_lowercase().as_ref() {
            "deflate" => Ok(Self::Deflate),
            _ => Err(ValidationError::new(ValidationErrorKind::Invalid)),
        }
    }
}

impl AsRef<str> for CompressionAlgorithm {
    fn as_ref(&self) -> &str {
        match self {
            CompressionAlgorithm::Deflate => "DEFLATE",
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_conversion() {
        let tests = [(CompressionAlgorithm::Deflate, "DEFLATE")];

        for (object, string) in tests {
            // Create from `&[u8]`.
            let got = CompressionAlgorithm::try_from(string.as_bytes()).unwrap();
            assert_eq!(object, got);

            // Create from `&str`.
            let got = CompressionAlgorithm::try_from(string).unwrap();
            assert_eq!(object, got);

            // Create from `Atom`.
            let got = CompressionAlgorithm::try_from(Atom::try_from(string).unwrap()).unwrap();
            assert_eq!(object, got);

            // AsRef
            let encoded = object.as_ref();
            assert_eq!(encoded, string);
        }
    }

    #[test]
    fn test_conversion_failing() {
        let tests = [
            "", "D", "DE", "DEF", "DEFL", "DEFLA", "DEFLAT", "DEFLATX", "DEFLATEX", "XDEFLATE",
        ];

        for string in tests {
            // Create from `&[u8]`.
            assert!(CompressionAlgorithm::try_from(string.as_bytes()).is_err());

            // Create from `&str`.
            assert!(CompressionAlgorithm::try_from(string).is_err());

            if !string.is_empty() {
                // Create from `Atom`.
                assert!(CompressionAlgorithm::try_from(Atom::try_from(string).unwrap()).is_err());
            }
        }
    }
}