seanmonstar / httparse

A push parser for the HTTP 1.x protocol in Rust.
https://docs.rs/httparse
Apache License 2.0
585 stars 114 forks source link

Make the Error::description_str public. #69

Closed gavadinov closed 3 years ago

gavadinov commented 4 years ago

This will make it easier to wrap the parse error in another error. Right now to achieve this you have to do something like

pub enum Error {
    AnotherError,
    ParseError(httparse::Error),
}

impl Display for Error {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        let message = match self {
            AnotherError => String::from("MESSAGE"),
            Error::ParseError(e) => format!("{}", e),
...

By going through the Display trait we have to allocate a new String and copy the static error message into it. If we had access to the &'static str error description the match above would be much simpler and will have no need for allocations.

AnotherError => "MESSAGE",
Error::ParseError(e) => e.description_str(),