From d0e6c54498605303f9a5ab4f72ef65ef66ab3cea Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Fri, 30 Mar 2018 19:12:31 -0700 Subject: [PATCH] Delete all the old hex stuff --- src/hex.rs | 124 ----------------------------------------------------- 1 file changed, 124 deletions(-) diff --git a/src/hex.rs b/src/hex.rs index c27aa71..af1caf7 100644 --- a/src/hex.rs +++ b/src/hex.rs @@ -2,130 +2,6 @@ use std::char; use std::iter; use std::str::Chars; -pub enum HexResult { - Byte(u8), - Invalid(char), -} - -pub struct HexBytes<'a> { - input: Chars<'a>, -} - -impl<'a> HexBytes<'a> { - fn new(input: &'a str) -> HexBytes { - HexBytes { input: input.chars() } - } - - pub fn valid(self) -> ValidHexBytes<'a> { - ValidHexBytes::new(self) - } -} - -impl<'a> iter::Iterator for HexBytes<'a> { - type Item = HexResult; - - fn next(&mut self) -> Option { - if let Some(c) = self.input.next() { - if let Some(hex) = c.to_digit(16) { - Some(HexResult::Byte(hex as u8)) - } else { - Some(HexResult::Invalid(c)) - } - } else { - None - } - } -} - -/* - * ValidHexBytes -- - */ - -pub struct ValidHexBytes<'a> { - input: HexBytes<'a>, -} - -impl<'a> ValidHexBytes<'a> { - fn new(input: HexBytes) -> ValidHexBytes { - ValidHexBytes { input: input } - } -} - -impl<'a> iter::Iterator for ValidHexBytes<'a> { - type Item = u8; - - fn next(&mut self) -> Option { - loop { - match self.input.next() { - Some(hex_c) => match hex_c { - HexResult::Byte(c) => return Some(c), - _ => continue, - }, - None => return None, - } - } - } -} - -/* - * HexDigest -- - */ - -pub trait HexDigest { - fn hex_digest(self) -> String; -} - -impl<'a> HexDigest for HexBytes<'a> { - fn hex_digest(self) -> String { - self.valid().hex_digest() - } -} - -impl<'a> HexDigest for ValidHexBytes<'a> { - fn hex_digest(self) -> String { - self.map(|x| char::from_digit(x as u32, 16).unwrap()).collect() - } -} - -impl HexDigest for Vec { - fn hex_digest(self) -> String { - self.into_iter().map(|x| char::from_digit(x as u32, 16).unwrap()).collect() - } -} - -impl<'a> HexDigest for &'a [u8] { - fn hex_digest(self) -> String { - self.into_iter().map(|x| char::from_digit(*x as u32, 16).unwrap()).collect() - } -} - -/* - * AsHexbytes -- - */ - -pub trait AsHexBytes<'a> { - fn hex_bytes(&self) -> HexBytes<'a>; -} - -impl<'a> AsHexBytes<'a> for &'a str { - fn hex_bytes(&self) -> HexBytes<'a> { HexBytes::new(self) } -} - -//pub trait HexDecodable { -// fn hex_decoded(self) -> Option>; -//} -// -//impl<'a> HexDecodable for &'a str { -// fn hex_decoded(self) -> Option> { -// fn valid(ch: &&str) -> bool { ch.chars().all(|c| c.is_digit(16)) } -// fn decode(ch: &str) -> u8 { ch.chars().fold(0u8, |acc, c| (acc << 4) + c.to_digit(16).unwrap() as u8) } -// self.splitn(2, "") -// .take_while(valid) -// .map(decode) -// .collect::>() -// } -//} - pub trait HexDecodable { fn hex_decoded(self) -> HexDecoder; }