From 68138ab850f3020898db90680b1511a028e9e5f5 Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Thu, 16 Jan 2025 19:03:30 -0500 Subject: [PATCH] Code cleanup and refactor to match interface of b64 and b32 better --- src/decode.rs | 55 +++-------------------------------------------- src/encode.rs | 59 ++++++++------------------------------------------- src/error.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 6 ++++-- 4 files changed, 73 insertions(+), 104 deletions(-) create mode 100644 src/error.rs diff --git a/src/decode.rs b/src/decode.rs index 54cda4c..b2cb1c7 100644 --- a/src/decode.rs +++ b/src/decode.rs @@ -1,57 +1,8 @@ -use std::{ - fmt, - io::{self, ErrorKind, Read, Write}, - num::TryFromIntError, +use { + crate::Error, + std::io::{ErrorKind, Read, Write}, }; -#[derive(Debug)] -/// Errors which might possibly occur while decoding base64 encoded data -pub enum Error { - Io(io::Error), - IllegalChar(char), - IntError(TryFromIntError), - MissingChar, -} - -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::Io(e) => write!(f, "{e}"), - Self::IllegalChar(c) => write!(f, "Illegal Character ({c})"), - Self::IntError(e) => write!(f, "Int Error: {e}"), - Self::MissingChar => write!(f, "Missing character"), - } - } -} - -impl From for Error { - fn from(value: io::Error) -> Self { - Self::Io(value) - } -} - -impl From for Error { - fn from(value: char) -> Self { - Self::IllegalChar(value) - } -} - -impl From for Error { - fn from(value: TryFromIntError) -> Self { - Self::IntError(value) - } -} - -impl std::error::Error for Error { - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { - match self { - Self::Io(e) => Some(e), - Self::IntError(e) => Some(e), - _ => None, - } - } -} - pub struct Decoder { reader: R, writer: W, diff --git a/src/encode.rs b/src/encode.rs index 3f0985e..1902a3e 100644 --- a/src/encode.rs +++ b/src/encode.rs @@ -1,47 +1,8 @@ use { - super::B16_ALPHABET, - std::{ - fmt::{self, Write}, - io::{self, ErrorKind, Read}, - }, + crate::{Error, B16_ALPHABET}, + std::io::{ErrorKind, Read, Write}, }; -#[derive(Debug)] -pub enum Error { - Io(io::Error), - Fmt(fmt::Error), -} - -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::Io(e) => write!(f, "{e}"), - Self::Fmt(e) => write!(f, "{e}"), - } - } -} - -impl From for Error { - fn from(value: io::Error) -> Self { - Self::Io(value) - } -} - -impl From for Error { - fn from(value: fmt::Error) -> Self { - Self::Fmt(value) - } -} - -impl std::error::Error for Error { - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { - match self { - Self::Fmt(e) => Some(e), - Self::Io(e) => Some(e), - } - } -} - pub struct Encoder { reader: R, writer: W, @@ -81,10 +42,8 @@ impl Encoder { } Ok(()) } -} -impl Encoder { - pub fn output(self) -> String { + pub fn output(self) -> W { self.writer } } @@ -95,17 +54,17 @@ mod tests { #[test] fn encode() { - let mut encoder = Encoder::new("Hello, World".as_bytes(), String::new()); + let mut encoder = Encoder::new("Hello, World".as_bytes(), vec![]); encoder.encode().unwrap(); - assert_eq!(encoder.output(), "48656C6C6F2C20576F726C64"); + assert_eq!(encoder.output(), b"48656C6C6F2C20576F726C64"); encoder = Encoder { reader: "Hello, World!".as_bytes(), - writer: String::new(), + writer: vec![], }; encoder.encode().unwrap(); - assert_eq!(encoder.output(), "48656C6C6F2C20576F726C6421"); - encoder = Encoder::new("Hello, World!\n".as_bytes(), String::new()); + assert_eq!(encoder.output(), b"48656C6C6F2C20576F726C6421"); + encoder = Encoder::new("Hello, World!\n".as_bytes(), vec![]); encoder.encode().unwrap(); - assert_eq!(encoder.output(), "48656C6C6F2C20576F726C64210A"); + assert_eq!(encoder.output(), b"48656C6C6F2C20576F726C64210A"); } } diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..3bafaa1 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,57 @@ +use std::{fmt, io, num::TryFromIntError}; + +#[derive(Debug)] +pub enum Error { + Io(io::Error), + Fmt(fmt::Error), + IllegalChar(char), + IntError(TryFromIntError), + MissingChar, +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Io(e) => write!(f, "{e}"), + Self::Fmt(e) => write!(f, "{e}"), + Self::IllegalChar(c) => write!(f, "Illegal Character ({c})"), + Self::IntError(e) => write!(f, "Int Error: {e}"), + Self::MissingChar => write!(f, "Missing character"), + } + } +} + +impl From for Error { + fn from(value: io::Error) -> Self { + Self::Io(value) + } +} + +impl From for Error { + fn from(value: char) -> Self { + Self::IllegalChar(value) + } +} + +impl From for Error { + fn from(value: fmt::Error) -> Self { + Self::Fmt(value) + } +} + +impl From for Error { + fn from(value: TryFromIntError) -> Self { + Self::IntError(value) + } +} + +impl std::error::Error for Error { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + match self { + Self::Fmt(e) => Some(e), + Self::Io(e) => Some(e), + Self::IntError(e) => Some(e), + _ => None, + } + } +} diff --git a/src/lib.rs b/src/lib.rs index 5b562c3..9ed5576 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,8 @@ -pub mod decode; #[warn(clippy::all, clippy::pedantic)] -pub mod encode; +mod decode; +mod encode; +mod error; +pub use {decode::Decoder, encode::Encoder, error::Error}; pub static B16_ALPHABET: [char; 16] = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',