use std::fs::File;
use std::io::{self, BufRead, BufReader, Read};
use std::path::Path;
use thiserror::Error;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ValidationIssue {
pub n: usize,
pub reason: String,
}
impl ValidationIssue {
pub fn new<T>(n: usize, reason: &T) -> Self
where
T: ToString,
{
Self {
n,
reason: reason.to_string(),
}
}
}
#[derive(Error, Debug)]
pub enum AnnotationLoadError {
#[error("I/O error")]
IoError(#[from] io::Error),
#[error("Validation issues encountered")]
ValidationError(Vec<ValidationIssue>),
#[error("Error: {0}")]
Error(String),
}
pub trait AnnotationLoader<A> {
fn load_from_path<P>(&self, path: P) -> Result<A, AnnotationLoadError>
where
P: AsRef<Path>,
{
self.load_from_read(File::open(path)?)
}
fn load_from_read<R>(&self, read: R) -> Result<A, AnnotationLoadError>
where
R: Read,
{
self.load_from_buf_read(BufReader::new(read))
}
fn load_from_buf_read<R>(&self, read: R) -> Result<A, AnnotationLoadError>
where
R: BufRead;
}