Struct byte_reader::Reader

source ·
pub struct Reader<'b> { /* private fields */ }

Implementations§

source§

impl<'b> Reader<'b>

source

pub fn new(content: impl IntoBytes<'b>) -> Self

Generate new Reader from Vec<u8> or &'b [u8]

Examples found in repository?
examples/usage.rs (line 9)
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>

source

pub fn read_while(&mut self, condition: impl Fn(&u8) -> bool) -> &[u8]

Read while the condition holds for the byte

source

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)

source

pub fn pop_if(&mut self, condition: impl Fn(&u8) -> bool) -> Option<u8>

Read one byte if the condition holds for it

source

pub fn peek(&self) -> Option<&u8>

Peek next byte (without consuming)

source

pub fn peek2(&self) -> Option<&u8>

Peek next byte of next byte (without consuming)

source

pub fn peek3(&self) -> Option<&u8>

Peek next byte of next byte of next byte (without consuming)

source

pub fn skip_whitespace(&mut self)

Advance while the byte is ascii-whitespace

Examples found in repository?
examples/usage.rs (line 15)
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

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?
examples/usage.rs (line 13)
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

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.

source

pub fn read_camel(&mut self) -> Result<String, Cow<'static, str>>

Read a camelCase word like helloWorld, userID, … as String

source

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?
examples/usage.rs (line 16)
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

pub fn read_kebab(&mut self) -> Result<String, Cow<'static, str>>

Read a kebeb-case word like hello-world, Content-Type, … as String

source

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

source

pub fn read_unsigned_int(&mut self) -> Result<usize, Cow<'static, str>>

Read a unsigned integer literal like 42, 123 as usize

source

pub fn read_int(&mut self) -> Result<isize, Cow<'static, str>>

Read a integer literal like 42, -1111 as isize

Auto Trait Implementations§

§

impl<'b> RefUnwindSafe for Reader<'b>

§

impl<'b> Send for Reader<'b>

§

impl<'b> Sync for Reader<'b>

§

impl<'b> Unpin for Reader<'b>

§

impl<'b> UnwindSafe for Reader<'b>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.