diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2019-12-23 12:51:54 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2019-12-23 12:51:54 +0100 |
commit | 31193c2bef934d26265673c91cbea0b64a53cd15 (patch) | |
tree | 105163b13ed6fcb43e655f233679cc5947cf0b7b /internals/src/bind | |
parent | 71ea078d4eb5d857680a69b01f7427ea43e2f5a2 (diff) |
Run cargo-fmt on codebasecargo-fmt
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
Diffstat (limited to 'internals/src/bind')
-rw-r--r-- | internals/src/bind/base64.rs | 112 | ||||
-rw-r--r-- | internals/src/bind/encoded_word/impls.rs | 77 | ||||
-rw-r--r-- | internals/src/bind/encoded_word/mod.rs | 47 | ||||
-rw-r--r-- | internals/src/bind/idna.rs | 44 | ||||
-rw-r--r-- | internals/src/bind/mime.rs | 27 | ||||
-rw-r--r-- | internals/src/bind/mod.rs | 6 | ||||
-rw-r--r-- | internals/src/bind/quoted_printable.rs | 143 | ||||
-rw-r--r-- | internals/src/bind/quoted_string.rs | 23 |
8 files changed, 218 insertions, 261 deletions
diff --git a/internals/src/bind/base64.rs b/internals/src/bind/base64.rs index 36bb735..26ac8f7 100644 --- a/internals/src/bind/base64.rs +++ b/internals/src/bind/base64.rs @@ -1,9 +1,9 @@ -use {base64 as extern_base64}; -use soft_ascii_string::{ SoftAsciiString, SoftAsciiChar}; +use base64 as extern_base64; use failure::Fail; +use soft_ascii_string::{SoftAsciiChar, SoftAsciiString}; -use ::utils::is_utf8_continuation_byte; -use ::error::{EncodingError, EncodingErrorKind}; +use error::{EncodingError, EncodingErrorKind}; +use utils::is_utf8_continuation_byte; use super::encoded_word::EncodedWordWriter; @@ -15,24 +15,28 @@ const USE_PADDING: bool = true; const ECW_STRIP_WHITESPACE: bool = false; const NON_ECW_STRIP_WHITESPACE: bool = true; - #[inline] pub fn normal_encode<R: AsRef<[u8]>>(input: R) -> SoftAsciiString { - let res = extern_base64::encode_config( input.as_ref(), extern_base64::Config::new( - //FIXME: check if line wrap should be used here, I thinks it should - CHARSET, USE_PADDING, NON_ECW_STRIP_WHITESPACE, LINE_WRAP - )); + let res = extern_base64::encode_config( + input.as_ref(), + extern_base64::Config::new( + //FIXME: check if line wrap should be used here, I thinks it should + CHARSET, + USE_PADDING, + NON_ECW_STRIP_WHITESPACE, + LINE_WRAP, + ), + ); SoftAsciiString::from_unchecked(res) } #[inline] pub fn normal_decode<R: AsRef<[u8]>>(input: R) -> Result<Vec<u8>, EncodingError> { - extern_base64::decode_config( input.as_ref(), extern_base64::Config::new( - CHARSET, USE_PADDING, NON_ECW_STRIP_WHITESPACE, LINE_WRAP - )).map_err(|err| err - .context(EncodingErrorKind::Malformed) - .into() + extern_base64::decode_config( + input.as_ref(), + extern_base64::Config::new(CHARSET, USE_PADDING, NON_ECW_STRIP_WHITESPACE, LINE_WRAP), ) + .map_err(|err| err.context(EncodingErrorKind::Malformed).into()) } #[inline(always)] @@ -49,20 +53,24 @@ fn calc_max_input_len(max_output_len: usize) -> usize { /// for now this only supports utf8/ascii input, as /// we have to know where we can split #[inline(always)] -pub fn encoded_word_encode<O, R: AsRef<str>>( input: R, out: &mut O ) - where O: EncodedWordWriter +pub fn encoded_word_encode<O, R: AsRef<str>>(input: R, out: &mut O) +where + O: EncodedWordWriter, { _encoded_word_encode(input.as_ref(), out) } -fn _encoded_word_encode<O>( input: &str, out: &mut O ) - where O: EncodedWordWriter +fn _encoded_word_encode<O>(input: &str, out: &mut O) +where + O: EncodedWordWriter, { - let config = extern_base64::Config::new( - CHARSET, USE_PADDING, ECW_STRIP_WHITESPACE, NO_LINE_WRAP - ); + let config = + extern_base64::Config::new(CHARSET, USE_PADDING, ECW_STRIP_WHITESPACE, NO_LINE_WRAP); - debug_assert!( USE_PADDING == true, "size calculation is tailored for padding"); + debug_assert!( + USE_PADDING == true, + "size calculation is tailored for padding" + ); let max_output_len = out.max_payload_len(); let max_input_len = calc_max_input_len(max_output_len); @@ -105,7 +113,7 @@ fn _encoded_word_encode<O>( input: &str, out: &mut O ) } if rest.len() == 0 { - break + break; } else { out.start_next_encoded_word(); } @@ -114,38 +122,37 @@ fn _encoded_word_encode<O>( input: &str, out: &mut O ) } #[inline(always)] -pub fn encoded_word_decode<R: AsRef<[u8]>>(input: R) - -> Result<Vec<u8>, EncodingError> -{ - extern_base64::decode_config(input.as_ref(), extern_base64::Config::new( - CHARSET, USE_PADDING, ECW_STRIP_WHITESPACE, NO_LINE_WRAP - )).map_err(|err| err - .context(EncodingErrorKind::Malformed) - .into() +pub fn encoded_word_decode<R: AsRef<[u8]>>(input: R) -> Result<Vec<u8>, EncodingError> { + extern_base64::decode_config( + input.as_ref(), + extern_base64::Config::new(CHARSET, USE_PADDING, ECW_STRIP_WHITESPACE, NO_LINE_WRAP), ) + .map_err(|err| err.context(EncodingErrorKind::Malformed).into()) } - - - #[cfg(test)] mod test { - use soft_ascii_string::SoftAsciiStr; - use bind::encoded_word::{VecWriter, EncodedWordEncoding}; use super::*; + use bind::encoded_word::{EncodedWordEncoding, VecWriter}; + use soft_ascii_string::SoftAsciiStr; #[test] fn encoding_uses_line_wrap() { let input = concat!( - "0123456789", "0123456789", - "0123456789", "0123456789", - "0123456789", "0123456789", + "0123456789", + "0123456789", + "0123456789", + "0123456789", + "0123456789", + "0123456789", ); let res = normal_encode(input); - assert_eq!(res.as_str(), - "MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nz\r\ng5"); + assert_eq!( + res.as_str(), + "MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nz\r\ng5" + ); let dec = normal_decode(res).unwrap(); @@ -164,26 +171,16 @@ mod test { #[test] fn encode_decode_normal() { - let pairs: &[(&str,&[u8])] = &[ + let pairs: &[(&str, &[u8])] = &[ ( "this is some\r\nlong\r\ntest.", - b"dGhpcyBpcyBzb21lDQpsb25nDQp0ZXN0Lg==" + b"dGhpcyBpcyBzb21lDQpsb25nDQp0ZXN0Lg==", ), - ( - "", - b"" - ) + ("", b""), ]; for &(raw, encoded) in pairs.iter() { - assert_eq!( - normal_encode(raw).as_bytes(), - encoded - ); - assert_eq!( - assert_ok!(normal_decode(encoded)), - raw.as_bytes() - ) - + assert_eq!(normal_encode(raw).as_bytes(), encoded); + assert_eq!(assert_ok!(normal_decode(encoded)), raw.as_bytes()) } } @@ -251,7 +248,6 @@ mod test { ] } - #[test] fn decode_encoded_word() { assert_eq!( @@ -259,4 +255,4 @@ mod test { b"this_crazy_encoded_word" ); } -}
\ No newline at end of file +} diff --git a/internals/src/bind/encoded_word/impls.rs b/internals/src/bind/encoded_word/impls.rs index f7051da..af2582c 100644 --- a/internals/src/bind/encoded_word/impls.rs +++ b/internals/src/bind/encoded_word/impls.rs @@ -1,23 +1,27 @@ -use soft_ascii_string::{ SoftAsciiString, SoftAsciiChar, SoftAsciiStr }; +use soft_ascii_string::{SoftAsciiChar, SoftAsciiStr, SoftAsciiString}; +use super::{EncodedWordEncoding as Encoding, EncodedWordWriter}; +use encoder::EncodingWriter; +use grammar::encoded_word::{ECW_SEP_OVERHEAD, MAX_ECW_LEN}; use vec1::Vec1; -use grammar::encoded_word::{ MAX_ECW_LEN, ECW_SEP_OVERHEAD }; -use ::encoder::EncodingWriter; -use super::{ EncodedWordWriter, EncodedWordEncoding as Encoding }; pub struct VecWriter<'a> { - data: Vec1<SoftAsciiString >, + data: Vec1<SoftAsciiString>, charset: &'a SoftAsciiStr, - encoding: Encoding + encoding: Encoding, } impl<'a> VecWriter<'a> { pub fn new(charset: &'a SoftAsciiStr, encoding: Encoding) -> Self { - let data = Vec1::new( SoftAsciiString::new() ); - VecWriter { data, charset, encoding } + let data = Vec1::new(SoftAsciiString::new()); + VecWriter { + data, + charset, + encoding, + } } - pub fn data( &self ) -> &[SoftAsciiString] { + pub fn data(&self) -> &[SoftAsciiString] { &*self.data } } @@ -29,70 +33,71 @@ impl<'a> Into<Vec1<SoftAsciiString>> for VecWriter<'a> { } impl<'a> EncodedWordWriter for VecWriter<'a> { - - fn encoding( &self ) -> Encoding { + fn encoding(&self) -> Encoding { self.encoding } - fn write_char( &mut self, ch: SoftAsciiChar ) { - self.data.last_mut().push( ch ); + fn write_char(&mut self, ch: SoftAsciiChar) { + self.data.last_mut().push(ch); } - fn write_charset( &mut self ) { - self.data.last_mut().extend( self.charset.chars() ) + fn write_charset(&mut self) { + self.data.last_mut().extend(self.charset.chars()) } - fn write_ecw_seperator( &mut self ) { - self.data.push( SoftAsciiString::new() ) + fn write_ecw_seperator(&mut self) { + self.data.push(SoftAsciiString::new()) } - fn max_payload_len( &self ) -> usize { + fn max_payload_len(&self) -> usize { MAX_ECW_LEN - ECW_SEP_OVERHEAD - self.charset.len() - 1 } } -pub struct WriterWrapper<'a, 'b: 'a>{ +pub struct WriterWrapper<'a, 'b: 'a> { charset: &'a SoftAsciiStr, encoding: Encoding, - encoder_handle: &'a mut EncodingWriter<'b> + encoder_handle: &'a mut EncodingWriter<'b>, } impl<'a, 'b: 'a> WriterWrapper<'a, 'b> { - pub fn new_with_charset(charset: &'a SoftAsciiStr, - encoding: Encoding, - encoder: &'a mut EncodingWriter<'b> ) -> Self - { - WriterWrapper { charset, encoding, encoder_handle: encoder } + pub fn new_with_charset( + charset: &'a SoftAsciiStr, + encoding: Encoding, + encoder: &'a mut EncodingWriter<'b>, + ) -> Self { + WriterWrapper { + charset, + encoding, + encoder_handle: encoder, + } } - pub fn new(encoding: Encoding, - encoder: &'a mut EncodingWriter<'b> ) -> Self - { + pub fn new(encoding: Encoding, encoder: &'a mut EncodingWriter<'b>) -> Self { Self::new_with_charset(SoftAsciiStr::from_unchecked("utf8"), encoding, encoder) } } impl<'a, 'b: 'a> EncodedWordWriter for WriterWrapper<'a, 'b> { - - fn encoding( &self ) -> Encoding { + fn encoding(&self) -> Encoding { self.encoding } - fn write_charset( &mut self ) { + fn write_charset(&mut self) { //TODO fix - let _ = self.encoder_handle.write_str( self.charset ); + let _ = self.encoder_handle.write_str(self.charset); } - fn write_ecw_seperator( &mut self ) { + fn write_ecw_seperator(&mut self) { self.encoder_handle.write_fws(); } - fn write_char( &mut self, ch: SoftAsciiChar ) { + fn write_char(&mut self, ch: SoftAsciiChar) { //TODO fix - let _ = self.encoder_handle.write_char( ch ); + let _ = self.encoder_handle.write_char(ch); } - fn max_payload_len( &self ) -> usize { + fn max_payload_len(&self) -> usize { MAX_ECW_LEN - ECW_SEP_OVERHEAD - self.charset.len() - 1 } } diff --git a/internals/src/bind/encoded_word/mod.rs b/internals/src/bind/encoded_word/mod.rs index ef61fc6..6e68505 100644 --- a/internals/src/bind/encoded_word/mod.rs +++ b/internals/src/bind/encoded_word/mod.rs @@ -1,4 +1,4 @@ -use soft_ascii_string::{ SoftAsciiStr, SoftAsciiChar }; +use soft_ascii_string::{SoftAsciiChar, SoftAsciiStr}; use super::{base64, quoted_printable}; @@ -7,18 +7,18 @@ pub use self::impls::*; #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)] pub enum EncodedWordEncoding { - Base64, QuotedPrintable + Base64, + QuotedPrintable, } impl EncodedWordEncoding { - /// returns the acronym for the given encoding /// used in a encoded word pub fn acronym(&self) -> &'static SoftAsciiStr { use self::EncodedWordEncoding::*; match *self { Base64 => SoftAsciiStr::from_unchecked("B"), - QuotedPrintable => SoftAsciiStr::from_unchecked("Q") + QuotedPrintable => SoftAsciiStr::from_unchecked("Q"), } } @@ -32,26 +32,24 @@ impl EncodedWordEncoding { /// only encoding utf8 is supported for now /// pub fn encode<R, O>(&self, input: R, out: &mut O) - where R: AsRef<str>, O: EncodedWordWriter + where + R: AsRef<str>, + O: EncodedWordWriter, { use self::EncodedWordEncoding::*; let input: &str = input.as_ref(); match *self { - Base64 => { - base64::encoded_word_encode(input, out) - }, - QuotedPrintable => { - quoted_printable::encoded_word_encode_utf8(input, out) - } + Base64 => base64::encoded_word_encode(input, out), + QuotedPrintable => quoted_printable::encoded_word_encode_utf8(input, out), } } } pub trait EncodedWordWriter { - fn write_char( &mut self, ch: SoftAsciiChar ); - fn write_charset( &mut self ); - fn encoding( &self ) -> EncodedWordEncoding; - fn write_ecw_seperator( &mut self ); + fn write_char(&mut self, ch: SoftAsciiChar); + fn write_charset(&mut self); + fn encoding(&self) -> EncodedWordEncoding; + fn write_ecw_seperator(&mut self); /// Returns the maximal length of the paylod/encoded data /// @@ -59,34 +57,33 @@ pub trait EncodedWordWriter { /// should never be able to change the returned value. /// Only changing e.g. the charset or encoding should be /// able to change what `max_paylod_len` returns. - fn max_payload_len( &self ) -> usize; + fn max_payload_len(&self) -> usize; - fn write_ecw_start( &mut self ) { + fn write_ecw_start(&mut self) { let qm = SoftAsciiChar::from_unchecked('?'); self.write_char(SoftAsciiChar::from_unchecked('=')); self.write_char(qm); self.write_charset(); self.write_char(qm); let acronym = self.encoding().acronym(); - self.write_str( acronym ); + self.write_str(acronym); self.write_char(qm); } - fn write_ecw_end( &mut self ) { - self.write_char( SoftAsciiChar::from_unchecked('?') ); - self.write_char( SoftAsciiChar::from_unchecked('=') ); + fn write_ecw_end(&mut self) { + self.write_char(SoftAsciiChar::from_unchecked('?')); + self.write_char(SoftAsciiChar::from_unchecked('=')); } - - fn start_next_encoded_word( &mut self ) { + fn start_next_encoded_word(&mut self) { self.write_ecw_end(); self.write_ecw_seperator(); self.write_ecw_start(); } - fn write_str( &mut self, s: &SoftAsciiStr ) { + fn write_str(&mut self, s: &SoftAsciiStr) { for ch in s.chars() { self.write_char(ch) } } -}
\ No newline at end of file +} diff --git a/internals/src/bind/idna.rs b/internals/src/bind/idna.rs index 7b82d69..be11a71 100644 --- a/internals/src/bind/idna.rs +++ b/internals/src/bind/idna.rs @@ -1,8 +1,7 @@ -use soft_ascii_string::SoftAsciiString; use idna; +use soft_ascii_string::SoftAsciiString; -use ::error::{EncodingError, EncodingErrorKind}; - +use error::{EncodingError, EncodingErrorKind}; /// uses puny code on given domain to return a ascii representation /// @@ -16,58 +15,45 @@ use ::error::{EncodingError, EncodingErrorKind}; /// if you puny code the domain `"this seems\0so;wrong"` it /// will return `Ok("this seems\0so;wrong")` /// -pub fn puny_code_domain<R: AsRef<str>>(domain: R) - -> Result<SoftAsciiString, EncodingError> -{ +pub fn puny_code_domain<R: AsRef<str>>(domain: R) -> Result<SoftAsciiString, EncodingError> { _puny_code_domain(domain.as_ref()) } -fn _puny_code_domain(domain: &str) - -> Result<SoftAsciiString, EncodingError> -{ +fn _puny_code_domain(domain: &str) -> Result<SoftAsciiString, EncodingError> { match idna::domain_to_ascii(domain) { Ok(asciified) => { //SAFE: well we converted it to ascii, so it's ascii Ok(SoftAsciiString::from_unchecked(asciified)) - }, - Err(_non_informative_err) => { - Err(EncodingErrorKind::NotEncodable { encoding: "punycode" }.into()) } + Err(_non_informative_err) => Err(EncodingErrorKind::NotEncodable { + encoding: "punycode", + } + .into()), } } - #[cfg(test)] mod test { - use idna; use super::puny_code_domain; + use idna; #[test] fn idna_does_not_validate() { let domain = "this seems\0so;wrong"; - assert_eq!( - domain.to_owned(), - assert_ok!( idna::domain_to_ascii(domain) ) - ); + assert_eq!(domain.to_owned(), assert_ok!(idna::domain_to_ascii(domain))); } #[test] fn nop_puny_code() { let domain = "is_ascii.notadomain"; - let encoded = assert_ok!( puny_code_domain( domain ) ); - assert_eq!( - &*encoded, - "is_ascii.notadomain" - ); + let encoded = assert_ok!(puny_code_domain(domain)); + assert_eq!(&*encoded, "is_ascii.notadomain"); } #[test] fn puny_code_ascii_mail() { let domain = "nöt_ascii.ü"; - let encoded = assert_ok!( puny_code_domain(domain) ); - assert_eq!( - &*encoded, - "xn--nt_ascii-n4a.xn--tda" - ); + let encoded = assert_ok!(puny_code_domain(domain)); + assert_eq!(&*encoded, "xn--nt_ascii-n4a.xn--tda"); } -}
\ No newline at end of file +} diff --git a/internals/src/bind/mime.rs b/internals/src/bind/mime.rs index afede94..c2ac73f 100644 --- a/internals/src/bind/mime.rs +++ b/internals/src/bind/mime.rs @@ -1,11 +1,8 @@ use std::borrow::Cow; -use soft_ascii_string::{ SoftAsciiStr, SoftAsciiString}; use grammar::is_token_char; -use percent_encoding::{ - EncodeSet, - percent_encode -}; +use percent_encoding::{percent_encode, EncodeSet}; +use soft_ascii_string::{SoftAsciiStr, SoftAsciiString}; #[derive(Debug, Eq, PartialEq, Clone, Copy, Hash)] struct MimeParamEncodingSet; @@ -17,28 +14,28 @@ impl EncodeSet for MimeParamEncodingSet { } } - /// percent encodes a byte sequence so that it can be used /// in a RFC 2231 conform encoded mime header parameter pub fn percent_encode_param_value<'a, R>(input: &'a R) -> Cow<'a, SoftAsciiStr> - where R: ?Sized+AsRef<[u8]> +where + R: ?Sized + AsRef<[u8]>, { let cow: Cow<'a, str> = percent_encode(input.as_ref(), MimeParamEncodingSet).into(); match cow { Cow::Owned(o) => - //SAFE: MimeParamEncodingSet makes all non-us-ascii bytes encoded AND - // percent_encoding::percent_encode always only produces ascii anyway - Cow::Owned(SoftAsciiString::from_unchecked(o)), - Cow::Borrowed(b) => - Cow::Borrowed(SoftAsciiStr::from_unchecked(b)) + //SAFE: MimeParamEncodingSet makes all non-us-ascii bytes encoded AND + // percent_encoding::percent_encode always only produces ascii anyway + { + Cow::Owned(SoftAsciiString::from_unchecked(o)) + } + Cow::Borrowed(b) => Cow::Borrowed(SoftAsciiStr::from_unchecked(b)), } } - #[cfg(test)] mod test { - use std::borrow::Cow; use super::*; + use std::borrow::Cow; #[test] fn encode_simple() { @@ -53,4 +50,4 @@ mod test { let res = percent_encode_param_value(input); assert_eq!(res, Cow::Borrowed(input)); } -}
\ No newline at end of file +} diff --git a/internals/src/bind/mod.rs b/internals/src/bind/mod.rs index 6b475ee..f482afe 100644 --- a/internals/src/bind/mod.rs +++ b/internals/src/bind/mod.rs @@ -1,8 +1,8 @@ //! This module contains bindings to a number of external crates. -pub mod encoded_word; pub mod base64; -pub mod quoted_string; -pub mod quoted_printable; +pub mod encoded_word; pub mod idna; pub mod mime; +pub mod quoted_printable; +pub mod quoted_string; diff --git a/internals/src/bind/quoted_printable.rs b/internals/src/bind/quoted_printable.rs index 7ef6291..7540419 100644 --- a/internals/src/bind/quoted_printable.rs +++ b/internals/src/bind/quoted_printable.rs @@ -1,9 +1,9 @@ -use soft_ascii_string::{ SoftAsciiChar, SoftAsciiString }; -use { quoted_printable as extern_quoted_printable }; +use quoted_printable as extern_quoted_printable; +use soft_ascii_string::{SoftAsciiChar, SoftAsciiString}; -use failure::Fail; -use ::error::{EncodingError, EncodingErrorKind}; use super::encoded_word::EncodedWordWriter; +use error::{EncodingError, EncodingErrorKind}; +use failure::Fail; /// a quoted printable encoding suitable for content transfer encoding, /// but _not_ suited for the encoding in encoded words @@ -14,24 +14,18 @@ pub fn normal_encode<A: AsRef<[u8]>>(data: A) -> SoftAsciiString { /// a quoted printable decoding suitable for content transfer encoding #[inline] -pub fn normal_decode<R: AsRef<[u8]>>(input: R) - -> Result<Vec<u8>, EncodingError> -{ +pub fn normal_decode<R: AsRef<[u8]>>(input: R) -> Result<Vec<u8>, EncodingError> { //extern_quoted_printable h - extern_quoted_printable::decode( - input.as_ref(), extern_quoted_printable::ParseMode::Strict - ).map_err(|err| err - .context(EncodingErrorKind::Malformed) - .into() - ) + extern_quoted_printable::decode(input.as_ref(), extern_quoted_printable::ParseMode::Strict) + .map_err(|err| err.context(EncodingErrorKind::Malformed).into()) } /// a quoted printable decoding suitable for decoding a quoted printable /// encpded text in encoded words #[inline(always)] -pub fn encoded_word_decode<R: AsRef<[u8]>>( input: R ) -> Result<Vec<u8>, EncodingError> { +pub fn encoded_word_decode<R: AsRef<[u8]>>(input: R) -> Result<Vec<u8>, EncodingError> { //we can just use the stadard decoding - normal_decode( input ) + normal_decode(input) } //FIXME we don't use EncodedWord context here, @@ -39,13 +33,14 @@ pub fn encoded_word_decode<R: AsRef<[u8]>>( input: R ) -> Result<Vec<u8>, Encodi // making it compatilble with all context, but not nessesary // the best solution... /// Simple wrapper around ecoded_word_encode for utf8 strings only -pub fn encoded_word_encode_utf8<'a, O>(word: &str, writer: &mut O ) - where O: EncodedWordWriter +pub fn encoded_word_encode_utf8<'a, O>(word: &str, writer: &mut O) +where + O: EncodedWordWriter, { - let iter = word.char_indices().map( |(idx, ch)| { - &word.as_bytes()[idx..idx+ch.len_utf8()] - }); - encoded_word_encode(iter, writer ); + let iter = word + .char_indices() + .map(|(idx, ch)| &word.as_bytes()[idx..idx + ch.len_utf8()]); + encoded_word_encode(iter, writer); } /// @@ -100,8 +95,10 @@ pub fn encoded_word_encode_utf8<'a, O>(word: &str, writer: &mut O ) /// might be encoded with completely different bytes, but when the RFC speaks of /// '\r','\n' it normally means the bytes 10/13 independent of the character set, /// or if they appear in a image, zip-archiev etc. ) -pub fn encoded_word_encode<'a, I, O>(input: I, out: &mut O ) - where I: Iterator<Item=&'a [u8]>, O: EncodedWordWriter +pub fn encoded_word_encode<'a, I, O>(input: I, out: &mut O) +where + I: Iterator<Item = &'a [u8]>, + O: EncodedWordWriter, { out.write_ecw_start(); let max_payload_len = out.max_payload_len(); @@ -118,19 +115,22 @@ pub fn encoded_word_encode<'a, I, O>(input: I, out: &mut O ) // this is the way to go as long as we don't want to behave differently for // different context, the COMMENT context allows more chars, and the // TEXT context even more - b'!' | b'*' | - b'+' | b'-' | - b'/' | b'_' | - b'0'...b'9' | - b'A'...b'Z' | - b'a'...b'z' => { + b'!' + | b'*' + | b'+' + | b'-' + | b'/' + | b'_' + | b'0'...b'9' + | b'A'...b'Z' + | b'a'...b'z' => { buf[buf_idx] = SoftAsciiChar::from_unchecked(byte as char); buf_idx += 1; - }, + } _otherwise => { buf[buf_idx] = SoftAsciiChar::from_unchecked('='); - buf[buf_idx+1] = lower_nibble_to_hex( byte >> 4 ); - buf[buf_idx+2] = lower_nibble_to_hex( byte ); + buf[buf_idx + 1] = lower_nibble_to_hex(byte >> 4); + buf[buf_idx + 2] = lower_nibble_to_hex(byte); buf_idx += 3; } } @@ -140,10 +140,13 @@ pub fn encoded_word_encode<'a, I, O>(input: I, out: &mut O ) remaining = max_payload_len; } if buf_idx > remaining { - panic!( "single character longer then max length ({:?}) of encoded word", remaining ); + panic!( + "single character longer then max length ({:?}) of encoded word", + remaining + ); } for idx in 0..buf_idx { - out.write_char( buf[idx] ) + out.write_char(buf[idx]) } remaining -= buf_idx; } @@ -151,39 +154,33 @@ pub fn encoded_word_encode<'a, I, O>(input: I, out: &mut O ) } #[inline] -fn lower_nibble_to_hex( half_byte: u8 ) -> SoftAsciiChar { +fn lower_nibble_to_hex(half_byte: u8) -> SoftAsciiChar { static CHARS: &[char] = &[ - '0', '1', '2', '3', '4', '5', - '6', '7', '8', '9', 'A', 'B', - 'C', 'D', 'E', 'F' + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', ]; - SoftAsciiChar::from_unchecked(CHARS[ (half_byte & 0x0F) as usize ]) + SoftAsciiChar::from_unchecked(CHARS[(half_byte & 0x0F) as usize]) } - - - #[cfg(test)] mod test { - use soft_ascii_string::SoftAsciiStr; - use ::bind::encoded_word::EncodedWordEncoding; use super::super::encoded_word::VecWriter; use super::*; + use bind::encoded_word::EncodedWordEncoding; + use soft_ascii_string::SoftAsciiStr; #[test] fn to_hex() { let data = &[ ('0', 0b11110000), - ('0', 0b0 ), + ('0', 0b0), ('7', 0b0111), ('7', 0b10111), - ('F', 0b1111) + ('F', 0b1111), ]; for &(ch, byte) in data { - assert_eq!( lower_nibble_to_hex( byte), ch ); + assert_eq!(lower_nibble_to_hex(byte), ch); } - } macro_rules! test_ecw_encode { @@ -241,7 +238,7 @@ mod test { ] } - test_ecw_encode!{ encode_ascii, + test_ecw_encode! { encode_ascii, data "abcdefghijklmnopqrstuvwxyz \t?=0123456789!@#$%^&*()_+-" => [ "=?utf8?Q?abcdefghijklmnopqrstuvwxyz=20=09=3F=3D0123456789!=40=23=24=25=5E?=", "=?utf8?Q?=26*=28=29_+-?=" @@ -254,7 +251,6 @@ mod test { ] } - test_ecw_encode! { split_into_multiple_ecws, data "0123456789012345678901234567890123456789012345678901234567891234newline" => [ "=?utf8?Q?0123456789012345678901234567890123456789012345678901234567891234?=", @@ -262,7 +258,7 @@ mod test { ] } - test_ecw_encode!{ bigger_chunks, + test_ecw_encode! { bigger_chunks, data "ランダムテキスト ראַנדאָם טעקסט" => [ //ランダムテキス "=?utf8?Q?=E3=83=A9=E3=83=B3=E3=83=80=E3=83=A0=E3=83=86=E3=82=AD=E3=82=B9?=", @@ -279,44 +275,29 @@ mod test { ("=28=29=22", "()\""), ( "=7B=7D=7E=40=23=24=25=5E=26*=28=29=3D=7C=5C=5B=5D=27=3B=3A=2E", - "{}~@#$%^&*()=|\\[]';:." - ), - ( - "=3F=3D=20=09=0D=0A", - "?= \t\r\n" - ), - ( - "=26*=28=29_+-", - "&*()_+-" + "{}~@#$%^&*()=|\\[]';:.", ), + ("=3F=3D=20=09=0D=0A", "?= \t\r\n"), + ("=26*=28=29_+-", "&*()_+-"), ( "abcdefghijklmnopqrstuvwxyz=20=09=3F=3D0123456789!=40=23=24=25=5E", - "abcdefghijklmnopqrstuvwxyz \t?=0123456789!@#$%^" - ), - ( - "=0D=0A", - "\r\n" + "abcdefghijklmnopqrstuvwxyz \t?=0123456789!@#$%^", ), + ("=0D=0A", "\r\n"), ( "=E3=83=A9=E3=83=B3=E3=83=80=E3=83=A0=E3=83=86=E3=82=AD=E3=82=B9", - "ランダムテキス" + "ランダムテキス", ), ( "=E3=83=88=20=D7=A8=D7=90=D6=B7=D7=A0=D7=93=D7=90=D6=B8=D7=9D=20", - "ト ראַנדאָם " + "ト ראַנדאָם ", ), - ( - "=D7=98=D7=A2=D7=A7=D7=A1=D7=98", - "טעקסט" - ) + ("=D7=98=D7=A2=D7=A7=D7=A1=D7=98", "טעקסט"), ]; for &(inp, outp) in pairs.iter() { let dec = assert_ok!(encoded_word_decode(inp)); let dec = String::from_utf8(dec).unwrap(); - assert_eq!( - outp.as_bytes(), - dec.as_bytes() - ); + assert_eq!(outp.as_bytes(), dec.as_bytes()); } } @@ -342,11 +323,11 @@ mod test { #[test] fn normal_decode_text() { let text = concat!( - "This is a llllllllllllllllllllllllllllllllllllll00000000000000000000ng test=\r\n", - " 0123456789qwertyuio\r\n", - "With many lines\r\n", - "And utf=E2=86=92=E2=86=92=E2=86=92=E2=86=928" - ); + "This is a llllllllllllllllllllllllllllllllllllll00000000000000000000ng test=\r\n", + " 0123456789qwertyuio\r\n", + "With many lines\r\n", + "And utf=E2=86=92=E2=86=92=E2=86=92=E2=86=928" + ); let encoded = String::from_utf8(normal_decode(text).unwrap()).unwrap(); assert_eq!( concat!( @@ -357,4 +338,4 @@ mod test { encoded.as_str() ); } -}
\ No newline at end of file +} diff --git a/internals/src/bind/quoted_string.rs b/internals/src/bind/quoted_string.rs index 2a5c3ab..12cb456 100644 --- a/internals/src/bind/quoted_string.rs +++ b/internals/src/bind/quoted_string.rs @@ -1,11 +1,7 @@ -use quoted_string::spec::{ - GeneralQSSpec, - PartialCodePoint, - WithoutQuotingValidator -}; +use quoted_string::spec::{GeneralQSSpec, PartialCodePoint, WithoutQuotingValidator}; use media_type_impl_utils::quoted_string; -use ::MailType; +use MailType; /// A Quoted String specification in context of Mail ([rfc5322](https://tools.ietf.org/html/rfc5322#section-2.2.3)) /// @@ -21,7 +17,6 @@ impl GeneralQSSpec for MailQsSpec { type Parsing = quoted_string::MimeParsing; } - /// A Quoted String specification in context of Mail ([rfc5322](https://tools.ietf.org/html/rfc5322#section-2.2.3)) /// /// This implementation of MailQsSpec _does not_ include support for the obsolete parts of the grammar @@ -37,10 +32,9 @@ impl GeneralQSSpec for InternationalizedMailQsSpec { pub use self::quoted_string::MimeTokenValidator as UnquotedTokenValidator; - #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] pub struct UnquotedATextValidator { - mail_type: MailType + mail_type: MailType, } impl UnquotedATextValidator { @@ -51,20 +45,22 @@ impl UnquotedATextValidator { impl WithoutQuotingValidator for UnquotedATextValidator { fn next(&mut self, pcp: PartialCodePoint) -> bool { - is_atext(pcp, self.mail_type) + is_atext(pcp, self.mail_type) } } - #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] pub struct UnquotedDotAtomTextValidator { mail_type: MailType, - allow_dot: bool + allow_dot: bool, } impl UnquotedDotAtomTextValidator { pub fn new(mail_type: MailType) -> Self { - UnquotedDotAtomTextValidator { mail_type, allow_dot: false } + UnquotedDotAtomTextValidator { + mail_type, + allow_dot: false, + } } } @@ -88,7 +84,6 @@ impl WithoutQuotingValidator for UnquotedDotAtomTextValidator { } } - |