1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#![deny(missing_docs)]
#![doc(html_root_url = "https://docs.rs/transition/0.1.1")]

//! This crate allows to programmatically control LED light - blink(1).
//! The purpose is to be able to show the status of your task by "wrapping" all it's
//! execution between calls to this library.
//!
//! While your code is executing, the LED transition between two specified colors
//! indicating "pending" state. After your code finishes execution, you can notify the
//! LED about the status of your task, changing it's light to one of two
//! colors - depending on the status of your code.
//!
//! # Example
//!
//! ```rust
//! use transition::{Transition, Led};
//! use std::error::Error;
//! use std::thread;
//! use std::time::Duration;
//!
//! fn main() -> Result<(), Box<dyn Error>> {
//!     let notifier = Transition::new(&[Led::Blue, Led::Blank])? // pending state
//!         .on_success(&Led::Green)
//!         .on_failure(&Led::Red)
//!         .start()?;
//!
//!     // your code here, e.g.:
//!     thread::sleep(Duration::from_secs(2));
//!
//!     notifier.notify_success(); // or notifier.notify_failure();
//!     // LED changes color to the one specified for success
//!     Ok(())
//! }
//! ```

#[cfg(test)]
mod testutils;

mod color;
mod error;
mod msg;
mod notifier;
mod task;
mod transition;

pub use crate::color::Led;
pub use crate::transition::Transition;
pub use error::TransitionErr;
pub use notifier::Notifier;

#[cfg(test)]
mod test {
    use crate::testutils::utils::init_logging;
    use crate::Transition;
    use crate::TransitionErr;
    use doc_comment::doctest;
    use log::debug;
    use std::time::Duration;

    doctest!("../README.md");

    #[test]
    fn test_clone_of_transition() -> Result<(), TransitionErr> {
        init_logging();
        let transition = Transition::default();
        let other_transition = transition.clone();
        let notifier = transition.start()?;
        std::thread::sleep(Duration::from_millis(1000));
        notifier.notify_failure()?;

        let notifier = other_transition.start()?;
        std::thread::sleep(Duration::from_millis(1000));
        notifier.notify_success()?;

        Ok(())
    }

    #[test]
    fn test_debug_of_transition() {
        init_logging();
        let transition = Transition::default();
        debug!("testing Debug of transition: {:#?}", transition);
    }

    #[test]
    fn test_debug_of_notifier() -> Result<(), TransitionErr> {
        init_logging();
        let notifier = Transition::default().start()?;
        debug!("testing Debug of notifier: {:#?}", notifier);

        Ok(())
    }
}