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.
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()])))));
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
impl Eq for Decoder
impl StructuralPartialEq for Decoder
Auto Trait Implementations§
impl Freeze for Decoder
impl RefUnwindSafe for Decoder
impl Send for Decoder
impl Sync for Decoder
impl Unpin for Decoder
impl UnwindSafe for Decoder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more