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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use rust_i18n::t;
use crate::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)]
#[non_exhaustive]
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),
/// Stores a list of strings
StringVec(Vec<String>),
}
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(),
t!("error.no_string", s = format!("{:?}", self)),
)),
}
}
/// Returns the BookOption as a slice on a vector of strings
pub fn as_str_vec(&self) -> Result<&[String]> {
match *self {
BookOption::StringVec(ref v) => Ok(v),
_ => Err(Error::book_option(
Source::empty(),
t!("error.no_string_vector", s = format!("{:?}", 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(),
t!("error.no_path", s = format!("{:?}", self)),
)),
}
}
/// Returns 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(),
t!("error.no_bool", s = format!("{:?}", 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(),
t!("error.no_char", s = format!("{:?}", self)),
)),
}
}
/// Returns 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(),
t!("error.no_i32", s = format!("{:?}", 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(),
t!("error.no_f32", s = format!("{:?}", self)),
)),
}
}
}