test_data_generation/configs.rs
1//! The `configs` module provides functionality for the library to read configuration settings that the user can set in their implementation.
2//!
3//! # Examples
4//!
5//!
6//! Generate some demo test data ...
7//!
8//! ```
9//! extern crate test_data_generation;
10//!
11//! use test_data_generation::configs::Configs;
12//!
13//! fn main() {
14//! // initalize a new Configs
15//! let mut cfg = Configs::new(&String::from("./tests/config/tdg.yaml"));
16//! cfg.load_config_file();
17//!
18//! // verify the configuration file has been loaded
19//! println!("{:?}", cfg);
20//! }
21//! ```
22
23//use std::path::Path;
24use serde_json;
25use std::fs::File;
26use std::io::prelude::*;
27use yaml_rust::YamlLoader;
28
29#[derive(Serialize, Deserialize, Debug)]
30// Represents a Configs object that can be set by an implementation of the test data generation library
31pub struct Configs {
32 /// the file path of the test data generation library configuration file
33 file: String,
34}
35
36impl Configs {
37 /// Constructs a new Configs
38 ///
39 /// # Arguments
40 ///
41 /// * `path: &String - The full path name (including the file name and extension) to the configuration file.</br>
42 ///
43 /// #Example
44 ///
45 /// ```
46 /// extern crate test_data_generation;
47 ///
48 /// use test_data_generation::configs::Configs;
49 ///
50 /// fn main() {
51 /// // initalize a new Configs
52 /// let mut cfg = Configs::new(&String::from("./tests/config/tdg.yaml"));
53 /// cfg.load_config_file();
54 ///
55 /// // verify the configuration file has been loaded
56 /// println!("{:?}", cfg);
57 /// }
58 /// ```
59 pub fn new(path: &String) -> Configs {
60 let pth = path.to_string().to_owned();
61 Configs { file: pth }
62 }
63
64 /// Constructs a new Configs object from a serialized (JSON) string. This is used when restoring from "archive"
65 ///
66 /// #Example
67 ///
68 /// ```
69 /// extern crate test_data_generation;
70 ///
71 /// use test_data_generation::configs::Configs;
72 ///
73 /// fn main() {
74 /// let serialized = "{\"file\":\"./tests/config/tdg.yaml\"}";
75 /// let mut cfg = Configs::from_serialized(&serialized);
76 ///
77 /// assert_eq!(cfg.get_config_file_path(), "./tests/config/tdg.yaml");
78 /// }
79 /// ```
80 pub fn from_serialized(serialized: &str) -> Configs {
81 serde_json::from_str(&serialized).unwrap()
82 }
83
84 /// Loads the configuration file using the path that was provided during calling a new Configs object
85 ///
86 /// #Example
87 ///
88 /// ```
89 /// extern crate test_data_generation;
90 ///
91 /// use test_data_generation::configs::Configs;
92 ///
93 /// fn main() {
94 /// // initalize a new Configs
95 /// let mut cfg = Configs::new(&String::from("./tests/config/tdg.yaml"));
96 ///
97 /// // verify the configuration file path was set
98 /// println!("The configuration fiel is located at {}", cfg.get_config_file_path());
99 /// }
100 /// ```
101 pub fn get_config_file_path(&self) -> &str {
102 &self.file
103 }
104
105 /// Loads the configuration file using the path that was provided during calling a new Configs object
106 ///
107 /// #Example
108 ///
109 /// ```
110 /// extern crate test_data_generation;
111 ///
112 /// use test_data_generation::configs::Configs;
113 ///
114 /// fn main() {
115 /// // initalize a new Configs
116 /// let mut cfg = Configs::new(&String::from("./tests/config/tdg.yaml"));
117 /// cfg.load_config_file();
118 ///
119 /// // verify the configuration file has been loaded
120 /// println!("{:?}", cfg);
121 /// }
122 /// ```
123 pub fn load_config_file(&mut self) {
124 let mut f = File::open(&self.file).expect(&format!(
125 "Error: Configuration file not found at {}",
126 &self.file.to_string()
127 ));
128 let mut contents = String::new();
129 f.read_to_string(&mut contents)
130 .expect("Something went wrong reading file");
131 let _cfg_yaml =
132 &YamlLoader::load_from_str(&*contents).expect("failed to load YAML file")[0];
133 //println!("{:?}", cfg);
134 }
135
136 /// This function converts the Configs object to a serialize JSON string.
137 ///
138 /// #Example
139 ///
140 /// ```
141 /// extern crate test_data_generation;
142 ///
143 /// use test_data_generation::configs::Configs;
144 ///
145 /// fn main() {
146 /// //create a Configs object from a configuration file
147 /// let mut cfg = Configs::new(&String::from("./tests/config/tdg.yaml"));
148 /// cfg.load_config_file();
149 ///
150 /// println!("{}", cfg.serialize());
151 /// // {"key":"r","prior_key":null,"next_key":null,"pattern_placeholder":"c","starts_with":0,"ends_with":0,"index_offset":2}
152 /// }
153 ///
154 pub fn serialize(&mut self) -> String {
155 serde_json::to_string(&self).unwrap()
156 }
157}
158
159#[cfg(test)]
160mod tests {
161 use super::*;
162
163 #[test]
164 // ensure Configs reads a valid configuration file
165 fn create_config_good_cfg_file() {
166 let mut cfg = Configs::new(&String::from("./tests/config/tdg.yaml"));
167
168 cfg.load_config_file();
169 }
170
171 #[test]
172 #[should_panic(expected = "Error: Configuration file not found at ./badpath/tdg.yaml")]
173 // ensure Configs errors when reading an invalid configuration file
174 fn create_config_bad_cfg_file() {
175 let mut cfg = Configs::new(&String::from("./badpath/tdg.yaml"));
176
177 cfg.load_config_file();
178 }
179
180 #[test]
181 fn new_fact_from_serialized() {
182 let serialized = "{\"file\":\"./tests/config/tdg.yaml\"}";
183 let cfg = Configs::from_serialized(&serialized);
184
185 assert_eq!(cfg.get_config_file_path(), "./tests/config/tdg.yaml");
186 }
187
188 #[test]
189 // ensure a Configs object can be exported (to be archived) as JSON
190 fn serialize() {
191 let mut cfg = Configs::new(&String::from("./tests/config/tdg.yaml"));
192 cfg.load_config_file();
193
194 let serialized = cfg.serialize();
195 println!("serialized : {}", serialized);
196
197 assert_eq!(serialized, "{\"file\":\"./tests/config/tdg.yaml\"}");
198 }
199}