Skip to main content

rusty_tip/
error.rs

1use std::fmt;
2use std::time::Duration;
3
4use crate::NanonisError;
5
6/// Outcome of a successful tip controller run
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub enum RunOutcome {
9    /// Tip preparation completed (tip reached stable state)
10    Completed,
11    /// User requested shutdown (Ctrl+C or GUI stop)
12    StoppedByUser,
13}
14
15/// Error type for the tip controller
16#[derive(Debug)]
17pub enum Error {
18    /// Underlying Nanonis communication error
19    Nanonis(NanonisError),
20    /// Graceful shutdown was requested (internal, caught by run())
21    Shutdown,
22    /// Maximum cycle count exceeded
23    CycleLimit(u32),
24    /// Maximum duration exceeded
25    TimedOut(Duration),
26    /// Configuration validation error
27    Config(String),
28}
29
30impl From<NanonisError> for Error {
31    fn from(e: NanonisError) -> Self {
32        Error::Nanonis(e)
33    }
34}
35
36impl fmt::Display for Error {
37    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38        match self {
39            Error::Nanonis(e) => write!(f, "Nanonis error: {}", e),
40            Error::Shutdown => write!(f, "Shutdown requested"),
41            Error::CycleLimit(n) => {
42                write!(f, "Maximum cycle count ({}) exceeded", n)
43            }
44            Error::TimedOut(d) => {
45                write!(f, "Maximum duration ({:.0}s) exceeded", d.as_secs_f64())
46            }
47            Error::Config(msg) => write!(f, "Configuration error: {}", msg),
48        }
49    }
50}
51
52impl std::error::Error for Error {
53    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
54        match self {
55            Error::Nanonis(e) => Some(e),
56            _ => None,
57        }
58    }
59}