Skip to content

Commit

Permalink
Add no_std feature, impl std::error::Error
Browse files Browse the repository at this point in the history
  • Loading branch information
abonander committed Oct 17, 2016
1 parent 49bf4d8 commit 2f6793f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 16 deletions.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ cache:
- target/debug/deps
- target/debug/build

script:
- cargo build
- cargo test --verbose
- cargo build --verbose --no-default-features
- cargo test --verbose --no-default-features

notifications:
email: false

Expand Down
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ license = "MIT"
description = "A tiny, safe, speedy, zero-copy HTTP/1.x parser."
repository = "https://github.com/seanmonstar/httparse"

[features]
default = ["no_std"]
no_std = []

[dev-dependencies]
pico-sys = "0.0"

Expand Down
4 changes: 4 additions & 0 deletions src/iter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#[cfg(feature = "no_std")]
use core::slice;

#[cfg(not(feature = "no_std"))]
use std::slice;

pub struct Bytes<'a> {
slice: &'a [u8],
pos: usize
Expand Down
50 changes: 34 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg_attr(not(test),no_std)]
#![cfg_attr(feature = "no_std", no_std)]
#![cfg_attr(test, deny(warnings))]
#![deny(missing_docs)]
//! # httparse
Expand All @@ -14,9 +14,13 @@
//! 1.6 times slower than pico. Improvements can be made as a `likely`
//! intrinsic, and simd, are stabilized in rustc.

#[cfg(test)] extern crate core;

use core::{str, slice};
#[cfg(any(feature = "no_std"))]
use core::{fmt, result, str, slice};

#[cfg(not(feature = "no_std"))]
use std::{fmt, result, str, slice};

use iter::Bytes;

mod iter;
Expand Down Expand Up @@ -111,27 +115,41 @@ pub enum Error {
Version,
}

impl ::core::fmt::Display for Error {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
impl Error {
#[inline]
fn description_str(&self) -> &'static str {
match *self {
Error::HeaderName => f.write_str("invalid header name"),
Error::HeaderValue => f.write_str("invalid header value"),
Error::NewLine => f.write_str("invalid new line"),
Error::Status => f.write_str("invalid response status"),
Error::Token => f.write_str("invalid token"),
Error::TooManyHeaders => f.write_str("too many headers"),
Error::Version => f.write_str("invalid HTTP version"),
Error::HeaderName => "invalid header name",
Error::HeaderValue => "invalid header value",
Error::NewLine => "invalid new line",
Error::Status => "invalid response status",
Error::Token => "invalid token",
Error::TooManyHeaders => "too many headers",
Error::Version => "invalid HTTP version",
}
}
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(self.description_str())
}
}

#[cfg(not(feature = "no_std"))]
impl std::error::Error for Error {
fn description(&self) -> &str {
self.description_str()
}
}

/// An error in parsing a chunk size.
// Note: Move this into the error enum once v2.0 is released.
#[derive(Debug, PartialEq, Eq)]
pub struct InvalidChunkSize;

impl ::core::fmt::Display for InvalidChunkSize {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
impl fmt::Display for InvalidChunkSize {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("invalid chunk size")
}
}
Expand All @@ -141,7 +159,7 @@ impl ::core::fmt::Display for InvalidChunkSize {
/// If the input is invalid, an `Error` will be returned. Note that incomplete
/// data is not considered invalid, and so will not return an error, but rather
/// a `Ok(Status::Partial)`.
pub type Result<T> = ::core::result::Result<Status<T>, Error>;
pub type Result<T> = result::Result<Status<T>, Error>;

/// The result of a successful parse pass.
///
Expand Down Expand Up @@ -581,7 +599,7 @@ fn parse_headers_iter<'a, 'b>(headers: &mut &mut [Header<'a>], bytes: &'b mut By
/// Ok(httparse::Status::Complete((3, 4))));
/// ```
pub fn parse_chunk_size(buf: &[u8])
-> ::core::result::Result<Status<(usize, u64)>, InvalidChunkSize> {
-> result::Result<Status<(usize, u64)>, InvalidChunkSize> {
const RADIX: u64 = 16;
let mut bytes = Bytes::new(buf);
let mut size = 0;
Expand Down

0 comments on commit 2f6793f

Please sign in to comment.