Skip to main content

conspire/math/assert/error/
mod.rs

1#[cfg(test)]
2mod test;
3
4use crate::math::{TensorError, defeat_message};
5use std::{
6    fmt::{self, Debug, Formatter},
7    io::Error as ErrorIO,
8};
9
10/// An error produced by a failed assertion, comparing two values.
11pub struct AssertionError {
12    pub message: String,
13}
14
15impl Debug for AssertionError {
16    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
17        write!(
18            f,
19            "{}\n\x1b[0;2;31m{}\x1b[0m\n",
20            self.message,
21            defeat_message()
22        )
23    }
24}
25
26impl From<String> for AssertionError {
27    fn from(error: String) -> Self {
28        Self { message: error }
29    }
30}
31
32impl From<&str> for AssertionError {
33    fn from(error: &str) -> Self {
34        Self {
35            message: error.to_string(),
36        }
37    }
38}
39
40impl From<TensorError> for AssertionError {
41    fn from(error: TensorError) -> Self {
42        Self {
43            message: error.to_string(),
44        }
45    }
46}
47
48impl From<ErrorIO> for AssertionError {
49    fn from(error: ErrorIO) -> Self {
50        Self {
51            message: error.to_string(),
52        }
53    }
54}