autd3_core/link/
error.rs

1#[derive(Debug, PartialEq, Clone)]
2/// An error produced by the link.
3pub struct LinkError {
4    msg: String,
5}
6
7impl core::fmt::Display for LinkError {
8    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
9        write!(f, "{}", self.msg)
10    }
11}
12
13impl core::error::Error for LinkError {}
14
15impl LinkError {
16    /// Creates a new [`LinkError`].
17    #[must_use]
18    pub fn new(msg: impl ToString) -> LinkError {
19        LinkError {
20            msg: msg.to_string(),
21        }
22    }
23
24    /// Creates a new [`LinkError`] with a message indicating that the link is closed.
25    pub fn closed() -> LinkError {
26        LinkError::new("Link is closed")
27    }
28}
29
30impl From<std::io::Error> for LinkError {
31    fn from(err: std::io::Error) -> Self {
32        LinkError::new(err.to_string())
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39
40    #[test]
41    fn closed() {
42        let err = LinkError::closed();
43        assert_eq!("Link is closed", err.to_string());
44    }
45
46    #[test]
47    fn from_io_error() {
48        let io_err = std::io::Error::other("io error");
49        let link_err: LinkError = io_err.into();
50        assert_eq!("io error", link_err.to_string());
51    }
52}