Struct byte_reader::Reader
source · pub struct Reader<'b> { /* private fields */ }Implementations§
source§impl<'b> Reader<'b>
impl<'b> Reader<'b>
sourcepub fn new(content: impl IntoBytes<'b>) -> Self
pub fn new(content: impl IntoBytes<'b>) -> Self
Generate new Reader from Vec<u8> or &'b [u8]
Examples found in repository?
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
fn main() {
// Get a `&[u8]` or `Vec<u8>` input from
// a File, standard input, or something
let sample_input = "Hello, byte_reader!".as_bytes();
// Create mutable `r`
let mut r = Reader::new(sample_input);
// Use some simple operations
// to parse the input
r.consume("Hello").unwrap();
r.consume(",").unwrap();
r.skip_whitespace();
let name = r.read_snake().unwrap(); // byte_reader
r.consume("!").unwrap();
println!("Greeted to `{name}`.");
}source§impl<'b> Reader<'b>
impl<'b> Reader<'b>
sourcepub fn read_while(&mut self, condition: impl Fn(&u8) -> bool) -> &[u8] ⓘ
pub fn read_while(&mut self, condition: impl Fn(&u8) -> bool) -> &[u8] ⓘ
Read while the condition holds for the byte
sourcepub fn advance_by(&mut self, max_bytes: usize)
pub fn advance_by(&mut self, max_bytes: usize)
Advance by max_bytes bytes (or, if remained bytes is shorter than max_bytes, read all remained bytes)
sourcepub fn pop_if(&mut self, condition: impl Fn(&u8) -> bool) -> Option<u8>
pub fn pop_if(&mut self, condition: impl Fn(&u8) -> bool) -> Option<u8>
Read one byte if the condition holds for it
sourcepub fn peek3(&self) -> Option<&u8>
pub fn peek3(&self) -> Option<&u8>
Peek next byte of next byte of next byte (without consuming)
sourcepub fn skip_whitespace(&mut self)
pub fn skip_whitespace(&mut self)
Advance while the byte is ascii-whitespace
Examples found in repository?
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
fn main() {
// Get a `&[u8]` or `Vec<u8>` input from
// a File, standard input, or something
let sample_input = "Hello, byte_reader!".as_bytes();
// Create mutable `r`
let mut r = Reader::new(sample_input);
// Use some simple operations
// to parse the input
r.consume("Hello").unwrap();
r.consume(",").unwrap();
r.skip_whitespace();
let name = r.read_snake().unwrap(); // byte_reader
r.consume("!").unwrap();
println!("Greeted to `{name}`.");
}sourcepub fn consume(&mut self, token: &'static str) -> Result<(), Cow<'static, str>>
pub fn consume(&mut self, token: &'static str) -> Result<(), Cow<'static, str>>
Read token if the remained bytes starts with it, otherwise return Err
Examples found in repository?
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
fn main() {
// Get a `&[u8]` or `Vec<u8>` input from
// a File, standard input, or something
let sample_input = "Hello, byte_reader!".as_bytes();
// Create mutable `r`
let mut r = Reader::new(sample_input);
// Use some simple operations
// to parse the input
r.consume("Hello").unwrap();
r.consume(",").unwrap();
r.skip_whitespace();
let name = r.read_snake().unwrap(); // byte_reader
r.consume("!").unwrap();
println!("Greeted to `{name}`.");
}sourcepub fn consume_oneof<const N: usize>(
&mut self,
tokens: [&'static str; N]
) -> Result<usize, Cow<'static, str>>
pub fn consume_oneof<const N: usize>( &mut self, tokens: [&'static str; N] ) -> Result<usize, Cow<'static, str>>
Read first token in tokens that the remained bytes starts with, and returns the index of the (matched) token.
Returns Err if none matched.
sourcepub fn read_camel(&mut self) -> Result<String, Cow<'static, str>>
pub fn read_camel(&mut self) -> Result<String, Cow<'static, str>>
Read a camelCase word like helloWorld, userID, … as String
sourcepub fn read_snake(&mut self) -> Result<String, Cow<'static, str>>
pub fn read_snake(&mut self) -> Result<String, Cow<'static, str>>
Read a snake_case word like hello_world, user_id, … as String
Examples found in repository?
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
fn main() {
// Get a `&[u8]` or `Vec<u8>` input from
// a File, standard input, or something
let sample_input = "Hello, byte_reader!".as_bytes();
// Create mutable `r`
let mut r = Reader::new(sample_input);
// Use some simple operations
// to parse the input
r.consume("Hello").unwrap();
r.consume(",").unwrap();
r.skip_whitespace();
let name = r.read_snake().unwrap(); // byte_reader
r.consume("!").unwrap();
println!("Greeted to `{name}`.");
}sourcepub fn read_kebab(&mut self) -> Result<String, Cow<'static, str>>
pub fn read_kebab(&mut self) -> Result<String, Cow<'static, str>>
Read a kebeb-case word like hello-world, Content-Type, … as String
sourcepub fn read_string(&mut self) -> Result<String, Cow<'static, str>>
pub fn read_string(&mut self) -> Result<String, Cow<'static, str>>
Read a double-quoted string literal like "Hello, world!", "application/json", … and return the quoted content as String