rusty_tip/
job.rs

1use crate::error::NanonisError;
2use std::time::Duration;
3
4/// A trait for long-running processes that can succeed, fail, or timeout
5///
6/// Jobs represent autonomous processes like tip controllers, scanning operations,
7/// monitoring loops, or any other task that runs for a duration and produces a result.
8pub trait Job {
9    /// The type returned on successful completion
10    type Output;
11
12    /// Run the job with a timeout
13    ///
14    /// Returns:
15    /// - `Ok(output)` if the job completes successfully
16    /// - `Err(NanonisError)` if the job fails or times out
17    fn run(&mut self, timeout: Duration) -> Result<Self::Output, NanonisError>;
18}
19
20#[cfg(test)]
21mod tests {
22    use super::*;
23
24    struct TestJob {
25        should_succeed: bool,
26    }
27
28    impl Job for TestJob {
29        type Output = String;
30
31        fn run(&mut self, _timeout: Duration) -> Result<Self::Output, NanonisError> {
32            if self.should_succeed {
33                Ok("success".to_string())
34            } else {
35                Err(NanonisError::Timeout)
36            }
37        }
38    }
39
40    #[test]
41    fn test_job_success() {
42        let mut job = TestJob {
43            should_succeed: true,
44        };
45        let result = job.run(Duration::from_secs(1)).unwrap();
46        assert_eq!(result, "success");
47    }
48
49    #[test]
50    fn test_job_failure() {
51        let mut job = TestJob {
52            should_succeed: false,
53        };
54        let result = job.run(Duration::from_secs(1));
55        assert!(result.is_err());
56    }
57}