melib: Add parameters field in ContentType::Text

Intending to add the option to set the parameter format=flowed in the
next commits
jmap
Manos Pitsidianakis 2019-11-17 13:24:19 +02:00
parent 62f3d12253
commit 953c3aa9d0
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
3 changed files with 34 additions and 16 deletions

View File

@ -140,6 +140,7 @@ impl From<&[u8]> for MultipartType {
pub enum ContentType { pub enum ContentType {
Text { Text {
kind: Text, kind: Text,
parameters: Vec<(Vec<u8>, Vec<u8>)>,
charset: Charset, charset: Charset,
}, },
Multipart { Multipart {
@ -162,6 +163,7 @@ impl Default for ContentType {
fn default() -> Self { fn default() -> Self {
ContentType::Text { ContentType::Text {
kind: Text::Plain, kind: Text::Plain,
parameters: Vec::new(),
charset: Charset::UTF8, charset: Charset::UTF8,
} }
} }

View File

@ -131,10 +131,7 @@ impl AttachmentBuilder {
parts, parts,
}; };
} else if ct.eq_ignore_ascii_case(b"text") { } else if ct.eq_ignore_ascii_case(b"text") {
self.content_type = ContentType::Text { self.content_type = ContentType::default();
kind: Text::Plain,
charset: Charset::UTF8,
};
for (n, v) in params { for (n, v) in params {
if n.eq_ignore_ascii_case(b"charset") { if n.eq_ignore_ascii_case(b"charset") {
if let ContentType::Text { if let ContentType::Text {
@ -143,7 +140,13 @@ impl AttachmentBuilder {
{ {
*c = Charset::from(v); *c = Charset::from(v);
} }
break; }
if let ContentType::Text {
parameters: ref mut p,
..
} = self.content_type
{
p.push((n.to_vec(), v.to_vec()));
} }
} }
if cst.eq_ignore_ascii_case(b"html") { if cst.eq_ignore_ascii_case(b"html") {
@ -587,11 +590,28 @@ impl Attachment {
.chars(), .chars(),
); );
match &a.content_type { match &a.content_type {
ContentType::Text { kind: _, charset } => { ContentType::Text {
kind: _,
parameters,
charset,
} => {
ret.extend( ret.extend(
format!("Content-Type: {}; charset={}\n\n", a.content_type, charset) format!("Content-Type: {}; charset={}", a.content_type, charset).chars(),
.chars(),
); );
for (n, v) in parameters {
ret.push_str("; ");
ret.extend(String::from_utf8_lossy(n).chars());
ret.push_str("=");
if v.contains(&b' ') {
ret.push_str("\"");
}
ret.extend(String::from_utf8_lossy(v).chars());
if v.contains(&b' ') {
ret.push_str("\"");
}
}
ret.push_str("\n\n");
ret.extend(String::from_utf8_lossy(a.body()).chars()); ret.extend(String::from_utf8_lossy(a.body()).chars());
} }
ContentType::Multipart { ContentType::Multipart {

View File

@ -329,18 +329,14 @@ fn build_multipart(ret: &mut String, kind: MultipartType, parts: Vec<AttachmentB
ContentType::Text { ContentType::Text {
kind: crate::email::attachment_types::Text::Plain, kind: crate::email::attachment_types::Text::Plain,
charset: Charset::UTF8, charset: Charset::UTF8,
} => { parameters: ref v,
} if v.is_empty() => {
ret.push('\n'); ret.push('\n');
ret.push_str(&String::from_utf8_lossy(sub.raw())); ret.push_str(&String::from_utf8_lossy(sub.raw()));
ret.push('\n'); ret.push('\n');
} }
Text { Text { .. } => {
ref kind, ret.extend(sub.build().into_raw().chars());
charset: _,
} => {
ret.extend(format!("Content-Type: {}; charset=\"utf-8\"\n", kind).chars());
ret.push('\n');
ret.push_str(&String::from_utf8_lossy(sub.raw()));
ret.push('\n'); ret.push('\n');
} }
Multipart { Multipart {