Skip to content

Commit

Permalink
clippy / fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
jaytaph committed Nov 10, 2023
1 parent bdcc6ca commit 66b0164
Show file tree
Hide file tree
Showing 7 changed files with 278 additions and 282 deletions.
41 changes: 21 additions & 20 deletions src/bin/config-store.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use clap::{Parser, Subcommand};
use derive_more::Display;
use gosub_engine::config::{ConfigStore, StorageAdapter};
use gosub_engine::config::settings::Setting;
use gosub_engine::config::storage::json_storage::JsonStorageAdapter;
use gosub_engine::config::storage::sqlite_storage::SqliteStorageAdapter;
use gosub_engine::config::{ConfigStore, StorageAdapter};

#[derive(Debug, Parser)]
#[clap(name = "Config-Store", version = "0.1.0", author = "Gosub")]
Expand Down Expand Up @@ -56,31 +56,29 @@ impl std::str::FromStr for Engine {
}
}


#[derive(Debug, Parser)]
struct GlobalOpts {
#[clap(short = 'e', long = "engine", global = true, default_value = "sqlite")]
engine: Engine,
#[clap(short = 'p', long = "path", global = true, default_value = "settings.db")]
#[clap(
short = 'p',
long = "path",
global = true,
default_value = "settings.db"
)]
path: String,
}

fn main() {
let args = Cli::parse();

let storage_box: Box<dyn StorageAdapter>;
match args.global_opts.engine {
Engine::Sqlite => {
storage_box = Box::new(SqliteStorageAdapter::new(args.global_opts.path.as_str()));
}
Engine::Json => {
storage_box = Box::new(JsonStorageAdapter::new(args.global_opts.path.as_str()));
}
}
let storage_box: Box<dyn StorageAdapter> = match args.global_opts.engine {
Engine::Sqlite => Box::new(SqliteStorageAdapter::new(args.global_opts.path.as_str())),
Engine::Json => Box::new(JsonStorageAdapter::new(args.global_opts.path.as_str())),
};

let mut store = ConfigStore::new(storage_box, true);


match args.command {
Commands::View { key } => {
if !store.has(key.as_str()) {
Expand All @@ -92,23 +90,26 @@ fn main() {
let value = store.get(key.as_str(), None);

println!("Key : {}", key);
println!("Current Value : {}", value.to_string());
println!("Default Value : {}", info.default.to_string());
println!("Current Value : {}", value);
println!("Default Value : {}", info.default);
println!("Description : {}", info.description);
}
Commands::List => {
for key in store.find("*".into()) {
for key in store.find("*") {
let value = store.get(key.as_str(), None);
println!("{:40}: {}", key, value.to_string());
println!("{:40}: {}", key, value);
}
}
Commands::Set { key, value } => {
store.set(key.as_str(), Setting::from_string(value.as_str()).expect("incorrect value"));
store.set(
key.as_str(),
Setting::from_string(value.as_str()).expect("incorrect value"),
);
}
Commands::Search { key } => {
for key in store.find(key.as_str().into()) {
for key in store.find(key.as_str()) {
let value = store.get(key.as_str(), None);
println!("{:40}: {}", key, value.to_string());
println!("{:40}: {}", key, value);
}
}
}
Expand Down
32 changes: 16 additions & 16 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
pub mod storage;
pub mod settings;
pub mod storage;

use std::collections::HashMap;
use crate::config::settings::{Setting, SettingInfo};
use serde_derive::Deserialize;
use serde_json::Value;
use std::collections::HashMap;
use wildmatch::WildMatch;
use crate::config::settings::{Setting, SettingInfo};

const SETTINGS_JSON: &str = include_str!("./config/settings.json");

Expand Down Expand Up @@ -36,7 +36,7 @@ pub struct ConfigStore {
/// Keys of all settings so we can iterate keys easily
setting_keys: Vec<String>,
/// The storage adapter used for persisting and loading keys
storage: Box<dyn StorageAdapter>
storage: Box<dyn StorageAdapter>,
}

impl ConfigStore {
Expand All @@ -46,9 +46,9 @@ impl ConfigStore {
settings: HashMap::new(),
settings_info: HashMap::new(),
setting_keys: Vec::new(),
storage
storage,
};

// Populate the settings from the json file
store.populate_settings();

Expand Down Expand Up @@ -83,10 +83,7 @@ impl ConfigStore {
}

pub fn get_info(&self, key: &str) -> Option<SettingInfo> {
match self.settings_info.get(key) {
Some(info) => Some(info.clone()),
None => None
}
self.settings_info.get(key).cloned()
}

/// Returns the setting with the given key
Expand Down Expand Up @@ -125,15 +122,18 @@ impl ConfigStore {
let json_data = json_data.unwrap();
if let Value::Object(data) = json_data {
for (section_prefix, section_entries) in data.iter() {
let section_entries: Vec<JsonEntry> = serde_json::from_value(section_entries.clone()).expect("Failed to parse settings.json");
let section_entries: Vec<JsonEntry> =
serde_json::from_value(section_entries.clone())
.expect("Failed to parse settings.json");

for entry in section_entries {
let key = format!("{}.{}", section_prefix, entry.key);

let info = SettingInfo{
let info = SettingInfo {
key: key.clone(),
description: entry.description,
default: Setting::from_string(entry.default.as_str()).expect("cannot parse default setting"),
default: Setting::from_string(entry.default.as_str())
.expect("cannot parse default setting"),
last_accessed: 0,
};

Expand All @@ -148,17 +148,17 @@ impl ConfigStore {

#[cfg(test)]
mod test {
use crate::config::storage::sqlite_storage::SqliteStorageAdapter;
use super::*;
use crate::config::storage::memory_storage::MemoryStorageAdapter;

#[test]
fn test_config_store() {
let mut store = ConfigStore::new(Box::new(SqliteStorageAdapter::new("settings.db")), true);
let mut store = ConfigStore::new(Box::new(MemoryStorageAdapter::new()), true);
let setting = store.get("dns.local_resolver.enabled", None);
assert_eq!(setting, Setting::Bool(false));

store.set("dns.local_resolver.enabled", Setting::Bool(true));
let setting = store.get("dns.local_resolver.enabled", None);
assert_eq!(setting, Setting::Bool(true));
}
}
}
Loading

0 comments on commit 66b0164

Please sign in to comment.