Struct rfc2047_decoder::Decoder
source · pub struct Decoder {
pub too_long_encoded_word: RecoverStrategy,
}
Expand description
Represents the decoder builder.
§Example
use rfc2047_decoder::{Decoder, RecoverStrategy};
let decoder = Decoder::new()
.too_long_encoded_word_strategy(RecoverStrategy::Skip);
let decoded_str = decoder.decode("=?UTF-8?B?c3Ry?=").unwrap();
assert_eq!(decoded_str, "str");
Fields§
§too_long_encoded_word: RecoverStrategy
Determines which strategy should be used, if the parser encounters encoded words which are longer than allowed in the RFC (it’s longer than 75 chars).
Implementations§
source§impl Decoder
impl Decoder
sourcepub fn new() -> Self
pub fn new() -> Self
Equals Decoder::default.
Examples found in repository?
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
fn main() {
let encoded_str = "=?UTF-8?Q?str?=";
let decoded_str = "str";
// using the decode helper (default options)
assert_eq!(
rfc2047_decoder::decode(encoded_str.as_bytes()).unwrap(),
decoded_str
);
// using the decoder builder (custom options)
assert_eq!(
rfc2047_decoder::Decoder::new()
.too_long_encoded_word_strategy(rfc2047_decoder::RecoverStrategy::Skip)
.decode(encoded_str.as_bytes())
.unwrap(),
decoded_str
);
}
sourcepub fn too_long_encoded_word_strategy(self, strategy: RecoverStrategy) -> Self
pub fn too_long_encoded_word_strategy(self, strategy: RecoverStrategy) -> Self
Set the strategy if the decoder finds an encoded word which is too long.
§Examples
Each example uses the same encoded message:
=?utf-8?B?TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gVXQgaW50ZXJkdW0gcXVhbSBldSBmYWNpbGlzaXMgb3JuYXJlLg==?=
which exceeds the maximum length of 75 chars so it’s actually invalid.
§RecoverStrategy::Skip
Skips the invalid encoded word and parses it as clear text.
use rfc2047_decoder::{Decoder, RecoverStrategy};
let message = "=?utf-8?B?TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gVXQgaW50ZXJkdW0gcXVhbSBldSBmYWNpbGlzaXMgb3JuYXJlLg==?=";
let decoder = Decoder::new()
.too_long_encoded_word_strategy(RecoverStrategy::Skip);
let parsed = decoder.decode(message).unwrap();
// nothing changed!
assert_eq!(parsed, message);
§RecoverStrategy::Decode
Although the encoded word is invalid, keep decoding it.
use rfc2047_decoder::{Decoder, RecoverStrategy};
let message = "=?utf-8?B?TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gVXQgaW50ZXJkdW0gcXVhbSBldSBmYWNpbGlzaXMgb3JuYXJlLg==?=";
let decoder = Decoder::new()
.too_long_encoded_word_strategy(RecoverStrategy::Decode);
let parsed = decoder.decode(message).unwrap();
// could you decode it? ;)
let expected_result = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut interdum quam eu facilisis ornare.";
assert_eq!(parsed, expected_result);
§RecoverStrategy::Abort (default)
The parser will return an Err
and collects all encoded words which are
too long. You can use them afterwards for error messages for example.
use rfc2047_decoder::{Decoder, RecoverStrategy, Error::{self, Lexer}};
use rfc2047_decoder::LexerError::ParseEncodedWordTooLongError;
use rfc2047_decoder::TooLongEncodedWords;
let message = "=?utf-8?B?TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gVXQgaW50ZXJkdW0gcXVhbSBldSBmYWNpbGlzaXMgb3JuYXJlLg==?=";
// `RecoverStrategy::Abort` is the default strategy
let decoder = Decoder::new();
let parsed = decoder.decode(message);
assert_eq!(parsed, Err(Lexer(ParseEncodedWordTooLongError(TooLongEncodedWords(vec!["=?utf-8?B?TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gVXQgaW50ZXJkdW0gcXVhbSBldSBmYWNpbGlzaXMgb3JuYXJlLg==?=".to_string()])))));
Examples found in repository?
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
fn main() {
let encoded_str = "=?UTF-8?Q?str?=";
let decoded_str = "str";
// using the decode helper (default options)
assert_eq!(
rfc2047_decoder::decode(encoded_str.as_bytes()).unwrap(),
decoded_str
);
// using the decoder builder (custom options)
assert_eq!(
rfc2047_decoder::Decoder::new()
.too_long_encoded_word_strategy(rfc2047_decoder::RecoverStrategy::Skip)
.decode(encoded_str.as_bytes())
.unwrap(),
decoded_str
);
}
sourcepub fn decode<T: AsRef<[u8]>>(self, encoded_str: T) -> Result<String, Error>
pub fn decode<T: AsRef<[u8]>>(self, encoded_str: T) -> Result<String, Error>
Decodes the given RFC 2047 MIME Message Header encoded string.
Examples found in repository?
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
fn main() {
let encoded_str = "=?UTF-8?Q?str?=";
let decoded_str = "str";
// using the decode helper (default options)
assert_eq!(
rfc2047_decoder::decode(encoded_str.as_bytes()).unwrap(),
decoded_str
);
// using the decoder builder (custom options)
assert_eq!(
rfc2047_decoder::Decoder::new()
.too_long_encoded_word_strategy(rfc2047_decoder::RecoverStrategy::Skip)
.decode(encoded_str.as_bytes())
.unwrap(),
decoded_str
);
}
Trait Implementations§
source§impl Default for Decoder
impl Default for Decoder
source§fn default() -> Self
fn default() -> Self
Returns the decoder with the following default “settings”:
too_long_encoded_word
: RecoverStrategy::Abort