Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions imap-codec/examples/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use imap_codec::{fragmentizer::Fragmentizer, GreetingCodec, ResponseCodec};

#[path = "common/common.rs"]
mod common;

use common::read_more;

use crate::common::Role;

enum State {
Greeting,
Response,
}

const WELCOME: &str = r#"# Parsing of IMAP greeting and responses

"S:" denotes the server.
".." denotes the continuation of an (incomplete) response, e.g., due to the use of an IMAP literal.

Note: "\n" will be automatically replaced by "\r\n".

--------------------------------------------------------------------------------------------------

Enter intial IMAP greeting followed by IMAP responses (or "exit").
"#;

fn main() {
println!("{}", WELCOME);

let mut fragmentizer = Fragmentizer::new(10 * 1024);
let mut state = State::Greeting;

loop {
// Progress next fragment.
let Some(_fragment_info) = fragmentizer.progress() else {
// Read more bytes ...
let bytes = read_more(Role::Server, fragmentizer.message_bytes().is_empty());

// ... and pass the bytes to the Fragmentizer ...
fragmentizer.enqueue_bytes(&bytes);

// ... and try again.
continue;
};

// Check whether the Fragmentizer detected a complete message.
if !fragmentizer.is_message_complete() {
// Read next fragment.
continue;
}

// The Fragmentizer detected a complete message.
match state {
State::Greeting => {
match fragmentizer.decode_message(&GreetingCodec::default()) {
Ok(greeting) => {
// Do something with the greeting ...
println!("{:#?}", greeting);

// ... and proceed with reponses.
state = State::Response;
}
Err(err) => {
println!("Error parsing greeting: {err:?}");
}
};
}
State::Response => {
match fragmentizer.decode_message(&ResponseCodec::default()) {
Ok(response) => {
// Do something with the response.
println!("{:#?}", response);
}
Err(err) => {
println!("Error parsing response: {err:?}");
}
};
}
};
}
}
6 changes: 3 additions & 3 deletions imap-codec/examples/common/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ pub enum Role {
Server,
}

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

buffer.extend_from_slice(line.as_bytes());
line.into_bytes()
}

fn read_line(prompt: &str, role: Role) -> String {
Expand Down
61 changes: 0 additions & 61 deletions imap-codec/examples/fragmentizer_client.rs

This file was deleted.

80 changes: 0 additions & 80 deletions imap-codec/examples/fragmentizer_server.rs

This file was deleted.

65 changes: 0 additions & 65 deletions imap-codec/examples/parse_command.rs

This file was deleted.

55 changes: 0 additions & 55 deletions imap-codec/examples/parse_greeting.rs

This file was deleted.

Loading