use std::{
io::{BufRead, BufReader, Error, Lines as IoLines, Read},
str::{Bytes, Lines},
vec::IntoIter,
};
pub trait Input {
type Lines: Iterator;
type Line: AsRef<str>;
type Bytes: Iterator<Item = u8>;
fn lines(self) -> Self::Lines;
fn line(item: <Self::Lines as Iterator>::Item) -> Result<Self::Line, Error>;
fn bytes(line: Self::Line) -> Self::Bytes;
}
impl<'a> Input for &'a str {
type Lines = Lines<'a>;
type Line = &'a str;
type Bytes = Bytes<'a>;
fn lines(self) -> Self::Lines {
self.lines()
}
fn line(item: <Self::Lines as Iterator>::Item) -> Result<Self::Line, Error> {
Ok(item)
}
fn bytes(line: Self::Line) -> Self::Bytes {
line.bytes()
}
}
impl<'a> Input for Lines<'a> {
type Lines = Self;
type Line = &'a str;
type Bytes = Bytes<'a>;
fn lines(self) -> Self::Lines {
self
}
fn line(item: <Self::Lines as Iterator>::Item) -> Result<Self::Line, Error> {
Ok(item)
}
fn bytes(line: Self::Line) -> Self::Bytes {
line.bytes()
}
}
impl<'a> Input for &'a [u8] {
type Lines = IoLines<Self>;
type Line = String;
type Bytes = IntoIter<u8>;
fn lines(self) -> Self::Lines {
BufRead::lines(self)
}
fn line(item: <Self::Lines as Iterator>::Item) -> Result<Self::Line, Error> {
item
}
fn bytes(line: Self::Line) -> Self::Bytes {
line.into_bytes().into_iter()
}
}
impl<R: Read> Input for BufReader<R> {
type Lines = IoLines<Self>;
type Line = String;
type Bytes = IntoIter<u8>;
fn lines(self) -> Self::Lines {
BufRead::lines(self)
}
fn line(item: <Self::Lines as Iterator>::Item) -> Result<Self::Line, Error> {
item
}
fn bytes(line: Self::Line) -> Self::Bytes {
line.into_bytes().into_iter()
}
}
impl<'a, B: BufRead> Input for &'a mut B {
type Lines = IoLines<Self>;
type Line = String;
type Bytes = IntoIter<u8>;
fn lines(self) -> Self::Lines {
BufRead::lines(self)
}
fn line(item: <Self::Lines as Iterator>::Item) -> Result<Self::Line, Error> {
item
}
fn bytes(line: Self::Line) -> Self::Bytes {
line.into_bytes().into_iter()
}
}
impl<B: BufRead> Input for IoLines<B> {
type Lines = Self;
type Line = String;
type Bytes = IntoIter<u8>;
fn lines(self) -> Self::Lines {
self
}
fn line(item: <Self::Lines as Iterator>::Item) -> Result<Self::Line, Error> {
item
}
fn bytes(line: Self::Line) -> Self::Bytes {
line.into_bytes().into_iter()
}
}