From 76933c6d1a7d0116acb72f065b4a85b165a01ff2 Mon Sep 17 00:00:00 2001 From: Richard Whitehouse Date: Mon, 22 Oct 2018 09:00:37 -0400 Subject: [PATCH] Move chartype into a separate module --- src/codec/chartype.rs | 61 +++++++++++++++++++++++++++++++ src/{codec.rs => codec/mod.rs} | 66 ++-------------------------------- 2 files changed, 64 insertions(+), 63 deletions(-) create mode 100644 src/codec/chartype.rs rename src/{codec.rs => codec/mod.rs} (88%) diff --git a/src/codec/chartype.rs b/src/codec/chartype.rs new file mode 100644 index 0000000..61953f8 --- /dev/null +++ b/src/codec/chartype.rs @@ -0,0 +1,61 @@ +const SPACE: u8 = b' '; +const TAB: u8 = b'\t'; +pub const LF: u8 = b'\n'; +pub const CR: u8 = b'\r'; + +#[derive(PartialEq, Debug)] +pub enum CharType { + Line, + Whitespace, + Other, +} + +pub fn chartype(char: Option<&u8>) -> CharType { + match char { + None | Some(&LF) | Some(&CR) => CharType::Line, + Some(&SPACE) | Some(&TAB) => CharType::Whitespace, + Some(_) => CharType::Other, + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_chartype_none() { + assert_eq!(chartype(None), CharType::Line); + } + + #[test] + fn test_chartype_cr() { + assert_eq!(chartype(Some(&LF)), CharType::Line); + } + + #[test] + fn test_chartype_lf() { + assert_eq!(chartype(Some(&CR)), CharType::Line); + } + + #[test] + fn test_chartype_space() { + assert_eq!(chartype(Some(&SPACE)), CharType::Whitespace); + } + + #[test] + fn test_chartype_tab() { + assert_eq!(chartype(Some(&TAB)), CharType::Whitespace); + } + + #[test] + fn test_chartype_alpha() { + const A: u8 = b'a'; + assert_eq!(chartype(Some(&A)), CharType::Other); + } + + #[test] + fn test_chartype_number() { + const ONE: u8 = b'1'; + assert_eq!(chartype(Some(&ONE)), CharType::Other); + } +} diff --git a/src/codec.rs b/src/codec/mod.rs similarity index 88% rename from src/codec.rs rename to src/codec/mod.rs index b1d298c..63804bb 100644 --- a/src/codec.rs +++ b/src/codec/mod.rs @@ -4,15 +4,13 @@ use std; use std::io; use tokio_io::codec::{Decoder, Encoder}; +mod chartype; + +use self::chartype::{chartype, CharType, CR, LF}; use parser::header; use parser::top_line; use types::{Header, Method, RequestLine, StatusLine, TopLine}; -const SPACE: u8 = b' '; -const TAB: u8 = b'\t'; -const LF: u8 = b'\n'; -const CR: u8 = b'\r'; - impl Header { fn parse(line: UnparsedLine) -> Option<(Header, UnparsedLine)> { match header(line.as_bytes()) { @@ -331,61 +329,3 @@ impl Encoder for SipCodec { Ok(()) } } - -#[derive(PartialEq, Debug)] -enum CharType { - Line, - Whitespace, - Other, -} - -fn chartype(char: Option<&u8>) -> CharType { - match char { - None | Some(&LF) | Some(&CR) => CharType::Line, - Some(&SPACE) | Some(&TAB) => CharType::Whitespace, - Some(_) => CharType::Other, - } -} - -#[cfg(test)] -mod tests { - - use super::*; - - #[test] - fn test_chartype_none() { - assert_eq!(chartype(None), CharType::Line); - } - - #[test] - fn test_chartype_cr() { - assert_eq!(chartype(Some(&LF)), CharType::Line); - } - - #[test] - fn test_chartype_lf() { - assert_eq!(chartype(Some(&CR)), CharType::Line); - } - - #[test] - fn test_chartype_space() { - assert_eq!(chartype(Some(&SPACE)), CharType::Whitespace); - } - - #[test] - fn test_chartype_tab() { - assert_eq!(chartype(Some(&TAB)), CharType::Whitespace); - } - - #[test] - fn test_chartype_alpha() { - const A: u8 = b'a'; - assert_eq!(chartype(Some(&A)), CharType::Other); - } - - #[test] - fn test_chartype_number() { - const ONE: u8 = b'1'; - assert_eq!(chartype(Some(&ONE)), CharType::Other); - } -} -- 2.34.1