Crates.io | read_chars |
lib.rs | read_chars |
version | 0.3.0 |
source | src |
created_at | 2023-08-22 15:21:03.640738 |
updated_at | 2024-01-03 19:38:14.267289 |
description | An iterator over characters read from some I/O source. |
homepage | |
repository | https://github.com/AshtonSnapp/read_chars |
max_upload_size | |
id | 951135 |
size | 5,234 |
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(())
}