thormotion/error/
sn.rs

1/*
2Project: thormotion
3GitHub: https://github.com/MillieFD/thormotion
4
5BSD 3-Clause License, Copyright (c) 2025, Amelia Fraser-Dale
6
7Redistribution and use in source and binary forms, with or without
8modification, are permitted provided that the conditions of the LICENSE are met.
9*/
10
11use std::fmt::{Debug, Display, Formatter};
12
13use nusb::DeviceInfo;
14
15type Sn = String;
16
17#[derive(Debug)]
18pub enum Error {
19    Invalid(Sn),
20    Multiple(Sn),
21    NotFound(Sn),
22    Unknown(DeviceInfo),
23}
24
25impl Display for Error {
26    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
27        match self {
28            Error::Invalid(sn) => write!(
29                f,
30                "{:?} is not a valid serial number for the requested Thorlabs device type.",
31                sn
32            ),
33            Error::Multiple(sn) => {
34                write!(f, "Multiple devices found with serial number {}", sn)
35            }
36            Error::NotFound(sn) => {
37                write!(f, "No devices found with serial number {}", sn)
38            }
39            Error::Unknown(dev) => {
40                write!(f, "Serial number could not be read from device {:?}", dev)
41            }
42        }
43    }
44}
45
46impl std::error::Error for Error {}
47
48#[cfg(feature = "py")]
49impl From<Error> for pyo3::PyErr {
50    fn from(error: Error) -> Self {
51        pyo3::exceptions::PyException::new_err(error.to_string())
52    }
53}