improve example

This commit is contained in:
Quentin 2023-07-24 14:05:47 +02:00
parent 40ab7e33b6
commit b330745e43
Signed by: quentin
GPG key ID: E9602264D639FF68
3 changed files with 38 additions and 2 deletions

View file

@ -37,6 +37,7 @@ This library does not aim at implementing a specific RFC, but to be a swiss-army
- Maintainability - modifying the code does not create regression and is possible for someone exterior to the project.
- Compatibility - always try to parse something, do not panic or return an error.
- Exhaustivity - serve as a common project to encode knowledge about emails (existing mime types, existing headers, etc.).
- Type safe - do not manipulate only strings/bytes but leverage Rust type system instead so you benefit of its safety checks at compile time.
[See more about this library goals in the doc/ folder](./doc/goals.md)

View file

@ -9,10 +9,19 @@ Content-Type: text/plain; charset=us-ascii
This is the plain text body of the message. Note the blank line
between the header information and the body of the message."#;
let email = eml_codec::email(input).unwrap();
// if you are only interested in email metadata/headers
let header = eml_codec::imf(input).unwrap();
println!(
"{} just sent you an email with subject \"{}\"",
header.from[0].to_string(),
header.subject.unwrap().to_string(),
);
// if you like to also parse the body/content
let email = eml_codec::email(input).unwrap();
println!(
"{} raw message is:\n{}",
email.imf.from[0].to_string(),
email.imf.subject.unwrap().to_string(),
String::from_utf8_lossy(email.child.as_text().unwrap().body),
);
}

View file

@ -29,6 +29,32 @@ pub enum AnyPart<'a> {
Txt(Text<'a>),
Bin(Binary<'a>),
}
impl<'a> AnyPart<'a> {
pub fn as_multipart(&self) -> Option<&Multipart<'a>> {
match self {
Self::Mult(x) => Some(x),
_ => None,
}
}
pub fn as_message(&self) -> Option<&Message<'a>> {
match self {
Self::Msg(x) => Some(x),
_ => None,
}
}
pub fn as_text(&self) -> Option<&Text<'a>> {
match self {
Self::Txt(x) => Some(x),
_ => None,
}
}
pub fn as_binary(&self) -> Option<&Binary<'a>> {
match self {
Self::Bin(x) => Some(x),
_ => None,
}
}
}
pub fn to_anypart<'a>(m: AnyMIME<'a>, rpart: &'a [u8]) -> AnyPart<'a> {
match m {