Skip to content

Commit b5dd787

Browse files
committed
refactor: Refactor examples
1 parent 39118d3 commit b5dd787

File tree

8 files changed

+238
-330
lines changed

8 files changed

+238
-330
lines changed

imap-codec/examples/client.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
use imap_codec::{fragmentizer::Fragmentizer, GreetingCodec, ResponseCodec};
2+
3+
#[path = "common/common.rs"]
4+
mod common;
5+
6+
use common::read_more;
7+
8+
use crate::common::Role;
9+
10+
enum State {
11+
Greeting,
12+
Response,
13+
}
14+
15+
const WELCOME: &str = r#"# Parsing of IMAP greeting and responses
16+
17+
"S:" denotes the server.
18+
".." denotes the continuation of an (incomplete) response, e.g., due to the use of an IMAP literal.
19+
20+
Note: "\n" will be automatically replaced by "\r\n".
21+
22+
--------------------------------------------------------------------------------------------------
23+
24+
Enter intial IMAP greeting followed by IMAP responses (or "exit").
25+
"#;
26+
27+
fn main() {
28+
println!("{}", WELCOME);
29+
30+
let mut fragmentizer = Fragmentizer::new(10 * 1024);
31+
let mut state = State::Greeting;
32+
33+
loop {
34+
// Progress next fragment.
35+
let Some(_fragment_info) = fragmentizer.progress() else {
36+
// Read more bytes ...
37+
let bytes = read_more(Role::Server, fragmentizer.message_bytes().is_empty());
38+
39+
// ... and pass the bytes to the Fragmentizer ...
40+
fragmentizer.enqueue_bytes(&bytes);
41+
42+
// ... and try again.
43+
continue;
44+
};
45+
46+
// Check whether the Fragmentizer detected a complete message.
47+
if !fragmentizer.is_message_complete() {
48+
// Read next fragment.
49+
continue;
50+
}
51+
52+
// The Fragmentizer detected a complete message.
53+
match state {
54+
State::Greeting => {
55+
match fragmentizer.decode_message(&GreetingCodec::default()) {
56+
Ok(greeting) => {
57+
// Do something with the greeting ...
58+
println!("{:#?}", greeting);
59+
60+
// ... and proceed with reponses.
61+
state = State::Response;
62+
}
63+
Err(err) => {
64+
println!("Error parsing greeting: {err:?}");
65+
}
66+
};
67+
}
68+
State::Response => {
69+
match fragmentizer.decode_message(&ResponseCodec::default()) {
70+
Ok(response) => {
71+
// Do something with the response.
72+
println!("{:#?}", response);
73+
}
74+
Err(err) => {
75+
println!("Error parsing response: {err:?}");
76+
}
77+
};
78+
}
79+
};
80+
}
81+
}

imap-codec/examples/common/common.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ pub enum Role {
1212
Server,
1313
}
1414

15-
pub fn read_more(buffer: &mut Vec<u8>, role: Role) {
16-
let prompt = if buffer.is_empty() {
15+
pub fn read_more(role: Role, message_begin: bool) -> Vec<u8> {
16+
let prompt = if message_begin {
1717
match role {
1818
Role::Client => "C: ",
1919
Role::Server => "S: ",
@@ -30,7 +30,7 @@ pub fn read_more(buffer: &mut Vec<u8>, role: Role) {
3030
std::process::exit(0);
3131
}
3232

33-
buffer.extend_from_slice(line.as_bytes());
33+
line.into_bytes()
3434
}
3535

3636
fn read_line(prompt: &str, role: Role) -> String {

imap-codec/examples/fragmentizer_client.rs

Lines changed: 0 additions & 61 deletions
This file was deleted.

imap-codec/examples/fragmentizer_server.rs

Lines changed: 0 additions & 80 deletions
This file was deleted.

imap-codec/examples/parse_command.rs

Lines changed: 0 additions & 65 deletions
This file was deleted.

imap-codec/examples/parse_greeting.rs

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)