auto_diff/
err.rs

1use std::error::Error;
2use std::fmt;
3
4
5#[derive(Debug)]
6pub struct AutoDiffError {
7    details: String
8}
9
10impl AutoDiffError {
11    pub fn new(msg: &str) -> AutoDiffError {
12        AutoDiffError{
13            details: msg.to_string()
14        }
15    }
16}
17
18impl fmt::Display for AutoDiffError {
19    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
20        write!(f,"{}",self.details)
21    }
22}
23
24impl Error for AutoDiffError {
25    fn description(&self) -> &str {
26        &self.details
27    }
28}
29
30impl From<AutoDiffError> for std::fmt::Error {
31    fn from(item: AutoDiffError) -> std::fmt::Error {
32        std::fmt::Error::default()
33    }
34}
35
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    #[test]
42    fn test() {
43
44        fn return_err() -> Result<(), AutoDiffError> {
45            Err(AutoDiffError::new(&format!("{:?}", 12)))
46        }
47
48        let e = return_err();
49        assert!(e.is_err());
50
51    }
52}