Skip to content

Commit cdcc850

Browse files
abonanderseanmonstar
authored andcommitted
Add no_std feature, impl std::error::Error (#22)
1 parent 49bf4d8 commit cdcc850

File tree

4 files changed

+48
-15
lines changed

4 files changed

+48
-15
lines changed

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ cache:
1212
- target/debug/deps
1313
- target/debug/build
1414

15+
script:
16+
- cargo build
17+
- cargo test --verbose
18+
- cargo build --verbose --no-default-features
19+
- cargo test --verbose --no-default-features
20+
1521
notifications:
1622
email: false
1723

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ license = "MIT"
77
description = "A tiny, safe, speedy, zero-copy HTTP/1.x parser."
88
repository = "https://github.com/seanmonstar/httparse"
99

10+
[features]
11+
default = ["no_std"]
12+
no_std = []
13+
1014
[dev-dependencies]
1115
pico-sys = "0.0"
1216

src/iter.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
#[cfg(feature = "no_std")]
12
use core::slice;
23

4+
#[cfg(not(feature = "no_std"))]
5+
use std::slice;
6+
37
pub struct Bytes<'a> {
48
slice: &'a [u8],
59
pos: usize

src/lib.rs

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![cfg_attr(not(test),no_std)]
1+
#![cfg_attr(all(feature = "no_std", not(test)), no_std)]
22
#![cfg_attr(test, deny(warnings))]
33
#![deny(missing_docs)]
44
//! # httparse
@@ -16,7 +16,12 @@
1616
1717
#[cfg(test)] extern crate core;
1818

19-
use core::{str, slice};
19+
#[cfg(feature = "no_std")]
20+
use core::{fmt, result, str, slice};
21+
22+
#[cfg(not(feature = "no_std"))]
23+
use std::{fmt, result, str, slice};
24+
2025
use iter::Bytes;
2126

2227
mod iter;
@@ -111,27 +116,41 @@ pub enum Error {
111116
Version,
112117
}
113118

114-
impl ::core::fmt::Display for Error {
115-
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
119+
impl Error {
120+
#[inline]
121+
fn description_str(&self) -> &'static str {
116122
match *self {
117-
Error::HeaderName => f.write_str("invalid header name"),
118-
Error::HeaderValue => f.write_str("invalid header value"),
119-
Error::NewLine => f.write_str("invalid new line"),
120-
Error::Status => f.write_str("invalid response status"),
121-
Error::Token => f.write_str("invalid token"),
122-
Error::TooManyHeaders => f.write_str("too many headers"),
123-
Error::Version => f.write_str("invalid HTTP version"),
123+
Error::HeaderName => "invalid header name",
124+
Error::HeaderValue => "invalid header value",
125+
Error::NewLine => "invalid new line",
126+
Error::Status => "invalid response status",
127+
Error::Token => "invalid token",
128+
Error::TooManyHeaders => "too many headers",
129+
Error::Version => "invalid HTTP version",
124130
}
125131
}
126132
}
127133

134+
impl fmt::Display for Error {
135+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
136+
f.write_str(self.description_str())
137+
}
138+
}
139+
140+
#[cfg(not(feature = "no_std"))]
141+
impl std::error::Error for Error {
142+
fn description(&self) -> &str {
143+
self.description_str()
144+
}
145+
}
146+
128147
/// An error in parsing a chunk size.
129148
// Note: Move this into the error enum once v2.0 is released.
130149
#[derive(Debug, PartialEq, Eq)]
131150
pub struct InvalidChunkSize;
132151

133-
impl ::core::fmt::Display for InvalidChunkSize {
134-
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
152+
impl fmt::Display for InvalidChunkSize {
153+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
135154
f.write_str("invalid chunk size")
136155
}
137156
}
@@ -141,7 +160,7 @@ impl ::core::fmt::Display for InvalidChunkSize {
141160
/// If the input is invalid, an `Error` will be returned. Note that incomplete
142161
/// data is not considered invalid, and so will not return an error, but rather
143162
/// a `Ok(Status::Partial)`.
144-
pub type Result<T> = ::core::result::Result<Status<T>, Error>;
163+
pub type Result<T> = result::Result<Status<T>, Error>;
145164

146165
/// The result of a successful parse pass.
147166
///
@@ -581,7 +600,7 @@ fn parse_headers_iter<'a, 'b>(headers: &mut &mut [Header<'a>], bytes: &'b mut By
581600
/// Ok(httparse::Status::Complete((3, 4))));
582601
/// ```
583602
pub fn parse_chunk_size(buf: &[u8])
584-
-> ::core::result::Result<Status<(usize, u64)>, InvalidChunkSize> {
603+
-> result::Result<Status<(usize, u64)>, InvalidChunkSize> {
585604
const RADIX: u64 = 16;
586605
let mut bytes = Bytes::new(buf);
587606
let mut size = 0;

0 commit comments

Comments
 (0)