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
94
95
use error::{Error, Result, Source};

/// Structure for storing a book option
///
/// This Enum might grow additional variants, so library users should
/// **not** count on exhaustive matching.
#[derive(Debug, PartialEq, Clone)]
pub enum BookOption {
    /// Stores a String
    String(String),

    /// Stores a boolean
    Bool(bool),

    /// Stores a single char
    Char(char),

    /// Stores an int
    Int(i32),

    /// Stores a float
    Float(f32),

    /// Stores a path
    ///
    /// Stored the same way as a string, but some base path is usually prepended to it
    Path(String),

    /// Hint that destructuring should not be exhaustive
    #[doc(hidden)]
    __NonExhaustive,
}

impl BookOption {
    /// Returns the BookOption as a &str, or an error if it isn't a `String` variant
    ///
    /// # Examples
    ///
    /// ```
    /// use crowbook::BookOption;
    ///
    /// let option = BookOption::String("foo".to_owned());
    /// assert_eq!(option.as_str(), Ok("foo"));
    ///
    /// let option = BookOption::Int(42);
    /// assert!(option.as_str().is_err());
    /// ```
    pub fn as_str(&self) -> Result<&str> {
        match *self {
            BookOption::String(ref s) => Ok(s),
            _ => Err(Error::book_option(Source::empty(), lformat!("{:?} is not a string", self))),
        }
    }

    /// Returns the BookOption as a &str, on an error if it isn't a `Path` variant.
    pub fn as_path(&self) -> Result<&str> {
        match *self {
            BookOption::Path(ref s) => Ok(s),
            _ => Err(Error::book_option(Source::empty(), lformat!("{:?} is not a path", self))),
        }
    }

    /// Retuns the BookOption as a bool, or an error if it isn't a `Bool` variant.
    pub fn as_bool(&self) -> Result<bool> {
        match *self {
            BookOption::Bool(b) => Ok(b),
            _ => Err(Error::book_option(Source::empty(), lformat!("{:?} is not a bool", self))),
        }
    }

    /// Returns the BookOption as a char, or an error if it isn't a `Char` variant.
    pub fn as_char(&self) -> Result<char> {
        match *self {
            BookOption::Char(c) => Ok(c),
            _ => Err(Error::book_option(Source::empty(), lformat!("{:?} is not a char", self))),
        }
    }

    /// Retuns the BookOption as an i32, or an error if it isn't an `Int` variant.
    pub fn as_i32(&self) -> Result<i32> {
        match *self {
            BookOption::Int(i) => Ok(i),
            _ => Err(Error::book_option(Source::empty(), lformat!("{:?} is not an i32", self))),

        }
    }

    /// Returns the BookOption as an f32, or an error if it isn't a `Float` variant.
    pub fn as_f32(&self) -> Result<f32> {
        match *self {
            BookOption::Float(f) => Ok(f),
            _ => Err(Error::book_option(Source::empty(), lformat!("{:?} is not a f32", self))),
        }
    }
}