limit error printing

This commit is contained in:
mos 2024-11-01 10:48:01 +01:00
parent 92f3c09252
commit 393db5da62
1 changed files with 13 additions and 2 deletions

View File

@ -2,6 +2,8 @@ use bip::bp;
use std::fmt;
use std::io::{BufRead, BufReader};
const ERR_MAX: usize = 40;
pub type Result<T> = std::result::Result<T, Error>;
pub enum Error {
@ -39,12 +41,21 @@ fn read_bip<R: BufRead>(buf: R) -> Result<Vec<bip::Node>> {
.collect();
let err_count = errors.iter().fold(0, |count, err| {
eprintln!("error: {err}");
if count < ERR_MAX {
eprintln!("error: {err}");
}
count + 1
});
if err_count > 0 {
eprintln!("{err_count} error{}", if err_count == 1 { "" } else { "s" });
let count_fmt = format!("{err_count} error{}", if err_count == 1 { "" } else { "s" });
if err_count >= ERR_MAX {
eprintln!("{} ({} shown)", count_fmt, ERR_MAX);
} else {
eprintln!("{}", count_fmt);
}
Err(Error::ReadError)
} else {
bip::Parser::new(&syms).parse().map_err(Error::ParseError)