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
//! # rtcm
//!
//! An example project.

// Extra lints you may or may not want
#![deny(
    missing_copy_implementations,
    missing_debug_implementations,
    missing_docs,
    trivial_casts,
    trivial_numeric_casts,
    unsafe_code,
    unstable_features,
    unused_import_braces,
    unused_qualifications
)]

use std::fmt;

/// A specialized [`Result`] type for pointless operations.
pub type Result<T> = std::result::Result<T, NotFourError>;

/// The number wasn't four
#[derive(Debug, Clone, Copy)]
pub enum NotFourError {
    /// Not four
    Close,

    /// Sort of four
    NotClose,
}

impl std::fmt::Display for NotFourError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            NotFourError::Close => write!(f, "it's pretty close to four"),
            NotFourError::NotClose => write!(f, "it's definitely not four"),
        }
    }
}

impl std::error::Error for NotFourError {}

/// Do work.
///
/// ```
/// let result = rtcm::do_work(4).unwrap();
/// assert_eq!(result, "it's four");
/// ```
pub fn do_work(input: usize) -> Result<&'static str> {
    match input {
        n if n == four() => Ok("it's four"),
        3..=5 => Err(NotFourError::Close),
        _ => Err(NotFourError::NotClose),
    }
}

fn four() -> usize {
    2 + 2
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works_private() {
        assert_eq!(four(), 4);
    }
}