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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
//! The `configs` module provides functionality for the library to read configuration settings that the user can set in their implementation.
//!
//! # Examples
//!
//!
//! Generate some demo test data ...
//!
//! ```
//! extern crate test_data_generation;
//!
//! use test_data_generation::configs::Configs;
//!
//! fn main() {
//! // initalize a new Configs
//! let mut cfg = Configs::new(&String::from("./tests/config/tdg.yaml"));
//! cfg.load_config_file();
//!
//! // verify the configuration file has been loaded
//! println!("{:?}", cfg);
//! }
//! ```
//use std::path::Path;
use serde_json;
use std::fs::File;
use std::io::prelude::*;
use yaml_rust::YamlLoader;
#[derive(Serialize, Deserialize, Debug)]
// Represents a Configs object that can be set by an implementation of the test data generation library
pub struct Configs {
/// the file path of the test data generation library configuration file
file: String,
}
impl Configs {
/// Constructs a new Configs
///
/// # Arguments
///
/// * `path: &String - The full path name (including the file name and extension) to the configuration file.</br>
///
/// #Example
///
/// ```
/// extern crate test_data_generation;
///
/// use test_data_generation::configs::Configs;
///
/// fn main() {
/// // initalize a new Configs
/// let mut cfg = Configs::new(&String::from("./tests/config/tdg.yaml"));
/// cfg.load_config_file();
///
/// // verify the configuration file has been loaded
/// println!("{:?}", cfg);
/// }
/// ```
pub fn new(path: &String) -> Configs {
let pth = path.to_string().to_owned();
Configs { file: pth }
}
/// Constructs a new Configs object from a serialized (JSON) string. This is used when restoring from "archive"
///
/// #Example
///
/// ```
/// extern crate test_data_generation;
///
/// use test_data_generation::configs::Configs;
///
/// fn main() {
/// let serialized = "{\"file\":\"./tests/config/tdg.yaml\"}";
/// let mut cfg = Configs::from_serialized(&serialized);
///
/// assert_eq!(cfg.get_config_file_path(), "./tests/config/tdg.yaml");
/// }
/// ```
pub fn from_serialized(serialized: &str) -> Configs {
serde_json::from_str(&serialized).unwrap()
}
/// Loads the configuration file using the path that was provided during calling a new Configs object
///
/// #Example
///
/// ```
/// extern crate test_data_generation;
///
/// use test_data_generation::configs::Configs;
///
/// fn main() {
/// // initalize a new Configs
/// let mut cfg = Configs::new(&String::from("./tests/config/tdg.yaml"));
///
/// // verify the configuration file path was set
/// println!("The configuration fiel is located at {}", cfg.get_config_file_path());
/// }
/// ```
pub fn get_config_file_path(&self) -> &str {
&self.file
}
/// Loads the configuration file using the path that was provided during calling a new Configs object
///
/// #Example
///
/// ```
/// extern crate test_data_generation;
///
/// use test_data_generation::configs::Configs;
///
/// fn main() {
/// // initalize a new Configs
/// let mut cfg = Configs::new(&String::from("./tests/config/tdg.yaml"));
/// cfg.load_config_file();
///
/// // verify the configuration file has been loaded
/// println!("{:?}", cfg);
/// }
/// ```
pub fn load_config_file(&mut self) {
let mut f = File::open(&self.file).expect(&format!(
"Error: Configuration file not found at {}",
&self.file.to_string()
));
let mut contents = String::new();
f.read_to_string(&mut contents)
.expect("Something went wrong reading file");
let _cfg_yaml =
&YamlLoader::load_from_str(&*contents).expect("failed to load YAML file")[0];
//println!("{:?}", cfg);
}
/// This function converts the Configs object to a serialize JSON string.
///
/// #Example
///
/// ```
/// extern crate test_data_generation;
///
/// use test_data_generation::configs::Configs;
///
/// fn main() {
/// //create a Configs object from a configuration file
/// let mut cfg = Configs::new(&String::from("./tests/config/tdg.yaml"));
/// cfg.load_config_file();
///
/// println!("{}", cfg.serialize());
/// // {"key":"r","prior_key":null,"next_key":null,"pattern_placeholder":"c","starts_with":0,"ends_with":0,"index_offset":2}
/// }
///
pub fn serialize(&mut self) -> String {
serde_json::to_string(&self).unwrap()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
// ensure Configs reads a valid configuration file
fn create_config_good_cfg_file() {
let mut cfg = Configs::new(&String::from("./tests/config/tdg.yaml"));
cfg.load_config_file();
}
#[test]
#[should_panic(expected = "Error: Configuration file not found at ./badpath/tdg.yaml")]
// ensure Configs errors when reading an invalid configuration file
fn create_config_bad_cfg_file() {
let mut cfg = Configs::new(&String::from("./badpath/tdg.yaml"));
cfg.load_config_file();
}
#[test]
fn new_fact_from_serialized() {
let serialized = "{\"file\":\"./tests/config/tdg.yaml\"}";
let cfg = Configs::from_serialized(&serialized);
assert_eq!(cfg.get_config_file_path(), "./tests/config/tdg.yaml");
}
#[test]
// ensure a Configs object can be exported (to be archived) as JSON
fn serialize() {
let mut cfg = Configs::new(&String::from("./tests/config/tdg.yaml"));
cfg.load_config_file();
let serialized = cfg.serialize();
println!("serialized : {}", serialized);
assert_eq!(serialized, "{\"file\":\"./tests/config/tdg.yaml\"}");
}
}