yore 1.4.0

Rust library for decoding/encoding character sets according to OEM code pages
Documentation
use alloc::borrow::Cow;
use alloc::vec::Vec;

use crate::EncodeError;

pub trait Encoder {
    fn encode_grapheme(&self, bytes: &mut &[u8]) -> Option<u8>;
    #[doc(hidden)]
    #[inline(always)]
    fn encode_helper<'a>(
        &self,
        s: &'a str,
        fallback: Option<u8>,
    ) -> Result<Cow<'a, [u8]>, EncodeError> {
        let mut src = s.as_bytes();
        if s.is_ascii() {
            return Ok(src.into());
        }
        let len = s.chars().count();
        let mut res = Vec::with_capacity(len);

        // extend uses iterator size hint to skip per-element capacity checks
        res.extend((0..len).filter_map(|_| self.encode_grapheme(&mut src).or(fallback)));

        // If any encoding failed, we got fewer bytes than expected
        if res.len() != len {
            return Err(EncodeError {});
        }

        Ok(res.into())
    }
}