mikedilger / email-format

Email data structure and builder for streaming emails
Apache License 2.0
17 stars 5 forks source link

Header strategy - Enum, Trait objects, etc #2

Closed mikedilger closed 7 years ago

mikedilger commented 8 years ago

I tried doing trait objects. It did not turn out well. The trait wants a "stream_out" function which must be type parameterized. However, trait objects in rust must not have such functions for object safety reasons.

Currently the 'dev' branch code uses fields, one per header type, but this is tedious.

I'm leaning towards enums...

pub enum Header {
  OrigDate(DateTime<FixedOffset>),
  To(String),
  // ...etc
}
impl Header {
  pub fn name(&self) -> &'static str {
    match *self {
      OrigDate(_) => "Date",
      To(_) => "To",
      // ...etc
    }
  }
  pub fn multiple(&self) -> bool {
    // ...
  }
}

pub struct Email {
  single_headers: HashMap<&'static str, Header>,
  multiple_headers: HashMap<&'static str, Vec<Header>>,
  // ...etc
}
mikedilger commented 8 years ago

BTW: the ORDER of headers matters, so the above is not quite correct.

mikedilger commented 7 years ago

I think the rfc5322::Message takes care of all of this. We will write functions that make it easier for users to get and set various types of headers.