scanner/stats: move MStats into its own module

and update the various use statements, plus better documentation on the
fields / methods
This commit is contained in:
Paul Stemmet 2021-09-06 13:23:52 +00:00 committed by Paul Stemmet
parent 1ed9d45344
commit 842ed7cacb
7 changed files with 92 additions and 78 deletions

View file

@ -66,7 +66,7 @@
//! queue position
//! ```
use super::MStats;
use crate::scanner::stats::MStats;
/// Manages the state for tracking possible implicit Keys
/// that the Scanner might may encounter during runtime

View file

@ -8,10 +8,9 @@ mod entry;
mod error;
mod key;
mod scalar;
mod stats;
mod tag;
use std::ops::{Add, AddAssign};
use atoi::atoi;
use self::{
@ -20,6 +19,7 @@ use self::{
error::{ScanError, ScanResult as Result},
key::{Key, KeyPossible},
scalar::{block::scan_block_scalar, plain::scan_plain_scalar},
stats::MStats,
};
use crate::{
queue::Queue,
@ -1224,77 +1224,6 @@ fn is_plain_safe_c(base: &str, offset: usize, block_context: bool) -> bool
block_context || (flow_context && not_flow_indicator)
}
/// Vessel for tracking various stats about the underlying
/// buffer that are required for correct parsing of certain
/// elements, and when contextualizing an error.
#[derive(Debug, Clone, PartialEq)]
struct MStats
{
read: usize,
lines: usize,
column: usize,
}
impl MStats
{
fn new() -> Self
{
Self::default()
}
fn update(&mut self, read: usize, lines: usize, column: usize)
{
self.read += read;
self.lines += lines;
match lines
{
0 => self.column += column,
_ => self.column = column,
}
}
}
impl Default for MStats
{
fn default() -> Self
{
Self {
read: 0,
lines: 0,
column: 0,
}
}
}
impl Add for MStats
{
type Output = Self;
fn add(mut self, rhs: Self) -> Self::Output
{
self += rhs;
self
}
}
impl AddAssign for MStats
{
fn add_assign(&mut self, rhs: Self)
{
self.update(rhs.read, rhs.lines, rhs.column)
}
}
impl PartialEq<(usize, usize, usize)> for MStats
{
fn eq(&self, (read, lines, column): &(usize, usize, usize)) -> bool
{
self.read == *read && self.lines == *lines && self.column == *column
}
}
const DIRECTIVE: u8 = b'%';
const ANCHOR: u8 = b'&';
const ALIAS: u8 = b'*';

View file

@ -26,7 +26,7 @@ use crate::{
scanner::{
context::Context,
error::{ScanError, ScanResult as Result},
MStats,
stats::MStats,
},
token::{ScalarStyle, Slice, Token},
};

View file

@ -4,7 +4,7 @@ use crate::{
scanner::{
error::{ScanError, ScanResult as Result},
scalar::escape::flow_unescape,
MStats,
stats::MStats,
},
token::{ScalarStyle, Token},
};

View file

@ -2,7 +2,7 @@ use crate::{
scanner::{
context::Context,
error::{ScanError, ScanResult as Result},
MStats,
stats::MStats,
},
token::{ScalarStyle, Token},
};

85
src/scanner/stats.rs Normal file
View file

@ -0,0 +1,85 @@
//! Contains the structure used for tracking marker stats in
//! a buffer, namely:
//!
//! - How far into the buffer have we read?
//! - How many lines have we read?
//! - What is the current column?
use std::ops::{Add, AddAssign};
/// Vessel for tracking various stats about the underlying
/// buffer that are required for correct parsing of certain
/// elements, and when contextualizing an error.
#[derive(Debug, Clone, PartialEq)]
pub(in crate::scanner) struct MStats
{
/// Amount of bytes read from the underlying byte stream
pub read: usize,
/// Number of lines seen in the underlying byte stream
pub lines: usize,
/// The offset from the last line break into a line
pub column: usize,
}
impl MStats
{
/// Construct a new empty MStats instance
pub fn new() -> Self
{
Self::default()
}
/// Update the stored stats with the given .read .lines
/// and .column
pub fn update(&mut self, read: usize, lines: usize, column: usize)
{
self.read += read;
self.lines += lines;
match lines
{
0 => self.column += column,
_ => self.column = column,
}
}
}
impl Default for MStats
{
fn default() -> Self
{
Self {
read: 0,
lines: 0,
column: 0,
}
}
}
impl Add for MStats
{
type Output = Self;
fn add(mut self, rhs: Self) -> Self::Output
{
self += rhs;
self
}
}
impl AddAssign for MStats
{
fn add_assign(&mut self, rhs: Self)
{
self.update(rhs.read, rhs.lines, rhs.column)
}
}
impl PartialEq<(usize, usize, usize)> for MStats
{
fn eq(&self, (read, lines, column): &(usize, usize, usize)) -> bool
{
self.read == *read && self.lines == *lines && self.column == *column
}
}

View file

@ -67,7 +67,7 @@ use crate::{
eat_whitespace,
error::{ScanError, ScanResult as Result},
scalar::escape::tag_uri_unescape,
MStats,
stats::MStats,
},
token::{Slice, Token},
};