Struct byte_reader::Reader

source ·
pub struct Reader<B: Bytes> { /* private fields */ }

Implementations§

source§

impl<B: Bytes> Reader<B>

source

pub fn new(content: B) -> Self

Generate new Reader from &str | String | &[u8] | Vec<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 input from a File, standard input, or others
    // Input can be `&str`, `String`, `&[u8]`, or `Vec<u8>`
    let sample_input = "Hello,    byte_reader!";

    // 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: Bytes> Reader<B>

source

pub fn advance_by(&mut self, max: usize)

Advance by max bytes (or, if remained bytes is shorter than max, read all remained bytes)

source

pub fn unwind_by(&mut self, max: usize)

Unwind the parsing point by max bytes (or, if already-read bytes is shorter than max, rewind all)

When "location" feature is activated, this may be less performant for some extensive input

source

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

Skip next byte while condition holds on it

source

pub fn skip_whitespace(&mut self)

.skip_while(|b| b.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 input from a File, standard input, or others
    // Input can be `&str`, `String`, `&[u8]`, or `Vec<u8>`
    let sample_input = "Hello,    byte_reader!";

    // 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_while(&mut self, condition: impl Fn(&u8) -> bool) -> &[u8]

Read next byte while the condition holds on it

source

pub fn next(&mut self) -> Option<u8>

Read next one byte, or return None if the remained bytes is empty

source

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

Read next one byte if the condition holds on 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 consume(&mut self, token: impl Bytes) -> Option<()>

Read token if the remained bytes starts with it, otherwise return Err

token : &str | String | &[u8] | Vec<u8>

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 input from a File, standard input, or others
    // Input can be `&str`, `String`, `&[u8]`, or `Vec<u8>`
    let sample_input = "Hello,    byte_reader!";

    // 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: [impl Bytes; N] ) -> Option<usize>

Read first token in tokens that the remained bytes starts with, and returns the index of the (matched) token, or Err if none matched

token : &str | String | &[u8] | Vec<u8>

source

pub fn read_camel(&mut self) -> Option<String>

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

source

pub fn read_snake(&mut self) -> Option<String>

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 input from a File, standard input, or others
    // Input can be `&str`, `String`, `&[u8]`, or `Vec<u8>`
    let sample_input = "Hello,    byte_reader!";

    // 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) -> Option<String>

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

source

pub fn read_string(&mut self) -> Option<String>

Read a double-quoted UTF-8 string literal like "Hello, world!", "application/json", … and return the quoted content as String

  • Returns None if

    • Expected "s were not found
    • The quoted content is not UTF-8
  • This doesn’t handle escape sequences

source

pub unsafe fn read_string_unchecked(&mut self) -> Option<String>

Read a double-quoted string literal like "Hello, world!", "application/json", … the and return the quoted content as String without checking if the content bytes is valid UTF-8

This doesn’t handle escape sequences

source

pub fn read_uint(&mut self) -> Option<usize>

Read an unsigned integer literal like 42, 123 if found, and return it as usize

  • Panics if the integer is larger then usize::MAX
source

pub fn read_int(&mut self) -> Option<isize>

Read an integer literal like 42, -1111 if found, and return it as isize

  • Panics if the integer is larger then isize::MAX or smaller then isize::MIN

Auto Trait Implementations§

§

impl<B> RefUnwindSafe for Reader<B>where B: RefUnwindSafe,

§

impl<B> Send for Reader<B>where B: Send,

§

impl<B> Sync for Reader<B>where B: Sync,

§

impl<B> Unpin for Reader<B>where B: Unpin,

§

impl<B> UnwindSafe for Reader<B>where B: UnwindSafe,

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.