Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add no_std feature, impl std::error::Error #22

Merged
merged 2 commits into from
Oct 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
49 changes: 34 additions & 15 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(all(feature = "no_std", not(test)), no_std)]
#![cfg_attr(test, deny(warnings))]
#![deny(missing_docs)]
//! # httparse
Expand All @@ -16,7 +16,12 @@

#[cfg(test)] extern crate core;

use core::{str, slice};
#[cfg(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 +116,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 +160,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 +600,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