diff --git a/README.md b/README.md index c2ca75f..0c68e2d 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ 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(); +let (_, email) = eml_codec::email(input).unwrap(); println!( "{} just sent you an email with subject \"{}\"", email.imf.from[0].to_string(), diff --git a/examples/simple.rs b/examples/simple.rs index ca15fc3..38c7b78 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -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."#; // if you are only interested in email metadata/headers - let header = eml_codec::imf(input).unwrap(); + let (_, header) = eml_codec::imf(input).unwrap(); println!( "{} just sent you an email with subject \"{}\"", 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 - let email = eml_codec::email(input).unwrap(); + let (_, email) = eml_codec::email(input).unwrap(); println!( "{} raw message is:\n{}", email.imf.from[0].to_string(), diff --git a/src/error.rs b/src/error.rs deleted file mode 100644 index 127c45a..0000000 --- a/src/error.rs +++ /dev/null @@ -1,6 +0,0 @@ -/// Errors triggered when parsing email - -#[derive(Debug, PartialEq)] -pub enum EMLError<'a> { - ParseError(nom::Err>), -} diff --git a/src/lib.rs b/src/lib.rs index 2f5383f..41cf312 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,8 +15,7 @@ pub mod header; /// Low-level email-specific text-based representation for data pub mod text; -/// Error management -pub mod error; +use nom::IResult; /// Parse a whole email including its (MIME) body /// @@ -28,6 +27,12 @@ pub mod error; /// /// * `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 /// /// ``` @@ -41,17 +46,15 @@ pub mod error; /// 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(); +/// let (_, email) = eml_codec::email(input).unwrap(); /// println!( /// "{} raw message is:\n{}", /// email.imf.from[0].to_string(), /// String::from_utf8_lossy(email.child.as_text().unwrap().body), /// ); /// ``` -pub fn email(input: &[u8]) -> Result { +pub fn email(input: &[u8]) -> IResult<&[u8], part::composite::Message> { 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 @@ -66,6 +69,11 @@ pub fn email(input: &[u8]) -> Result /// * `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) /// +/// # 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 /// /// ``` @@ -79,15 +87,13 @@ pub fn email(input: &[u8]) -> Result /// This is the plain text body of the message. Note the blank line /// 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!( /// "{} just sent you an email with subject \"{}\"", /// header.from[0].to_string(), /// header.subject.unwrap().to_string(), /// ); /// ``` -pub fn imf(input: &[u8]) -> Result { +pub fn imf(input: &[u8]) -> IResult<&[u8], imf::Imf> { imf::field::imf(input) - .map(|(_, v)| v) - .map_err(error::EMLError::ParseError) } diff --git a/src/parse.rs b/src/parse.rs index 821f0d6..78756f6 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -8,7 +8,7 @@ fn main() { let mut rawmail = Vec::new(); 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); assert!(eml.imf.date.is_some()); assert!(!eml.imf.from.is_empty());