kalgan_config/lib.rs
1//! Collection of functions to retrieve data and settings parameters defined in yaml files.
2
3pub use serde_yaml::Value;
4use std::collections::HashMap;
5mod parameters;
6
7#[derive(Debug)]
8/// The object that keeps the data collection.
9///
10/// This is the yaml file to be used in the following tests:
11/// ```yaml
12/// ## tests/settings.yaml
13///
14/// user:
15/// name: John
16/// is_real: false
17/// age: 39
18/// height: 1.78
19/// children:
20/// - Huey
21/// - Dewey
22/// - Louie
23/// ```
24pub struct Config {
25 pub collection: HashMap<String, Value>,
26}
27impl Config {
28 /// Creates and return the `Config` instance given the data source path (can be a file or a folder).
29 /// # Examples:
30 /// ```
31 /// use kalgan_config::Config;
32 ///
33 /// let config: Config = Config::new("tests/settings.yaml");
34 /// ```
35 pub fn new(source: &str) -> Config {
36 parameters::generate(source)
37 }
38 /// Returns the `serde_yaml::Value` for the given parameter.
39 /// # Examples:
40 /// ```
41 /// # use kalgan_config::Config;
42 /// # let config: Config = Config::new("tests/settings.yaml");
43 /// assert_eq!(config.get("user.name").unwrap(), "John");
44 /// ```
45 pub fn get(&self, key: &str) -> Result<Value, String> {
46 if self.exists(&key) {
47 Ok(self.collection[key].clone())
48 } else {
49 Err(format!("Key \"{}\" not found.", &key))
50 }
51 }
52 /// Returns the value as a `String` for the given parameter.
53 /// # Examples:
54 /// ```
55 /// # use kalgan_config::Config;
56 /// # let config: Config = Config::new("tests/settings.yaml");
57 /// assert_eq!(config.get_string("user.name").unwrap(), "John".to_string());
58 /// ```
59 pub fn get_string(&self, key: &str) -> Result<String, String> {
60 match self.get(&key)?.as_str() {
61 Some(string) => Ok(string.to_string()),
62 None => Err(format!("Value \"{}\" is not a string.", &key)),
63 }
64 }
65 /// Returns the value as a `bool` for the given parameter.
66 /// # Examples:
67 /// ```
68 /// # use kalgan_config::Config;
69 /// # let config: Config = Config::new("tests/settings.yaml");
70 /// assert_eq!(config.get_bool("user.is_real").unwrap(), false);
71 /// ```
72 pub fn get_bool(&self, key: &str) -> Result<bool, String> {
73 match self.get(&key)?.as_bool() {
74 Some(bool) => Ok(bool),
75 None => Err(format!("Value \"{}\" is not boolean.", &key)),
76 }
77 }
78 /// Returns the value as a `i64` for the given parameter.
79 /// # Examples:
80 /// ```
81 /// # use kalgan_config::Config;
82 /// # let config: Config = Config::new("tests/settings.yaml");
83 /// assert_eq!(config.get_number("user.age").unwrap(), 39);
84 /// ```
85 pub fn get_number(&self, key: &str) -> Result<i64, String> {
86 match self.get(&key)?.as_i64() {
87 Some(num) => Ok(num),
88 None => Err(format!("Value \"{}\" is not i64/u64.", &key)),
89 }
90 }
91 /// Returns the value as a `f64` for the given parameter.
92 /// # Examples:
93 /// ```
94 /// # use kalgan_config::Config;
95 /// # let config: Config = Config::new("tests/settings.yaml");
96 /// assert_eq!(config.get_float("user.height").unwrap(), 1.78);
97 /// ```
98 pub fn get_float(&self, key: &str) -> Result<f64, String> {
99 match self.get(&key)?.as_f64() {
100 Some(num) => Ok(num),
101 None => Err(format!("Value \"{}\" is not f64.", &key)),
102 }
103 }
104 /// Returns the value as a `Vec<serde_yaml::Value>` for the given parameter.
105 /// # Examples:
106 /// ```
107 /// # use kalgan_config::Config;
108 /// # let config: Config = Config::new("tests/settings.yaml");
109 /// assert_eq!(config.get_vec("user.children").unwrap(), vec!["Huey", "Dewey", "Louie"]);
110 /// ```
111 pub fn get_vec(&self, key: &str) -> Result<Vec<Value>, String> {
112 match self.get(&key)?.as_sequence() {
113 Some(sequence) => Ok(sequence.clone()),
114 None => Err(format!("Value \"{}\" is not a sequence.", &key)),
115 }
116 }
117 pub fn exists(&self, key: &str) -> bool {
118 self.collection.contains_key(&key.to_string())
119 }
120}