Skip to content

Commit

Permalink
Unflatten the Config/MainConfig struct
Browse files Browse the repository at this point in the history
This works around a known serde problem when wrong error messages
are generated with flattened fields.

See sharkdp#295 for details

Closes sharkdp#295
  • Loading branch information
eminence committed Jan 28, 2024
1 parent ca4df99 commit 6ac4aa7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 41 deletions.
52 changes: 23 additions & 29 deletions numbat-cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,6 @@ pub enum PrettyPrintMode {
Auto,
}

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "kebab-case", default, deny_unknown_fields)]
pub struct MainConfig {
pub intro_banner: IntroBanner,
pub prompt: String,
pub pretty_print: PrettyPrintMode,

#[serde(skip_serializing)]
pub load_prelude: bool,

#[serde(skip_serializing)]
pub load_user_init: bool,
}

impl Default for MainConfig {
fn default() -> Self {
Self {
prompt: ">>> ".to_owned(),
intro_banner: IntroBanner::default(),
pretty_print: PrettyPrintMode::Auto,
load_prelude: true,
load_user_init: true,
}
}
}

#[derive(Serialize, Deserialize, PartialEq, Eq, Default)]
#[serde(rename_all = "kebab-case")]
pub enum ExchangeRateFetchingPolicy {
Expand Down Expand Up @@ -72,10 +46,30 @@ impl Default for ExchangeRateConfig {
}
}

#[derive(Default, Serialize, Deserialize)]
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "kebab-case", default, deny_unknown_fields)]
pub struct Config {
#[serde(flatten)]
pub main: MainConfig,
pub intro_banner: IntroBanner,
pub prompt: String,
pub pretty_print: PrettyPrintMode,

#[serde(skip_serializing)]
pub load_prelude: bool,

#[serde(skip_serializing)]
pub load_user_init: bool,
pub exchange_rates: ExchangeRateConfig,
}

impl Default for Config {
fn default() -> Self {
Self {
prompt: ">>> ".to_owned(),
intro_banner: IntroBanner::default(),
pretty_print: PrettyPrintMode::Auto,
load_prelude: true,
load_user_init: true,
exchange_rates: Default::default(),
}
}
}
24 changes: 12 additions & 12 deletions numbat-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ impl Cli {
Config::default()
};

config.main.load_prelude &= !args.no_prelude;
config.main.load_user_init &= !(args.no_prelude || args.no_init);
config.load_prelude &= !args.no_prelude;
config.load_user_init &= !(args.no_prelude || args.no_init);

config.main.intro_banner = args.intro_banner.unwrap_or(config.main.intro_banner);
config.main.pretty_print = args.pretty_print.unwrap_or(config.main.pretty_print);
config.intro_banner = args.intro_banner.unwrap_or(config.intro_banner);
config.pretty_print = args.pretty_print.unwrap_or(config.pretty_print);

let mut fs_importer = FileSystemImporter::default();
for path in Self::get_modules_paths() {
Expand Down Expand Up @@ -165,7 +165,7 @@ impl Cli {
}

fn run(&mut self) -> Result<()> {
if self.config.main.load_prelude {
if self.config.load_prelude {
let result = self.parse_and_evaluate(
"use prelude",
CodeSource::Internal,
Expand All @@ -177,7 +177,7 @@ impl Cli {
}
}

if self.config.main.load_user_init {
if self.config.load_user_init {
let user_init_path = Self::get_config_path().join("init.nbt");

if let Ok(user_init_code) = fs::read_to_string(&user_init_path) {
Expand All @@ -193,7 +193,7 @@ impl Cli {
}
}

if self.config.main.load_prelude
if self.config.load_prelude
&& self.config.exchange_rates.fetching_policy != ExchangeRateFetchingPolicy::Never
{
self.context
Expand Down Expand Up @@ -221,7 +221,7 @@ impl Cli {
&code,
code_source,
ExecutionMode::Normal,
self.config.main.pretty_print,
self.config.pretty_print,
);

match result {
Expand All @@ -232,7 +232,7 @@ impl Cli {
}
}
} else {
let mut currency_fetch_thread = if self.config.main.load_prelude
let mut currency_fetch_thread = if self.config.load_prelude
&& self.config.exchange_rates.fetching_policy
== ExchangeRateFetchingPolicy::OnStartup
{
Expand Down Expand Up @@ -275,7 +275,7 @@ impl Cli {
rl.load_history(&history_path).ok();

if interactive {
match self.config.main.intro_banner {
match self.config.intro_banner {
IntroBanner::Long => {
println!();
println!(
Expand Down Expand Up @@ -313,7 +313,7 @@ impl Cli {
interactive: bool,
) -> Result<()> {
loop {
let readline = rl.readline(&self.config.main.prompt);
let readline = rl.readline(&self.config.prompt);
match readline {
Ok(line) => {
if !line.trim().is_empty() {
Expand Down Expand Up @@ -394,7 +394,7 @@ impl Cli {
} else {
ExecutionMode::Normal
},
self.config.main.pretty_print,
self.config.pretty_print,
);

match result {
Expand Down

0 comments on commit 6ac4aa7

Please sign in to comment.