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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
// Copyright (c) 2018 atter developers // // Licensed under the Apache License, Version 2.0 // <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT // license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your // option. All files in the project carrying such notice may not be copied, // modified, or distributed except according to those terms. //! Configuration management for atter. //! //! # Examples //! //! ## Read from TOML string //! //! ``` //! # #[macro_use] extern crate error_chain; //! # extern crate atter; //! # extern crate toml; //! # //! # use atter::{read_toml, Config}; //! # use std::io::Cursor; //! # use std::path::PathBuf; //! # use std::time::Duration; //! # //! # mod error { //! # error_chain!{ //! # foreign_links { //! # Io(::std::io::Error); //! # Atter(::atter::Error); //! # TomlDe(::toml::de::Error); //! # TomlSer(::toml::ser::Error); //! # } //! # } //! # } //! # //! # fn main() { //! # read().expect("") //! # } //! # //! # fn read() -> error::Result<()> { //! let test_toml = r#"[common] //! base_db_path = "/Users/kon8116/projects/kon8116/atters" //! //! [[index]] //! idx_prefix = "MslCheckout" //! //! [[index.fields]] //! name = "id" //! data_type = "Integer" //! primary_key = true //! //! [[index.fields]] //! name = "correlation_id" //! data_type = "Text" //! not_null = true //! //! [[index]] //! idx_prefix = "ProxyApi" //! //! [[index.fields]] //! name = "id" //! data_type = "Integer" //! primary_key = true //! //! [[index.fields]] //! name = "correlation_id" //! data_type = "Text" //! not_null = true //! //! [prod] //! echo_env_affix = "-digital" //! verbose = false //! //! [prod.duration] //! secs = 900 //! nanos = 0 //! //! [stage] //! verbose = false //! //! [stage.duration] //! secs = 900 //! nanos = 0 //! //! [test] //! verbose = true //! //! [test.duration] //! secs = 900 //! nanos = 0 //! //! [dev] //! verbose = true //! //! [dev.duration] //! secs = 900 //! nanos = 0 //! "#; //! //! // Serialize the TOML above into a `Config` struct. //! let mut reader = Cursor::new(test_toml); //! let config = read_toml(&mut reader)?; //! //! // Check the `Config` struct. //! let common = config.common(); //! let index = config.index(); //! let prod = config.prod(); //! let stage = config.stage(); //! let test = config.test(); //! let dev = config.dev(); //! //! assert_eq!( //! common.base_db_path(), //! &PathBuf::from("/Users/kon8116/projects/kon8116/atters") //! ); //! assert_eq!(common.search_prefix(), "http://echo".to_string()); //! assert_eq!(common.search_affix(), ".kroger.com/elastic".to_string()); //! assert_eq!(common.search_suffix(), "/_search".to_string()); //! assert_eq!(common.db_prefix(), "ordmon".to_string()); //! assert_eq!(common.db_suffix(), ".db".to_string()); //! assert_eq!(index.len(), 2); //! //! # Ok(()) //! # } //! ``` //! //! ## Write to TOML string //! //! ``` //! # #[macro_use] extern crate error_chain; //! # extern crate atter; //! # extern crate toml; //! # //! # use atter::{write_toml, Common, Config, EchoIndex, Environment, Field, Index, SqliteDataType}; //! # use std::io::Cursor; //! # use std::path::PathBuf; //! # use std::time::Duration; //! # //! # mod error { //! # error_chain!{ //! # foreign_links { //! # Io(::std::io::Error); //! # Atter(::atter::Error); //! # TomlDe(::toml::de::Error); //! # TomlSer(::toml::ser::Error); //! # } //! # } //! # } //! # //! # const TEST_TOML: &str = r#"[common] //! # base_db_path = "/Users/kon8116/projects/kon8116/atters" //! # //! # [[index]] //! # idx_prefix = "MslCheckout" //! # //! # [[index.fields]] //! # name = "id" //! # data_type = "Integer" //! # primary_key = true //! # //! # [[index.fields]] //! # name = "correlation_id" //! # data_type = "Text" //! # not_null = true //! # //! # [[index]] //! # idx_prefix = "ProxyApi" //! # //! # [[index.fields]] //! # name = "id" //! # data_type = "Integer" //! # primary_key = true //! # //! # [[index.fields]] //! # name = "correlation_id" //! # data_type = "Text" //! # not_null = true //! # //! # [prod] //! # echo_env_affix = "-digital" //! # verbose = false //! # //! # [prod.duration] //! # secs = 900 //! # nanos = 0 //! # //! # [stage] //! # verbose = false //! # //! # [stage.duration] //! # secs = 900 //! # nanos = 0 //! # //! # [test] //! # verbose = true //! # //! # [test.duration] //! # secs = 900 //! # nanos = 0 //! # //! # [dev] //! # verbose = true //! # //! # [dev.duration] //! # secs = 900 //! # nanos = 0 //! # "#; //! # //! # fn main() { //! # write().expect("unable to write"); //! # } //! # //! # fn write() -> error::Result<()> { //! // Setup the `Config` struct. //! let mut config: Config = Default::default(); //! let mut common: Common = Default::default(); //! let mut msl_checkout_index: Index = Default::default(); //! let mut proxy_api_index: Index = Default::default(); //! let mut prod: Environment = Default::default(); //! let mut stage: Environment = Default::default(); //! let mut test: Environment = Default::default(); //! let mut dev: Environment = Default::default(); //! //! common.set_base_db_path(PathBuf::from("/Users/kon8116/projects/kon8116/atters")); //! //! let mut id: Field = Default::default(); //! id.set_name("id".to_string()); //! id.set_primary_key(Some(true)); //! //! let mut correlation_id: Field = Default::default(); //! correlation_id.set_name("correlation_id".to_string()); //! correlation_id.set_data_type(SqliteDataType::Text); //! correlation_id.set_not_null(Some(true)); //! //! msl_checkout_index.set_fields(vec![id.clone(), correlation_id.clone()]); //! //! proxy_api_index.set_idx_prefix(EchoIndex::ProxyApi); //! proxy_api_index.set_fields(vec![id, correlation_id]); //! //! prod.set_echo_env_affix(Some("-digital".to_string())); //! prod.set_verbose(false); //! prod.set_duration(Duration::from_millis(900000)); //! //! stage.set_verbose(false); //! stage.set_duration(Duration::from_millis(900000)); //! //! test.set_verbose(true); //! test.set_duration(Duration::from_millis(900000)); //! //! dev.set_verbose(true); //! dev.set_duration(Duration::from_millis(900000)); //! //! config.set_common(common); //! config.set_index(vec![msl_checkout_index, proxy_api_index]); //! config.set_prod(prod); //! config.set_stage(stage); //! config.set_test(test); //! config.set_dev(dev); //! //! // Write the TOML to the given buf. //! let mut buf = [0; 5000]; //! //! // Wrapped to drop mutable borrow. //! { //! let mut writer = Cursor::new(&mut buf[..]); //! write_toml(&config, &mut writer)?; //! } //! //! // Check that the result is the same as the TOML above. //! let filtered = buf.iter().filter(|x| **x > 0).cloned().collect::<Vec<u8>>(); //! assert_eq!( //! TEST_TOML, //! String::from_utf8(filtered).expect("Invalid UTF-8 in result") //! ); //! # Ok(()) //! # } //! ``` //! #![feature(try_from)] #[macro_use] extern crate error_chain; #[macro_use] extern crate getset; #[macro_use] extern crate serde_derive; #[cfg(test)] extern crate bincode; extern crate chrono; extern crate regex; extern crate rusqlite; extern crate toml; extern crate url; extern crate uuid; mod config; mod dal; mod error; mod message; pub use config::{ db_path, echo_url, read_toml, scroll_url, write_toml, Common, Config, EchoIndex, Environment, Field, Index, RuntimeEnvironment, SqliteDataType, }; pub use dal::sqlite::{find_most_recent, initialize}; pub use error::Error; pub use message::Message;