returns the remaining bytes

This commit is contained in:
Quentin 2023-07-24 18:10:44 +02:00
parent 4a46faa670
commit 606d3f7494
Signed by: quentin
GPG key ID: E9602264D639FF68
5 changed files with 20 additions and 20 deletions

View file

@ -18,7 +18,7 @@ Content-Type: text/plain; charset=us-ascii
This is the plain text body of the message. Note the blank line This is the plain text body of the message. Note the blank line
between the header information and the body of the message."#; between the header information and the body of the message."#;
let email = eml_codec::email(input).unwrap(); let (_, email) = eml_codec::email(input).unwrap();
println!( println!(
"{} just sent you an email with subject \"{}\"", "{} just sent you an email with subject \"{}\"",
email.imf.from[0].to_string(), email.imf.from[0].to_string(),

View file

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

View file

@ -1,6 +0,0 @@
/// Errors triggered when parsing email
#[derive(Debug, PartialEq)]
pub enum EMLError<'a> {
ParseError(nom::Err<nom::error::Error<&'a [u8]>>),
}

View file

@ -15,8 +15,7 @@ pub mod header;
/// Low-level email-specific text-based representation for data /// Low-level email-specific text-based representation for data
pub mod text; pub mod text;
/// Error management use nom::IResult;
pub mod error;
/// Parse a whole email including its (MIME) body /// Parse a whole email including its (MIME) body
/// ///
@ -28,6 +27,12 @@ pub mod error;
/// ///
/// * `input` - A buffer of bytes containing your full email /// * `input` - A buffer of bytes containing your full email
/// ///
/// # Returns
///
/// * `rest` - The rest of the buffer, the part that is not parsed as the email ended before the
/// end of the data
/// * `msg` - The parsed message
///
/// # Examples /// # Examples
/// ///
/// ``` /// ```
@ -41,17 +46,15 @@ pub mod error;
/// This is the plain text body of the message. Note the blank line /// This is the plain text body of the message. Note the blank line
/// between the header information and the body of the message."#; /// between the header information and the body of the message."#;
/// ///
/// let email = eml_codec::email(input).unwrap(); /// let (_, email) = eml_codec::email(input).unwrap();
/// println!( /// println!(
/// "{} raw message is:\n{}", /// "{} raw message is:\n{}",
/// email.imf.from[0].to_string(), /// email.imf.from[0].to_string(),
/// String::from_utf8_lossy(email.child.as_text().unwrap().body), /// String::from_utf8_lossy(email.child.as_text().unwrap().body),
/// ); /// );
/// ``` /// ```
pub fn email(input: &[u8]) -> Result<part::composite::Message, error::EMLError> { pub fn email(input: &[u8]) -> IResult<&[u8], part::composite::Message> {
part::composite::message(mime::Message::default())(input) part::composite::message(mime::Message::default())(input)
.map(|(_, v)| v)
.map_err(error::EMLError::ParseError)
} }
/// Only extract the headers of the email that are part of the Internet Message Format spec /// Only extract the headers of the email that are part of the Internet Message Format spec
@ -66,6 +69,11 @@ pub fn email(input: &[u8]) -> Result<part::composite::Message, error::EMLError>
/// * `input` - A buffer of bytes containing either only the headers of your email or your full /// * `input` - A buffer of bytes containing either only the headers of your email or your full
/// email (in both cases, the body will be ignored) /// email (in both cases, the body will be ignored)
/// ///
/// # Returns
///
/// * `rest` - The rest of the buffer, ie. the body of your email as raw bytes
/// * `imf` - The parsed IMF headers of your email
///
/// # Examples /// # Examples
/// ///
/// ``` /// ```
@ -79,15 +87,13 @@ pub fn email(input: &[u8]) -> Result<part::composite::Message, error::EMLError>
/// This is the plain text body of the message. Note the blank line /// This is the plain text body of the message. Note the blank line
/// between the header information and the body of the message."#; /// between the header information and the body of the message."#;
/// ///
/// let header = eml_codec::imf(input).unwrap(); /// let (_, header) = eml_codec::imf(input).unwrap();
/// println!( /// println!(
/// "{} just sent you an email with subject \"{}\"", /// "{} just sent you an email with subject \"{}\"",
/// header.from[0].to_string(), /// header.from[0].to_string(),
/// header.subject.unwrap().to_string(), /// header.subject.unwrap().to_string(),
/// ); /// );
/// ``` /// ```
pub fn imf(input: &[u8]) -> Result<imf::Imf, error::EMLError> { pub fn imf(input: &[u8]) -> IResult<&[u8], imf::Imf> {
imf::field::imf(input) imf::field::imf(input)
.map(|(_, v)| v)
.map_err(error::EMLError::ParseError)
} }

View file

@ -8,7 +8,7 @@ fn main() {
let mut rawmail = Vec::new(); let mut rawmail = Vec::new();
io::stdin().lock().read_to_end(&mut rawmail).unwrap(); io::stdin().lock().read_to_end(&mut rawmail).unwrap();
let eml = eml_codec::email(&rawmail).unwrap(); let (_, eml) = eml_codec::email(&rawmail).unwrap();
println!("{:#?}", eml); println!("{:#?}", eml);
assert!(eml.imf.date.is_some()); assert!(eml.imf.date.is_some());
assert!(!eml.imf.from.is_empty()); assert!(!eml.imf.from.is_empty());