File: api.rs

package info (click to toggle)
rust-imap-codec 2.0.0~alpha5%2B20250307-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,716 kB
  • sloc: makefile: 2; sh: 1
file content (225 lines) | stat: -rw-r--r-- 8,692 bytes parent folder | download
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
use std::fmt::{Debug, Display};

use imap_types::{
    command::{error::LoginError, Command, CommandBody},
    core::{AString, Atom, AtomExt, Charset, IString, Literal, NString, Quoted, Tag, Text},
    mailbox::{Mailbox, MailboxOther},
    response::Data,
    sequence::{SeqOrUid, Sequence, SequenceSet, MAX, MIN},
};

macro_rules! test_conversions {
    // Unvalidated
    (y, $try_from:tt, $from:tt, $as_ref:tt, $object:ty, $sample:expr) => {{
        let object = <$object>::unvalidated($sample);
        let _ = object.as_ref();

        test_conversions!($try_from, $from, $as_ref, $object, $sample);
    }};
    (n, $try_from:tt, $from:tt, $as_ref:tt, $object:ty, $sample:expr) => {{
        test_conversions!($try_from, $from, $as_ref, $object, $sample);
    }};

    // TryFrom
    (y, $from:tt, $as_ref:tt, $object:ty, $sample:expr) => {{
        let _ = <$object>::try_from($sample).unwrap();
        let _ = <$object>::try_from($sample.to_owned()).unwrap();
        let _ = <$object>::try_from($sample.as_bytes()).unwrap();
        let _ = <$object>::try_from($sample.as_bytes().to_vec()).unwrap();

        test_conversions!($from, $as_ref, $object, $sample);
    }};
    (n, $from:tt, $as_ref:tt, $object:ty, $sample:expr) => {{
        test_conversions!($from, $as_ref, $object, $sample);
    }};

    // From
    (y, $as_ref:tt, $object:ty, $sample:expr) => {{
        let _ = <$object>::from($sample);

        test_conversions!($as_ref, $object, $sample);
    }};
    (n, $as_ref:tt, $object:ty, $sample:expr) => {{
        test_conversions!($as_ref, $object, $sample);
    }};

    // AsRef
    (y, $object:ty, $sample:expr) => {{
        let object = <$object>::try_from($sample).unwrap();
        let _ = object.as_ref();

        // ...
    }};
    (n, $object:ty, $sample:expr) => {{
        // ...
    }};
}

#[test]
fn test_constructions() {
    // Unvalidated | TryFrom | From | AsRef | Type | Sample
    test_conversions!(y, y, n, y, Tag, "tag");
    test_conversions!(y, y, n, y, Text, "text");
    // --------------------------------------------
    test_conversions!(n, y, n, y, AString, "astring");
    test_conversions!(y, y, n, y, Atom, "atom");
    test_conversions!(y, y, n, y, AtomExt, "atomext");
    test_conversions!(n, y, n, y, IString, "istring");
    test_conversions!(y, y, n, y, Quoted, "quoted");
    test_conversions!(n, y, n, y, Literal, "literal");
    test_conversions!(n, y, n, n, NString, "nstring");
    // --------------------------------------------
    test_conversions!(n, y, n, n, Mailbox, "mailbox");
    test_conversions!(n, y, n, y, MailboxOther, "mailbox");
    // --------------------------------------------
    test_conversions!(n, y, n, y, Charset, "charset");
}

#[test]
fn test_construction_of_command() {
    trait DisplayDebug: Display + Debug {}

    impl<T> DisplayDebug for T where T: Display + Debug {}

    match CommandBody::login("\x00", "") {
        Err(LoginError::Username(e)) => println!("Oops, bad username: {}", e),
        Err(LoginError::Password(e)) => println!("Oops, bad password: {:?}", e),
        _ => {}
    }

    let tests: Vec<Box<dyn DisplayDebug>> = vec![
        Box::new(Command::new(b"".as_ref(), CommandBody::Noop).unwrap_err()),
        Box::new(Command::new(b"A ".as_ref(), CommandBody::Noop).unwrap_err()),
        Box::new(Command::new(b"\xff".as_ref(), CommandBody::Noop).unwrap_err()),
        Box::new("---"),
        Box::new(Command::new("", CommandBody::Noop).unwrap_err()),
        Box::new(Command::new("A ", CommandBody::Noop).unwrap_err()),
        Box::new("---"),
        Box::new(Command::new(String::from(""), CommandBody::Noop).unwrap_err()),
        Box::new(Command::new(String::from("A "), CommandBody::Noop).unwrap_err()),
        Box::new("---"),
        Box::new(Command::new(Vec::from(b"".as_ref()), CommandBody::Noop).unwrap_err()),
        Box::new(Command::new(Vec::from(b"\xff".as_ref()), CommandBody::Noop).unwrap_err()),
        Box::new("---"),
        Box::new(Atom::try_from("").unwrap_err()),
        Box::new(Atom::try_from("²").unwrap_err()),
        Box::new("---"),
        Box::new(AtomExt::try_from("").unwrap_err()),
        Box::new(AtomExt::try_from("²").unwrap_err()),
        Box::new("---"),
        Box::new(CommandBody::login("\x00", "").unwrap_err()),
        Box::new(CommandBody::login("", b"\x00".as_ref()).unwrap_err()),
        Box::new("---"),
        Box::new(Data::capability(vec![]).unwrap_err()),
    ];

    for test in tests.into_iter() {
        println!("{test:?} // {test}");
    }
}

