rust_texas/
errors.rs

1use std::{error::Error, fmt::Display, io};
2
3/// Your garden-variety custom error.
4/// Contains the catch-all `WhatEven` variant (WhatEven as in "What even is this?")
5/// If y'all want more, please put up an issue.
6#[derive(Debug)]
7pub enum TexError {
8    ArgLen,
9    RankMismatch(u8, u8),
10    WhatEven(String),
11    TraitUnimplemented(String),
12    VariantUndefined,
13    LabelUndefined,
14    Undefined,
15    // #[cfg(feature = "markdown")]
16    // MarkdownError(String),
17    IoError(io::Error),
18}
19
20impl Display for TexError {
21    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22        write!(
23            f,
24            "{}",
25            match &self {
26                TexError::ArgLen => "Incorrect number of arguments.".to_string(),
27                TexError::RankMismatch(a,b) => format!("Rank mismatch: {a} < {b}."),
28                TexError::WhatEven(s) => s.to_string(),
29                TexError::Undefined => "Object not defined.".to_string(),
30                TexError::VariantUndefined => "The literal you provided does not correspond to a Variant. Please refer to the documentation for the list of valid literals.".to_string(),
31                TexError::LabelUndefined => "The label you provided does not exist.".to_string(),
32                TexError::TraitUnimplemented(s) => format!("{} does not implement the trait you desire (probably Populate).", s.to_string()),
33                // #[cfg(feature = "markdown")]
34                // TexError::MarkdownError(message) => message.to_string(),
35                TexError::IoError(e) => e.to_string()
36            }
37        )?;
38
39        Ok(())
40    }
41}
42
43impl Error for TexError {}
44
45impl From<io::Error> for TexError {
46    fn from(value: io::Error) -> Self {
47        TexError::IoError(value)
48    }
49}
50
51pub type TexResult<T> = Result<T, TexError>;