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
| use std::fs; use std::io::Read; use serde::Deserialize;
#[derive(Deserialize, Debug)] struct Config { mysql: MySqlConfig, redis: RedisConfig, }
#[derive(Deserialize, Debug)] struct MySqlConfig { host: String, port: u16, username: String, password: String, database: String, charset: String, collation: String, }
#[derive(Deserialize, Debug)] struct RedisConfig { host: String, port: u16, password: Option<String>, db: u8, prefix: String, options: RedisOptions, }
#[derive(Deserialize, Debug)] struct RedisOptions { abort_on_connect_fail: bool, read_timeout: u64, retry_interval: u64, retry_limit: u64, }
fn main() -> Result<(), Box<dyn std::error::Error>> { let mut file = fs::File::open("config.json")?; let mut contents = String::new(); file.read_to_string(&mut contents)?;
let config: Config = serde_json::from_str(&contents)?;
println!("MySQL Config:"); println!(" Host: {}", config.mysql.host); println!(" Port: {}", config.mysql.port); println!(" Username: {}", config.mysql.username); println!(" Password: {}", config.mysql.password); println!(" Database: {}", config.mysql.database); println!(" Charset: {}", config.mysql.charset); println!(" Collation: {}", config.mysql.collation);
println!("MySQL Config:"); println!(" Host: {}", config.redis.host); println!(" Port: {}", config.redis.port);
if let Some(password) = config.redis.password { println!(" Password: {}", password); } else { println!(" Password: <not set>"); }
println!(" Db: {}", config.redis.db); println!(" Prefix: {}", config.redis.prefix); println!(" abort_on_connect_fail: {}", config.redis.options.abort_on_connect_fail); println!(" read_timeout: {}", config.redis.options.read_timeout); println!(" retry_interval: {}", config.redis.options.retry_interval); println!(" retry_limit: {}", config.redis.options.retry_limit);
Ok(()) }
|