[core] Tests: the mandatory suites, red against the API stubs (55 tests)
This commit is contained in:
+87
@@ -0,0 +1,87 @@
|
||||
//! Parsing a raw RFC 5322 message (mail-parser) into what the app stores and shows.
|
||||
|
||||
use crate::Address;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Larger messages are refused before parsing (Proton's own limit is 25 MB of attachments).
|
||||
pub const MAX_MESSAGE_BYTES: usize = 64 * 1024 * 1024;
|
||||
/// A subject longer than this is cut (characters).
|
||||
pub const MAX_SUBJECT_CHARS: usize = 998;
|
||||
/// At most this many addresses per field are kept.
|
||||
pub const MAX_ADDRESSES: usize = 500;
|
||||
/// At most this many attachments are kept; the rest are counted in `dropped_attachments`.
|
||||
pub const MAX_ATTACHMENTS: usize = 500;
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
|
||||
pub struct Parsed {
|
||||
pub message_id: Option<String>,
|
||||
pub in_reply_to: Vec<String>,
|
||||
pub references: Vec<String>,
|
||||
pub subject: String,
|
||||
pub from: Vec<Address>,
|
||||
pub to: Vec<Address>,
|
||||
pub cc: Vec<Address>,
|
||||
pub reply_to: Vec<Address>,
|
||||
/// Seconds since the Unix epoch, from the Date header.
|
||||
pub date: Option<i64>,
|
||||
/// The Date header as RFC 822 text, for "On <date>, <name> wrote:".
|
||||
pub date_text: Option<String>,
|
||||
/// Line endings are `\n`.
|
||||
pub text: Option<String>,
|
||||
pub html: Option<String>,
|
||||
pub attachments: Vec<Attachment>,
|
||||
pub dropped_attachments: usize,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct Attachment {
|
||||
/// Already safe for a file system (`safe_filename`).
|
||||
pub filename: String,
|
||||
pub content_type: String,
|
||||
/// Without the angle brackets.
|
||||
pub content_id: Option<String>,
|
||||
pub inline: bool,
|
||||
pub data: Vec<u8>,
|
||||
}
|
||||
|
||||
/// Bodies and attachments are personal data: `Debug` prints sizes only.
|
||||
impl std::fmt::Debug for Parsed {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("Parsed")
|
||||
.field("text_len", &self.text.as_ref().map(String::len))
|
||||
.field("html_len", &self.html.as_ref().map(String::len))
|
||||
.field("attachments", &self.attachments.len())
|
||||
.finish_non_exhaustive()
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for Attachment {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("Attachment")
|
||||
.field("bytes", &self.data.len())
|
||||
.field("inline", &self.inline)
|
||||
.finish_non_exhaustive()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum ParseError {
|
||||
TooLarge,
|
||||
NotMail,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for ParseError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
ParseError::TooLarge => write!(f, "message larger than {MAX_MESSAGE_BYTES} bytes"),
|
||||
ParseError::NotMail => f.write_str("not a mail message"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for ParseError {}
|
||||
|
||||
pub fn parse(raw: &[u8]) -> Result<Parsed, ParseError> {
|
||||
let _ = raw;
|
||||
todo!()
|
||||
}
|
||||
Reference in New Issue
Block a user