#![allow(dead_code, unused)]
use crate::numbers::{self, Float, Int};
use std::error::Error;
use std::fmt::{self, Display};
use std::io::{BufReader, Cursor, Read};
use std::result::Result;
#[derive(Debug)]
pub enum ScannerError<E> {
NoMoreData,
ParseError(E),
}
impl<E: std::fmt::Debug + std::fmt::Display> Display for ScannerError<E> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ScannerError::NoMoreData => write!(f, "no more data"),
ScannerError::ParseError(e) => write!(f, "Parse error {}", e),
}
}
}
impl<E: std::fmt::Debug + std::fmt::Display> Error for ScannerError<E> {}
#[derive(Debug, PartialEq, Clone)]
pub struct Scanner {
data: Vec<u8>,
wrds: Option<Vec<String>>,
counter: usize,
}
impl Scanner {
pub fn new<T: Read>(f: T) -> Self {
let mut values: Vec<u8> = Vec::new();
let mut buf = BufReader::new(f);
buf.read_to_end(&mut values).expect("Invalid read");
Self {
data: values,
counter: 0,
wrds: None,
}
}
pub fn from_str(str: &str) -> Self {
let mut data: Vec<u8> = Vec::new();
let mut cursor = Cursor::new(str.as_bytes());
cursor.read_to_end(&mut data).expect("Invalid stream");
Self {
data,
counter: 0,
wrds: None,
}
}
pub fn has_next(&self) -> bool {
if self.data.len() == 0 {
return false;
}
self.counter < self.data.len()
}
pub fn next_byte(&self) -> Option<u8> {
if self.has_next() {
Some(self.data[self.counter])
} else {
None
}
}
pub(crate) fn delimiter(&self, sep: char) -> Vec<String> {
String::from_utf8_lossy(&self.data)
.split(sep)
.filter(|s| !s.is_empty())
.map(|s| s.to_string())
.collect()
}
pub fn next_word(&mut self) -> Option<String> {
if self.wrds.is_none() {
self.wrds = Some(self.delimiter(' '));
}
let aword = self.wrds.as_ref().expect("can't use as reference");
if self.counter < aword.len() {
let word = aword[self.counter].clone();
self.counter += 1;
Some(word)
} else {
None
}
}
pub fn next_line(&mut self) -> Option<String> {
if self.wrds.is_none() {
self.wrds = Some(self.delimiter('\n'));
}
let aword = self.wrds.as_ref().expect("can't use as reference");
if self.counter < aword.len() {
let word = aword[self.counter].clone();
self.counter += 1;
Some(word)
} else {
None
}
}
pub fn next_number<T>(&mut self) -> Result<T, ScannerError<<T as std::str::FromStr>::Err>>
where
T: std::str::FromStr,
{
if self.wrds.is_none() {
self.wrds = Some(self.delimiter(' '));
}
let aword = self.wrds.as_ref().expect("can't use as reference");
if self.counter < aword.len() {
let word = aword[self.counter].clone();
self.counter += 1;
word.trim().parse::<T>().map_err(ScannerError::ParseError)
} else {
Err(ScannerError::NoMoreData)
}
}
pub fn next_int<T>(&mut self) -> Result<T, ScannerError<<T as std::str::FromStr>::Err>>
where
T: Int,
{
self.next_number::<T>()
}
pub fn next_float<T>(&mut self) -> Result<T, ScannerError<<T as std::str::FromStr>::Err>>
where
T: Float,
{
self.next_number::<T>()
}
pub fn next_double(&mut self) -> Result<f64, ScannerError<<f64 as std::str::FromStr>::Err>> {
self.next_number::<f64>()
}
}
impl Iterator for Scanner {
type Item = u8;
fn next(&mut self) -> Option<Self::Item> {
return if self.has_next() {
Some(
self.data[{
let tmp = self.counter;
self.counter += 1;
tmp
}],
)
} else {
None
};
}
}
#[cfg(test)]
mod unittest;