File: client_std.rs

package info (click to toggle)
rust-imap-next 0.3.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 496 kB
  • sloc: makefile: 2; sh: 1
file content (90 lines) | stat: -rw-r--r-- 3,047 bytes parent folder | download | duplicates (2)
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
use std::{
    io::{Read, Write},
    net::TcpStream,
};

use imap_next::{
    client::{Client, Event, Options},
    imap_types::{
        command::{Command, CommandBody},
        core::Tag,
    },
    Interrupt, Io, State,
};

fn main() {
    let mut stream = TcpStream::connect("127.0.0.1:12345").unwrap();
    let mut read_buffer = [0; 128];
    let mut client = Client::new(Options::default());

    let greeting = loop {
        match client.next() {
            Err(interrupt) => match interrupt {
                Interrupt::Io(Io::NeedMoreInput) => {
                    let count = stream.read(&mut read_buffer).unwrap();
                    client.enqueue_input(&read_buffer[0..count]);
                }
                interrupt => panic!("unexpected interrupt: {interrupt:?}"),
            },
            Ok(event) => match event {
                Event::GreetingReceived { greeting } => break greeting,
                event => println!("unexpected event: {event:?}"),
            },
        }
    };

    println!("received greeting: {greeting:?}");

    let handle = client.enqueue_command(Command {
        tag: Tag::try_from("A1").unwrap(),
        body: CommandBody::login("Al¹cE", "pa²²w0rd").unwrap(),
    });

    loop {
        match client.next() {
            Err(interrupt) => match interrupt {
                Interrupt::Io(Io::NeedMoreInput) => {
                    let count = stream.read(&mut read_buffer).unwrap();
                    client.enqueue_input(&read_buffer[0..count]);
                }
                Interrupt::Io(Io::Output(bytes)) => {
                    stream.write_all(&bytes).unwrap();
                }
                Interrupt::Error(error) => {
                    panic!("unexpected error: {error:?}");
                }
            },
            Ok(event) => match event {
                Event::CommandSent {
                    handle: got_handle,
                    command,
                } => {
                    println!("command sent: {got_handle:?}, {command:?}");
                    assert_eq!(handle, got_handle);
                }
                Event::CommandRejected {
                    handle: got_handle,
                    command,
                    status,
                } => {
                    println!("command rejected: {got_handle:?}, {command:?}, {status:?}");
                    assert_eq!(handle, got_handle);
                }
                Event::DataReceived { data } => {
                    println!("data received: {data:?}");
                }
                Event::StatusReceived { status } => {
                    println!("status received: {status:?}");
                }
                Event::ContinuationRequestReceived {
                    continuation_request,
                } => {
                    println!("unexpected continuation request received: {continuation_request:?}");
                }
                event => {
                    println!("{event:?}");
                }
            },
        }
    }
}