texcreate_lib/Config/
error.rs

1
2use std::error::Error;
3use std::fmt;
4use std::fmt::{Display, Formatter};
5
6/// Types of errors that can occur when creating a texcreate project:
7/// - Using beamer template and not using a beamer document class
8/// - Using an invalid template
9/// - Using an invalid document class
10/// - Empty project field
11/// - IO error (Uses std::io::Error)
12
13#[derive(Debug)]
14pub enum Errors {
15    BeamerError,
16    InvalidTemplateError(String),
17    InvalidDocumentClassError(String),
18    EmptyError(String),
19    IoError(std::io::Error),
20}
21// Implement From for Errors
22impl From<std::io::Error> for Errors {
23    fn from(i: std::io::Error) -> Self {
24        Errors::IoError(i)
25    }
26}
27
28
29// Implement Display for Errors
30impl Display for Errors {
31    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
32        match self {
33            Errors::BeamerError => write!(f, "ERROR: Using beamer template and not using a beamer document class"),
34            Errors::InvalidTemplateError(t) => {
35                let s = format!("ERROR: Using an invalid template: {}", t);
36                write!(f,"{}" ,&s)
37            },
38            Errors::InvalidDocumentClassError(d) => {
39                let s = format!("ERROR: Using an invalid document class: {}", d);
40                write!(f, "{}",&s)
41            },
42            Errors::EmptyError(s) => write!(f, "ERROR: Empty {} Field in config.toml", s),
43            Errors::IoError(e) => write!(f, "ERROR: {}", e.to_string()),
44        }
45    }
46}
47// Implement Error for Errors
48impl Error for Errors{
49    fn source(&self) -> Option<&(dyn Error + 'static)> {
50        match *self {
51            Errors::BeamerError => None,
52            Errors::InvalidTemplateError(_) => Some(self),
53            Errors::InvalidDocumentClassError(_) => Some(self),
54            Errors::EmptyError(_) => Some(self),
55            Errors::IoError(_) => Some(self),
56        }
57    }
58}
59// Check if a string is a valid document class
60pub fn invalid_class(class: &str)-> bool{
61    let valid = vec![
62        "article",
63        "IEEEtran",
64        "proc",
65        "minimal",
66        "report",
67        "book",
68        "slides",
69        "memoir",
70        "letter",
71        "beamer"
72    ];
73    if !valid.contains(&class){
74        true
75    } else {
76        false
77    }
78}
79