From 29172fb5f928d915bff5536e63549ef50d6b93eb Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Sun, 19 Jan 2025 19:04:18 -0500 Subject: [PATCH] Add wrapping and styling. Todo: test styling, make sure there is no double newline at the end of the output --- src/decode.rs | 4 +- src/encode.rs | 87 +++++++++++++++++++++++++++++++++++------- src/lib.rs | 6 +-- testdata/lorem.txt | 9 +++++ testdata/lorem_b16.txt | 61 +++++++++++++++++++++++++++++ 5 files changed, 149 insertions(+), 18 deletions(-) create mode 100644 testdata/lorem.txt create mode 100644 testdata/lorem_b16.txt diff --git a/src/decode.rs b/src/decode.rs index b2cb1c7..77d42f8 100644 --- a/src/decode.rs +++ b/src/decode.rs @@ -43,13 +43,13 @@ impl Decoder { _ => unreachable!(), } let mut c = char::from(in_buf[0]); - if let Some(idx) = super::get_idx(c.to_ascii_uppercase()) { + if let Some(idx) = super::get_idx(c.to_ascii_uppercase().try_into().unwrap()) { out_buf[0] |= (idx << 4) as u8; } else { return Err(Error::IllegalChar(c)); } c = char::from(in_buf[1]); - if let Some(idx) = super::get_idx(c.to_ascii_uppercase()) { + if let Some(idx) = super::get_idx(c.to_ascii_uppercase().try_into().unwrap()) { out_buf[0] |= idx as u8; } else { return Err(Error::IllegalChar(c)); diff --git a/src/encode.rs b/src/encode.rs index 1902a3e..d64faf1 100644 --- a/src/encode.rs +++ b/src/encode.rs @@ -3,25 +3,45 @@ use { std::io::{ErrorKind, Read, Write}, }; +pub enum Style { + Plain, + Spaces, + SpacesWithHex, +} + +impl Default for Style { + fn default() -> Self { + Self::Plain + } +} + pub struct Encoder { reader: R, writer: W, + style: Style, + wrap: Option, } impl Encoder { /// Creates a new encoder with the given reader and writer. If alphabet is /// `None` the encoder will use the default rfc4648 alphabet. - pub fn new(reader: R, writer: W) -> Self { - Self { reader, writer } + pub fn new(reader: R, writer: W, style: Option