use crate::{PosnInCharStream, UserPosn};
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Hash)]
pub struct StreamCharPos<P>
where
P: UserPosn,
{
byte_ofs: usize,
pos: P,
}
impl<P> StreamCharPos<P>
where
P: UserPosn,
{
pub fn pos(&self) -> P {
self.pos
}
}
impl<P> UserPosn for StreamCharPos<P>
where
P: UserPosn,
{
fn advance_cols(mut self, num_bytes: usize, num_chars: usize) -> Self {
self.byte_ofs += num_bytes;
self.pos = self.pos.advance_cols(num_bytes, num_chars);
self
}
fn advance_line(mut self, num_bytes: usize) -> Self {
self.byte_ofs += num_bytes;
self.pos = self.pos.advance_line(num_bytes);
self
}
fn line(&self) -> usize {
self.pos.line()
}
fn column(&self) -> usize {
self.pos.column()
}
fn error_fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
self.pos.error_fmt(fmt)
}
}
impl<P> PosnInCharStream for StreamCharPos<P>
where
P: UserPosn,
{
fn byte_ofs(&self) -> usize {
self.byte_ofs
}
}
impl<P> std::fmt::Display for StreamCharPos<P>
where
P: UserPosn,
{
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
self.pos.error_fmt(fmt)
}
}