Skip to main content

Module parse

Module parse 

Source
Expand description

JMAP Email/parse coroutine (RFC 8621 §4.11): parses RFC 5322 message blobs that are not yet stored as Email objects (useful for attached .eml files).

§Example

use std::{
    io::{Read, Write},
    net::TcpStream,
};

use io_jmap::{
    coroutine::{JmapCoroutine, JmapCoroutineState, JmapYield},
    rfc8620::session::JmapSession,
    rfc8621::email::parse::{JmapEmailParse, JmapEmailParseOptions},
};
use secrecy::SecretString;

// Ready stream needed (TCP-connected, TLS-negociated)
let mut stream = TcpStream::connect("api.example.com:443").unwrap();
let mut buf = [0u8; 4096];

let session: JmapSession = serde_json::from_str(r#"{
    "username": "",
    "accounts": {},
    "primaryAccounts": {"urn:ietf:params:jmap:mail": "a1"},
    "capabilities": {},
    "apiUrl": "https://api.example.com/jmap/",
    "downloadUrl": "",
    "uploadUrl": "",
    "eventSourceUrl": "",
    "state": ""
}"#).unwrap();
let auth = SecretString::from("Bearer xyz");
let mut coroutine = JmapEmailParse::new(
    &session,
    &auth,
    vec!["b1".into()],
    JmapEmailParseOptions::default(),
)
.unwrap();
let mut arg = None;

let out = loop {
    match coroutine.resume(arg.take()) {
        JmapCoroutineState::Yielded(JmapYield::WantsWrite(bytes)) => {
            stream.write_all(&bytes).unwrap();
        }
        JmapCoroutineState::Yielded(JmapYield::WantsRead) => {
            let n = stream.read(&mut buf).unwrap();
            arg = Some(&buf[..n]);
        }
        JmapCoroutineState::Complete(Ok(out)) => break out,
        JmapCoroutineState::Complete(Err(err)) => panic!("{err}"),
    }
};

println!("{} parsed", out.parsed.len());

Structs§

JmapEmailParse
I/O-free coroutine for the JMAP Email/parse method.
JmapEmailParseOptions
Options for JmapEmailParse::new.
JmapEmailParseOutput
Successful terminal output of JmapEmailParse.

Enums§

JmapEmailParseError
Failure causes during a JMAP Email/parse flow.