Skip to content
This repository was archived by the owner on Jul 22, 2019. It is now read-only.

Commit 7bb5dbe

Browse files
committed
Make parsing of strings optional
This fixes #5 as proposed in that issue. Specifically, it introduces a trait, `FromByteResponse`, and provides a generic `parse_response` function that parses input as raw byte strings, and then maps them using some implementation of that trait. This in turns allows users to choose how they want to parse byte sequences in an ergonomic way. Under the hood, this is *slightly* less efficient that it could be. Specifically, it parses everything as `&[u8]` first, and then maps everything using calls to `FromByteResponse`. This works fine, but will cause unnecessary re-allocation of vectors inside a bunch of structs. The way to work around this is to have `nom` directly use a generic return value in all its parsers, but this unfortunately doesn't seem to be supported at the time of writing.
1 parent 5b9dc9b commit 7bb5dbe

File tree

3 files changed

+315
-113
lines changed

3 files changed

+315
-113
lines changed

Diff for: src/lib.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
11
#[macro_use]
22
extern crate nom;
33

4+
pub trait FromByteResponse<'a> {
5+
fn from_bytes(&'a [u8]) -> Self;
6+
}
7+
8+
impl<'a> FromByteResponse<'a> for &'a str {
9+
fn from_bytes(b: &'a [u8]) -> Self {
10+
use std;
11+
std::str::from_utf8(b).unwrap()
12+
}
13+
}
14+
15+
impl<'a> FromByteResponse<'a> for &'a [u8] {
16+
fn from_bytes(b: &'a [u8]) -> Self {
17+
b
18+
}
19+
}
20+
421
pub mod builders;
522
mod parser;
623
pub mod types;
724

8-
pub use parser::{parse_response, ParseResult};
25+
pub use parser::ParseResult;
26+
pub use parser::{parse_response_raw, parse_response, parse_response_str};
927
pub use types::*;

0 commit comments

Comments
 (0)