use crate::error::{MarcError, Result};
use crate::iso2709::ParseContext;
pub const DEFAULT_MAX_ERRORS: usize = 10_000;
#[derive(Debug, Clone)]
pub struct RecoveryCap {
max_errors: usize,
error_count: usize,
exceeded: bool,
}
impl Default for RecoveryCap {
fn default() -> Self {
Self::new()
}
}
impl RecoveryCap {
#[must_use]
pub fn new() -> Self {
RecoveryCap {
max_errors: DEFAULT_MAX_ERRORS,
error_count: 0,
exceeded: false,
}
}
pub fn set_max(&mut self, n: usize) {
self.max_errors = n;
}
#[must_use]
pub fn is_exhausted(&self) -> bool {
self.exceeded
}
pub fn note(&mut self, ctx: &ParseContext) -> Result<()> {
if self.max_errors == 0 {
return Ok(());
}
self.error_count += 1;
if self.error_count > self.max_errors {
self.exceeded = true;
let idx = ctx.record_index;
return Err(MarcError::FatalReaderError {
cap: self.max_errors,
errors_seen: self.error_count,
record_index: if idx == 0 { None } else { Some(idx) },
source_name: ctx.source_name.clone(),
});
}
Ok(())
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum RecoveryMode {
#[default]
Strict,
Lenient,
Permissive,
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ValidationLevel {
#[default]
Structural,
StrictMarc,
}