visa_device_handler/visa_interface/
err.rs

1use std::convert::From;
2use std::error::Error as ErrorTrait;
3use std::fmt::{Display, Formatter, Result as FmtResult};
4use std::io::Error as IoError;
5
6///This is a library-specific error that is returned by all calls to all APIs.
7#[derive(Debug)]
8pub enum VisaWrapperError {
9    ///The library could not be opened.
10    OpeningLibraryError(IoError),
11    ///The symbol could not be obtained.
12    SymbolGettingError(IoError),
13    ///Value of the symbol was null.
14    NullSymbol,
15    ///Address could not be matched to a dynamic link library
16    PathNotMatchingLibrary(IoError),
17    ///Null Character in path name
18    NullCharacter,
19    ///Unsupported Target
20    UnsupportedPlatform,
21}
22
23impl Display for VisaWrapperError {
24    fn fmt(&self, f: &mut Formatter) -> FmtResult {
25        use self::VisaWrapperError::*;
26        f.write_str(&format!("{:?}", self))?;
27        match self {
28            OpeningLibraryError(msg) => {
29                f.write_str(": ")?;
30                msg.fmt(f)
31            }
32            SymbolGettingError(msg) => {
33                f.write_str(": ")?;
34                msg.fmt(f)
35            }
36            NullSymbol => f.write_str(": Symbol is Null."),
37            PathNotMatchingLibrary(msg) => {
38                f.write_str(": Path does not lead to a library.")?;
39                msg.fmt(f)
40            }
41            NullCharacter => f.write_str(": The path contains a null character."),
42            UnsupportedPlatform => f.write_str(": The target system is not supported by visa."),
43        }
44    }
45}
46
47impl From<dlopen2::Error> for VisaWrapperError {
48    fn from(value: dlopen2::Error) -> Self {
49        match value {
50            dlopen2::Error::NullCharacter(_) => VisaWrapperError::NullCharacter,
51            dlopen2::Error::OpeningLibraryError(e) => VisaWrapperError::OpeningLibraryError(e),
52            dlopen2::Error::SymbolGettingError(e) => VisaWrapperError::SymbolGettingError(e),
53            dlopen2::Error::NullSymbol => VisaWrapperError::NullSymbol,
54            dlopen2::Error::AddrNotMatchingDll(e) => VisaWrapperError::PathNotMatchingLibrary(e),
55        }
56    }
57}
58
59impl ErrorTrait for VisaWrapperError {
60    fn description(&self) -> &str {
61        use self::VisaWrapperError::*;
62        match *self {
63            OpeningLibraryError(_) => "Could not open library",
64            SymbolGettingError(_) => "Could not obtain symbol from the library",
65            NullSymbol => "The symbol is NULL",
66            PathNotMatchingLibrary(_) => "Address does not match any dynamic link library",
67            NullCharacter => "Uncategorized",
68            UnsupportedPlatform => "The target system is not supported by visa",
69        }
70    }
71
72    fn cause(&self) -> Option<&dyn ErrorTrait> {
73        use self::VisaWrapperError::*;
74        match self {
75            &OpeningLibraryError(_)
76            | &SymbolGettingError(_)
77            | &NullSymbol
78            | &PathNotMatchingLibrary(_)
79            | &NullCharacter
80            | &UnsupportedPlatform => None,
81        }
82    }
83}