This repository has been archived by the owner on Aug 23, 2018. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Some general work on things. Vagueness abounds!
- Static header names. - Little bit of docs/code updating. - Some tests too. Next I’m going to try switching to tendril. That’ll be fun.
- Loading branch information
1 parent
48d2c7c
commit 9940efc
Showing
2 changed files
with
179 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
//! Implementations of `Header` and `ToHeader` for various types. | ||
use std::str; | ||
use std::fmt; | ||
|
||
use super::{Header, ToHeader}; | ||
|
||
impl ToHeader for usize { | ||
fn parse(raw: &[u8]) -> Option<usize> { | ||
str::from_utf8(raw).ok().and_then(|s| s.parse().ok()) | ||
} | ||
} | ||
|
||
impl Header for usize { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "{}", *self) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::fmt; | ||
use headers::{Header, ToHeader, HeaderDisplayAdapter}; | ||
|
||
fn eq<H: Header + ToHeader + Eq + fmt::Debug>(raw: &[u8], typed: H) { | ||
assert_eq!(format!("{}", HeaderDisplayAdapter(&typed)).as_bytes(), raw); | ||
assert_eq!(H::parse(raw), Some(typed)); | ||
} | ||
|
||
fn bad<H: ToHeader + Eq + fmt::Debug>(raw: &[u8]) { | ||
assert_eq!(H::parse(raw), None); | ||
} | ||
|
||
#[test] | ||
fn test_usize() { | ||
eq(b"0", 0usize); | ||
eq(b"1", 1usize); | ||
eq(b"123456789", 123456789usize); | ||
bad::<usize>(b"-1"); | ||
bad::<usize>(b"0xdeadbeef"); | ||
bad::<usize>(b"deadbeef"); | ||
bad::<usize>(b"1234567890123467901245790"); | ||
bad::<usize>(b"1,000"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters