No support for non-UTF8 parsing #5
Description
It is (unfortunately) not entirely uncommon for IMAP servers to not support UTF-8 (see mattnenterprise/rust-imap#54 for more), but imap-proto
currently returns &str
all over the place, which necessarily implies that it only works on UTF-8 response. I think the right thing to do here (although it makes the types a bit less pleasant) is to parameterize all the types by a T: TryFrom<&[u8]>
. That way, users can choose whether to parse into str
, or simply keep things as &[u8]
(or do some other decoding). Unfortunately, TryFrom
is still nightly-only, and hasn't even landed yet for strings (rust-lang/rust#44916), so in the meantime, you'd probably just want to add a trait like this:
pub trait FromByteResponse<'a> {
fn from_bytes(&'a [u8]) -> Self;
}
impl<'a> FromByteResponse<'a> for &'a str {
fn from_bytes(b: &'a [u8]) -> Self {
use std;
std::str::from_utf8(b).unwrap()
}
}
impl<'a> FromByteResponse<'a> for &'a [u8] {
fn from_bytes(b: &'a [u8]) -> Self {
b
}
}
And then make the various types use this trait. For example, for MailboxDatum
:
pub enum MailboxDatum<T: for<'a> FromByteResponse<'a>> {
Exists(u32),
Flags(Vec<T>),
List(Vec<T>, T, T),
Recent(u32),
}