Skip to main content

webgestalt_lib/
lib.rs

1#![doc = include_str!("../README.md")]
2use std::{error::Error, fmt};
3
4pub mod methods;
5pub mod readers;
6pub mod stat;
7pub mod writers;
8trait CustomError {
9    fn msg(&self) -> String;
10}
11
12#[derive(Debug)]
13pub enum WebGestaltError {
14    MalformedFile(MalformedError),
15    StatisticsError(StatisticsError),
16    IOError(std::io::Error),
17}
18
19impl Error for WebGestaltError {}
20
21impl fmt::Display for WebGestaltError {
22    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
23        let msg: String = match &self {
24            WebGestaltError::MalformedFile(x) => x.msg(),
25            WebGestaltError::StatisticsError(x) => x.msg(),
26            WebGestaltError::IOError(x) => x.to_string(),
27        };
28        write!(f, "{}", msg)
29    }
30}
31
32#[derive(Debug)]
33pub struct MalformedError {
34    pub path: String,
35    pub kind: MalformedErrorType,
36}
37
38#[derive(Debug)]
39pub enum MalformedErrorType {
40    NoColumnsFound { delimeter: String },
41    WrongFormat { found: String, expected: String },
42    Unknown,
43}
44
45impl CustomError for MalformedError {
46    fn msg(&self) -> String {
47        let error_msg = match &self.kind {
48            MalformedErrorType::WrongFormat { found, expected } => format!(
49                "Wrong Format Found. Found: {}; Expected: {}",
50                found, expected
51            ),
52            MalformedErrorType::Unknown => String::from("Unknown error type."),
53            MalformedErrorType::NoColumnsFound { delimeter } => format!(
54                "No column found with delimeter {}",
55                if delimeter == "\t" { "\\t" } else { delimeter }
56            ),
57        };
58        format!("Error in {}: {}.", self.path, error_msg)
59    }
60}
61
62#[derive(Debug)]
63pub enum StatisticsError {
64    FoundNANValue,
65    InvalidValue { value: f64 },
66}
67
68impl CustomError for StatisticsError {
69    fn msg(&self) -> String {
70        let error_msg = match &self {
71            StatisticsError::FoundNANValue => String::from("Found a NAN value"),
72            StatisticsError::InvalidValue { value } => format!("Found invalid value: {}", value),
73        };
74        format!("Statstical Error: {}.", error_msg)
75    }
76}