Skip to content
This repository was archived by the owner on Jul 22, 2019. It is now read-only.
This repository was archived by the owner on Jul 22, 2019. It is now read-only.

No support for non-UTF8 parsing #5

Open
@jonhoo

Description

@jonhoo

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),
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions