pub struct TokenReader<R> { /* private fields */ }
Expand description
This struct wraps a BufReader
to allow easy parsing of whitespace delimited files.
Implementations§
Source§impl<R: BufRead> TokenReader<R>
impl<R: BufRead> TokenReader<R>
Sourcepub fn new(buf_read: R) -> Self
pub fn new(buf_read: R) -> Self
Creates a TokenReader
from a type that implements BufRead
, such as Stdin
.
Sourcepub fn line<T>(&mut self) -> Result<T, ReadTokensError<T::Error>>where
T: FromTokens,
pub fn line<T>(&mut self) -> Result<T, ReadTokensError<T::Error>>where
T: FromTokens,
Reads and parses a single line of whitespace delimited tokens.
§Examples
let mut input = TokenReader::new("James 158000 0.58".as_bytes());
let (name, points, win_chance): (String, u64, f64) = input.line()?;
assert_eq!(name, "James");
assert_eq!(points, 158000);
assert_eq!(win_chance, 0.58);
let mut input = TokenReader::new("13 8 17".as_bytes());
let numbers: Vec<i64> = input.line()?;
assert_eq!(numbers, vec![13, 8, 17]);
Sourcepub fn line_raw(&mut self) -> Result<String, ReadLineError>
pub fn line_raw(&mut self) -> Result<String, ReadLineError>
Reads a single line, unmodified.
§Example
let mut input = TokenReader::new("1. Write a parsing library in Rust.\n2. ???\n3. Profit!".as_bytes());
let line = input.line_raw()?;
assert_eq!(line, "1. Write a parsing library in Rust.");
Sourcepub fn take<T>(&mut self, count: usize) -> Take<'_, T, R, usize> ⓘwhere
T: FromTokens,
pub fn take<T>(&mut self, count: usize) -> Take<'_, T, R, usize> ⓘwhere
T: FromTokens,
Creates an iterator that reads and parses a specific number of lines.
The line count must be an usize
.
You can also use TokenReader::take_count
, which allows other types.
§Example
let mut input = TokenReader::new("1 a\n2 b\n3 c".as_bytes());
let lines: Vec<(u64, char)> = input.take(3).collect::<Result<_, _>>()?;
assert_eq!(lines, vec![(1, 'a'), (2, 'b'), (3, 'c')]);
Sourcepub fn take_count<T, S>(&mut self, count: S) -> Take<'_, T, R, S> ⓘwhere
T: FromTokens,
S: LineCount,
pub fn take_count<T, S>(&mut self, count: S) -> Take<'_, T, R, S> ⓘwhere
T: FromTokens,
S: LineCount,
Like TokenReader::take
, but can use non-usize
counts.
This method can use any type implementing LineCount
as the element count, like u32
.
It can be used to process more than 2^32 lines on 32-bit systems.
§Example
let mut input = TokenReader::new("1 a\n2 b\n3 c".as_bytes());
let lines: Vec<(u64, char)> = input.take_count(3u64).collect::<Result<_, _>>()?;
assert_eq!(lines, vec![(1, 'a'), (2, 'b'), (3, 'c')]);
Source§impl<R: Read> TokenReader<BufReader<R>>
impl<R: Read> TokenReader<BufReader<R>>
Sourcepub fn from_read(read: R) -> Self
pub fn from_read(read: R) -> Self
Creates a TokenReader
from a type that implements Read
.
This is a convenience method for wrapping the reader with BufReader
.
Trait Implementations§
Source§impl<R: Debug> Debug for TokenReader<R>
impl<R: Debug> Debug for TokenReader<R>
Source§impl<R> From<R> for TokenReader<R>where
R: BufRead,
impl<R> From<R> for TokenReader<R>where
R: BufRead,
Source§fn from(value: R) -> Self
fn from(value: R) -> Self
Wraps an implementation of BufRead
.
Identical to TokenReader::new
.