Skip to content

Commit

Permalink
Revert "Replace Box<dyn ...> with a struct parameter"
Browse files Browse the repository at this point in the history
This reverts commit b58cdd0.
  • Loading branch information
jaytaph committed Nov 13, 2023
1 parent 371256b commit 1580d32
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 72 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ target/
*.swp*

# Local testing
local/
settings.db
local/
60 changes: 7 additions & 53 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::settings::{Setting, SettingInfo};
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;
use gosub_engine::config::{ConfigStore, StorageAdapter};

#[derive(Debug, Parser)]
#[clap(name = "Config-Store", version = "0.1.0", author = "Gosub")]
Expand Down Expand Up @@ -69,62 +69,16 @@ struct GlobalOpts {
path: String,
}

enum Config {
Sqlite(ConfigStore<SqliteStorageAdapter>),
Json(ConfigStore<JsonStorageAdapter>),
}

impl Config {
fn find(&self, search: &str) -> Vec<String> {
match self {
Self::Sqlite(store) => store.find(search),
Self::Json(store) => store.find(search),
}
}

fn has(&self, key: &str) -> bool {
match self {
Self::Sqlite(store) => store.has(key),
Self::Json(store) => store.has(key),
}
}

fn get(&mut self, key: &str) -> Setting {
match self {
Self::Sqlite(store) => store.get(key),
Self::Json(store) => store.get(key),
}
}

fn get_info(&self, key: &str) -> Option<SettingInfo> {
match self {
Self::Sqlite(store) => store.get_info(key),
Self::Json(store) => store.get_info(key),
}
}

fn set(&mut self, key: &str, value: Setting) {
match self {
Self::Sqlite(store) => store.set(key, value),
Self::Json(store) => store.set(key, value),
}
}
}

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

let mut store: Config = match args.global_opts.engine {
Engine::Sqlite => {
let store = SqliteStorageAdapter::new(args.global_opts.path.as_str());
Config::Sqlite(ConfigStore::new(store, true))
}
Engine::Json => {
let store = JsonStorageAdapter::new(args.global_opts.path.as_str());
Config::Json(ConfigStore::new(store, true))
}
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) {
Expand Down
32 changes: 15 additions & 17 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,22 @@ pub trait Store {
}

/// Configuration store is the place where the gosub engine can find all configurable options
pub struct ConfigStore<S> {
pub struct ConfigStore {
/// A hashmap of all settings so we can search o(1) time
settings: HashMap<String, Setting>,
/// A hashmap of all setting descriptions, default values and type information
settings_info: HashMap<String, SettingInfo>,
/// Keys of all settings so we can iterate keys easily
setting_keys: Vec<String>,
/// The storage adapter used for persisting and loading keys
storage: S,
storage: Box<dyn StorageAdapter>,
}

impl<S: StorageAdapter> ConfigStore<S> {
impl ConfigStore {
/// Creates a new store with the given storage adapter and preloads the store if needed
pub fn new(storage: S, preload: bool) -> Self {
// preload the settings if requested
let mut settings = HashMap::new();

if preload {
let all_settings = storage.get_all_settings();
for (key, value) in all_settings {
settings.insert(key, value);
}
}

pub fn new(storage: Box<dyn StorageAdapter>, preload: bool) -> Self {
let mut store = ConfigStore {
settings,
settings: HashMap::new(),
settings_info: HashMap::new(),
setting_keys: Vec::new(),
storage,
Expand All @@ -67,6 +57,14 @@ impl<S: StorageAdapter> ConfigStore<S> {
// Populate the settings from the json file
store.populate_settings();

// preload the settings if requested
if preload {
let all_settings = store.storage.get_all_settings();
for (key, value) in all_settings {
store.settings.insert(key, value);
}
}

store
}

Expand Down Expand Up @@ -173,7 +171,7 @@ mod test {

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

Expand All @@ -185,7 +183,7 @@ mod test {
#[test]
#[should_panic]
fn invalid_setting() {
let mut store = ConfigStore::new(MemoryStorageAdapter::new(), true);
let mut store = ConfigStore::new(Box::new(MemoryStorageAdapter::new()), true);
store.set(
"dns.local_resolver.enabled",
Setting::String("wont accept strings".into()),
Expand Down

0 comments on commit 1580d32

Please sign in to comment.