pub trait BufReadExt: BufRead {
// Provided methods
fn read_while<P>(
&mut self,
buf: &mut Vec<u8>,
predicate: P,
) -> Result<usize>
where P: FnMut(u8) -> bool { ... }
fn skip(&mut self, n: usize) -> Result<()> { ... }
fn skip_while<P>(&mut self, predicate: P) -> Result<usize>
where P: FnMut(u8) -> bool { ... }
fn skip_until(&mut self, byte: u8) -> Result<usize> { ... }
}
Expand description
Extend BufRead
with methods for streaming parsing.
Provided Methods§
Sourcefn read_while<P>(&mut self, buf: &mut Vec<u8>, predicate: P) -> Result<usize>
fn read_while<P>(&mut self, buf: &mut Vec<u8>, predicate: P) -> Result<usize>
Read bytes based on a predicate.
read_while
takes a predicate as an argument.
It will call this on each byte, and copy it to the slice if the
predicate evaluates to true
. Returns the amount of bytes read.
§Errors
If this function encounters an error of the kind
ErrorKind::Interrupted
then the error is ignored and the operation
will continue.
If any other read error is encountered then this function immediately
returns. Any bytes which have already been read will be appended to
buf
.
§Examples
[std::io::Cursor
][Cursor
] is a type that implements BufRead
. In
this example, we use [Cursor
] to read bytes in a byte slice until
we encounter a hyphen:
use std::io::{self, BufRead};
use omnom::prelude::*;
let mut cursor = io::Cursor::new(b"lorem-ipsum");
let mut buf = vec![];
let num_bytes = cursor.read_while(&mut buf, |b| b != b'-')
.expect("reading from cursor won't fail");
assert_eq!(buf, b"lorem");
Examples found in repository?
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
fn parse_mime(s: &str) -> Option<Mime> {
// parse the "type"
//
// ```txt
// text/html; charset=utf-8;
// ^^^^^
// ```
let mut s = Cursor::new(s);
let mut base_type = vec![];
match s.read_until(b'/', &mut base_type).unwrap() {
0 => return None,
_ => base_type.pop(),
};
validate_code_points(&base_type)?;
// parse the "subtype"
//
// ```txt
// text/html; charset=utf-8;
// ^^^^^
// ```
let mut sub_type = vec![];
s.read_until(b';', &mut sub_type).unwrap();
if let Some(b';') = sub_type.last() {
sub_type.pop();
}
validate_code_points(&sub_type)?;
// instantiate our mime struct
let mut mime = Mime {
base_type: String::from_utf8(base_type).unwrap(),
sub_type: String::from_utf8(sub_type).unwrap(),
parameters: None,
};
// parse parameters into a hashmap
//
// ```txt
// text/html; charset=utf-8;
// ^^^^^^^^^^^^^^^
// ```
loop {
// Stop parsing if there's no more bytes to consume.
if s.fill_buf().unwrap().len() == 0 {
break;
}
// Trim any whitespace.
//
// ```txt
// text/html; charset=utf-8;
// ^
// ```
s.skip_while(is_http_whitespace_char).ok()?;
// Get the param name.
//
// ```txt
// text/html; charset=utf-8;
// ^^^^^^^
// ```
let mut param_name = vec![];
s.read_while(&mut param_name, |b| b != b';' && b != b'=')
.ok()?;
validate_code_points(¶m_name)?;
let mut param_name = String::from_utf8(param_name).ok()?;
param_name.make_ascii_lowercase();
// Ignore param names without values.
//
// ```txt
// text/html; charset=utf-8;
// ^
// ```
let mut token = vec![0; 1];
s.read_exact(&mut token).unwrap();
if token[0] == b';' {
continue;
}
// Get the param value.
//
// ```txt
// text/html; charset=utf-8;
// ^^^^^^
// ```
let mut param_value = vec![];
s.read_until(b';', &mut param_value).ok()?;
if let Some(b';') = param_value.last() {
param_value.pop();
}
validate_code_points(¶m_value)?;
let mut param_value = String::from_utf8(param_value).ok()?;
param_value.make_ascii_lowercase();
// Insert attribute pair into hashmap.
if let None = mime.parameters {
mime.parameters = Some(HashMap::new());
}
mime.parameters.as_mut()?.insert(param_name, param_value);
}
Some(mime)
}
Sourcefn skip_while<P>(&mut self, predicate: P) -> Result<usize>
fn skip_while<P>(&mut self, predicate: P) -> Result<usize>
Skip bytes while the predicate is true.
Examples found in repository?
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
fn parse_mime(s: &str) -> Option<Mime> {
// parse the "type"
//
// ```txt
// text/html; charset=utf-8;
// ^^^^^
// ```
let mut s = Cursor::new(s);
let mut base_type = vec![];
match s.read_until(b'/', &mut base_type).unwrap() {
0 => return None,
_ => base_type.pop(),
};
validate_code_points(&base_type)?;
// parse the "subtype"
//
// ```txt
// text/html; charset=utf-8;
// ^^^^^
// ```
let mut sub_type = vec![];
s.read_until(b';', &mut sub_type).unwrap();
if let Some(b';') = sub_type.last() {
sub_type.pop();
}
validate_code_points(&sub_type)?;
// instantiate our mime struct
let mut mime = Mime {
base_type: String::from_utf8(base_type).unwrap(),
sub_type: String::from_utf8(sub_type).unwrap(),
parameters: None,
};
// parse parameters into a hashmap
//
// ```txt
// text/html; charset=utf-8;
// ^^^^^^^^^^^^^^^
// ```
loop {
// Stop parsing if there's no more bytes to consume.
if s.fill_buf().unwrap().len() == 0 {
break;
}
// Trim any whitespace.
//
// ```txt
// text/html; charset=utf-8;
// ^
// ```
s.skip_while(is_http_whitespace_char).ok()?;
// Get the param name.
//
// ```txt
// text/html; charset=utf-8;
// ^^^^^^^
// ```
let mut param_name = vec![];
s.read_while(&mut param_name, |b| b != b';' && b != b'=')
.ok()?;
validate_code_points(¶m_name)?;
let mut param_name = String::from_utf8(param_name).ok()?;
param_name.make_ascii_lowercase();
// Ignore param names without values.
//
// ```txt
// text/html; charset=utf-8;
// ^
// ```
let mut token = vec![0; 1];
s.read_exact(&mut token).unwrap();
if token[0] == b';' {
continue;
}
// Get the param value.
//
// ```txt
// text/html; charset=utf-8;
// ^^^^^^
// ```
let mut param_value = vec![];
s.read_until(b';', &mut param_value).ok()?;
if let Some(b';') = param_value.last() {
param_value.pop();
}
validate_code_points(¶m_value)?;
let mut param_value = String::from_utf8(param_value).ok()?;
param_value.make_ascii_lowercase();
// Insert attribute pair into hashmap.
if let None = mime.parameters {
mime.parameters = Some(HashMap::new());
}
mime.parameters.as_mut()?.insert(param_name, param_value);
}
Some(mime)
}
Sourcefn skip_until(&mut self, byte: u8) -> Result<usize>
fn skip_until(&mut self, byte: u8) -> Result<usize>
Skip bytes until the delimiter byte
or EOF is reached.
This function will read bytes from the underlying stream until the delimiter or EOF is found. Once found, all bytes up to, and including, the delimiter (if found) will be skipped.
If successful, this function will return the total number of bytes read.
§Errors
This function will ignore all instances of ErrorKind::Interrupted
and
will otherwise return any errors returned by BufRead::fill_buf
.
If an I/O error is encountered then all bytes read so far will be
present in buf
and its length will have been adjusted appropriately.
§Examples
std::io::Cursor
is a type that implements BufRead
. In
this example, we use Cursor
to read all the bytes in a byte slice
in hyphen delimited segments:
use std::io::{self, BufRead, Read};
use omnom::prelude::*;
let mut cursor = io::Cursor::new(b"lorem-ipsum");
// skip up to and including '-'
let num_bytes = cursor.skip_until(b'-').unwrap();
assert_eq!(num_bytes, 6);
// read the rest of the bytes
let mut buf = [0; 5];
cursor.read_exact(&mut buf).unwrap();
assert_eq!(&buf, b"ipsum");
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.