read_chars

Crates.ioread_chars
lib.rsread_chars
version0.3.0
sourcesrc
created_at2023-08-22 15:21:03.640738
updated_at2024-01-03 19:38:14.267289
descriptionAn iterator over characters read from some I/O source.
homepage
repositoryhttps://github.com/AshtonSnapp/read_chars
max_upload_size
id951135
size5,234
Ashton Scott Snapp (AshtonSnapp)

documentation

README

read_chars

This is just a simple library I made for use in a new lexer for my programming language project Rouge. You are free to use it how you wish under the MIT license.

use read_chars::ReadChars;
use std::convert::From;
use std::io;
use std::fs::File;

fn main() -> io::Result<()> {
	let chars = ReadChars::from(File::open("test.txt")?);

	let max_nesting = chars.filter(|r| match r { Ok(c) if c == '(' || c == ')' => Some(c), _ => None })
		.scan(0, |acc, chr| if chr == '(' { acc + 1 } else { acc - 1 })
		.max()
		.ok_or_else(|| io::Error::from(io::ErrorKind::InvalidData))?;

	println!("Max parentheses nesting in file 'text,txt' is {max_nesting}.");

	Ok(())
}
Commit count: 4

cargo fmt