Skip to content

Commit

Permalink
refactor: Put the config in a static OnceCell
Browse files Browse the repository at this point in the history
This should make it easier in future to access the config from awkward
places. I think it makes sense to store it like this anyway since it
never makes sense to have more than one config.
  • Loading branch information
Piturnah committed Jul 19, 2023
1 parent a0ec429 commit e47913d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dirs = "5.0.1"
git2 = { version = "0.17.1", default-features = false }
itertools = "0.11.0"
nom = "7.1.1"
once_cell = "1.18.0"
paste = "1.0.12"
serde = { version = "1.0.167", features = [ "derive" ] }
serde_ignored = "0.1.8"
Expand Down
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ use std::{fs, path::PathBuf};

use anyhow::{Context, Result};
use clap::Parser;
use once_cell::sync::OnceCell;
use serde::Deserialize;

pub static CONFIG: OnceCell<Config> = OnceCell::new();

/// Command line args.
#[derive(Parser)]
#[command(version = env!("GEX_VERSION"), about)]
Expand Down
34 changes: 18 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use git2::Repository;

use crate::{
command::GexCommand,
config::Config,
config::{Config, CONFIG},
minibuffer::{MessageType, MiniBuffer},
render::Render,
};
Expand Down Expand Up @@ -96,22 +96,24 @@ fn run(clargs: &Clargs) -> Result<()> {

let mut minibuffer = MiniBuffer::new();

let config = Config::read_from_file(&clargs.config_file)
.unwrap_or_else(|e| {
minibuffer.push(&format!("{e:?}"), MessageType::Error);
Some((Config::default(), Vec::new()))
})
.map_or_else(Config::default, |(config, unused_keys)| {
if !unused_keys.is_empty() {
let mut warning = String::from("Unknown keys in config file:");
for key in unused_keys {
warning.push_str("\n ");
warning.push_str(&key);
let config = CONFIG.get_or_init(|| {
Config::read_from_file(&clargs.config_file)
.unwrap_or_else(|e| {
minibuffer.push(&format!("{e:?}"), MessageType::Error);
Some((Config::default(), Vec::new()))
})
.map_or_else(Config::default, |(config, unused_keys)| {
if !unused_keys.is_empty() {
let mut warning = String::from("Unknown keys in config file:");
for key in unused_keys {
warning.push_str("\n ");
warning.push_str(&key);
}
minibuffer.push(&warning, MessageType::Error);
}
minibuffer.push(&warning, MessageType::Error);
}
config
});
config
})
});

let status = Status::new(&repo, &config.options)?;
let branch_list = BranchList::new()?;
Expand Down

0 comments on commit e47913d

Please sign in to comment.