use std::io;
use thiserror::Error;
use crate::{model::Device, state::StateError};
#[derive(Error, Debug)]
pub enum InstrumentError {
#[error("Connection error, couldn't connect to controller from device {:?}", 0)]
ConnectionError(Device),
#[error("Timeout error: Modbus device on port {port}, slave addr {addr} timed out after request to register 0x{register:X}")]
ModbusTimeoutError {
port: String,
addr: u8,
register: u16,
},
#[error("IO Error: {0}")]
IOError(io::Error),
#[error("addr {addr:?}: {msg}")]
ModbusError { msg: String, addr: Option<u8> },
#[error("addr {addr:?}: {msg}")]
SerialError { msg: String, addr: Option<u8> },
#[error("State Error: {0:?}")]
StateError(StateError),
}
impl InstrumentError {
pub fn modbusTimeoutError(port: &str, addr: u8, register: u16) -> Self {
Self::ModbusTimeoutError {
port: port.to_string(),
addr,
register,
}
}
pub fn serialError(msg: String, addr: Option<u8>) -> Self {
Self::SerialError { msg, addr }
}
pub fn modbusError(msg: String, addr: Option<u8>) -> Self {
Self::ModbusError { msg, addr }
}
}
impl From<io::Error> for InstrumentError {
fn from(e: io::Error) -> Self {
Self::IOError(e)
}
}