#[test]
fn test_construction_of_sequence_etc() {
    // # From
    // ## SequenceSet
    let _ = SequenceSet::from(MIN);
    let _ = SequenceSet::from(MAX);
    let _ = SequenceSet::from(..);
    let _ = SequenceSet::from(MIN..);
    let _ = SequenceSet::try_from(MIN..MAX).unwrap();
    let _ = SequenceSet::from(MIN..=MAX);
    let _ = SequenceSet::try_from(..MAX).unwrap();
    let _ = SequenceSet::from(MIN..=MAX);
    // ## Sequence
    let _ = Sequence::from(MIN);
    let _ = Sequence::from(MAX);
    let _ = Sequence::from(..);
    let _ = Sequence::from(MIN..);
    let _ = Sequence::try_from(MIN..MAX).unwrap();
    let _ = Sequence::from(MIN..=MAX);
    let _ = Sequence::try_from(..MAX).unwrap();
    let _ = Sequence::from(MIN..=MAX);
    // ## SeqOrUid
    let _ = SeqOrUid::from(MIN);
    let _ = SeqOrUid::from(MAX);

    macro_rules! try_from {
        ($min:literal, $max:literal) => {
            let _ = SequenceSet::try_from($min).unwrap();
            let _ = SequenceSet::try_from($max).unwrap();
            let _ = SequenceSet::try_from(..).unwrap();
            let _ = SequenceSet::try_from($min..).unwrap();
            let _ = SequenceSet::try_from($min..$max).unwrap();
            let _ = SequenceSet::try_from(..$max).unwrap();
            let _ = SequenceSet::try_from($min..$max).unwrap();

            let _ = Sequence::try_from($min).unwrap();
            let _ = Sequence::try_from($max).unwrap();
            let _ = Sequence::try_from(..).unwrap();
            let _ = Sequence::try_from($min..).unwrap();
            let _ = Sequence::try_from($min..$max).unwrap();
            let _ = Sequence::try_from(..$max).unwrap();
            let _ = Sequence::try_from($min..$max).unwrap();

            let _ = SeqOrUid::try_from($min).unwrap();
            let _ = SeqOrUid::try_from($max).unwrap();
        };
    }

    try_from!(1i8, 127i8);
    try_from!(1i16, 32_767i16);
    try_from!(1i32, 2_147_483_647i32);
    try_from!(1i64, 2_147_483_647i64);
    try_from!(1isize, 2_147_483_647isize);
    try_from!(1u8, 255u8);
    try_from!(1u16, 65_535u16);
    try_from!(1u32, 4_294_967_295u32);
    try_from!(1u64, 4_294_967_295u64);
    try_from!(1usize, 4_294_967_295usize);

    macro_rules! try_from_fail_zero {
        ($min:literal, $max:literal) => {
            let _ = SequenceSet::try_from($min).unwrap_err();
            let _ = SequenceSet::try_from($min..).unwrap_err();
            let _ = SequenceSet::try_from($min..$max).unwrap_err();
            let _ = SequenceSet::try_from($min..$max).unwrap_err();

            let _ = Sequence::try_from($min).unwrap_err();
            let _ = Sequence::try_from($min..).unwrap_err();
            let _ = Sequence::try_from($min..$max).unwrap_err();
            let _ = Sequence::try_from($min..$max).unwrap_err();

            let _ = SeqOrUid::try_from($min).unwrap_err();
        };
    }

    try_from_fail_zero!(0i8, 127i8);
    try_from_fail_zero!(0i16, 32_767i16);
    try_from_fail_zero!(0i32, 2_147_483_647i32);
    try_from_fail_zero!(0i64, 2_147_483_647i64);
    try_from_fail_zero!(0isize, 2_147_483_647isize);
    try_from_fail_zero!(0u8, 255u8);
    try_from_fail_zero!(0u16, 65_535u16);
    try_from_fail_zero!(0u32, 4_294_967_295u32);
    try_from_fail_zero!(0u64, 4_294_967_295u64);
    try_from_fail_zero!(0usize, 4_294_967_295usize);

    macro_rules! try_from_fail_max {
        ($min:literal, $max:literal) => {
            let _ = SequenceSet::try_from($max).unwrap_err();
            let _ = SequenceSet::try_from($min..$max).unwrap_err();
            let _ = SequenceSet::try_from(..$max).unwrap_err();
            let _ = SequenceSet::try_from($min..$max).unwrap_err();

            let _ = Sequence::try_from($max).unwrap_err();
            let _ = Sequence::try_from($min..$max).unwrap_err();
            let _ = Sequence::try_from(..$max).unwrap_err();
            let _ = Sequence::try_from($min..$max).unwrap_err();

            let _ = SeqOrUid::try_from($max).unwrap_err();
        };
    }

    try_from_fail_max!(1i64, 9_223_372_036_854_775_807i64);
    try_from_fail_max!(1u64, 18_446_744_073_709_551_615u64);
}