Skip to main content

rtcom_core/
error.rs

1//! Error types for `rtcom-core`.
2//!
3//! The crate deliberately avoids [`anyhow`](https://docs.rs/anyhow) at library
4//! boundaries — callers (including `rtcom-cli`) need to match on specific
5//! failure domains to drive reconnection, user-visible diagnostics, and
6//! exit-code selection.
7
8use std::io;
9
10use thiserror::Error;
11
12/// Convenience alias for results returned by `rtcom-core` APIs.
13pub type Result<T> = std::result::Result<T, Error>;
14
15/// All fallible operations in `rtcom-core` funnel into this enum.
16///
17/// New variants may be added in minor releases; match with a trailing `_`
18/// arm when you care about forward-compatibility.
19#[derive(Debug, Error)]
20#[non_exhaustive]
21pub enum Error {
22    /// I/O error from the host OS, typically while reading from or writing to
23    /// the serial device.
24    #[error("serial I/O error: {0}")]
25    Io(#[from] io::Error),
26
27    /// Error reported by the underlying [`serialport`] / [`tokio_serial`]
28    /// backend (for example, port not found, busy, or unsupported setting).
29    #[error("serial backend error: {0}")]
30    Backend(#[from] serialport::Error),
31
32    /// The supplied [`SerialConfig`](crate::SerialConfig) value is invalid —
33    /// e.g. a baud rate of zero.
34    #[error("invalid serial configuration: {0}")]
35    InvalidConfig(String),
36
37    /// Another live process already owns the device, advertised by a
38    /// UUCP lock file. The error carries enough context to print a
39    /// useful diagnostic.
40    #[error("device {device} is locked by PID {pid} (lock file: {lock_file})")]
41    AlreadyLocked {
42        /// Device path the user asked us to open.
43        device: String,
44        /// PID found in the lock file.
45        pid: i32,
46        /// Path of the lock file we read.
47        lock_file: std::path::PathBuf,
48    },
49
50    /// A UUCP lock file exists but its content cannot be parsed as a
51    /// PID. The lock is treated as stale and removed.
52    #[error("invalid UUCP lock file: {0}")]
53    InvalidLock(String),
54}