From 8a541546d9ddac4fa95aca958d58a2835deb69e5 Mon Sep 17 00:00:00 2001 From: Schrottkatze Date: Sun, 28 Apr 2024 13:22:04 +0200 Subject: [PATCH] app: fix error_reporting not being used --- crates/app/src/config/config_file.rs | 18 +++++++++++++----- crates/app/src/error_reporting.rs | 2 +- testfiles/config.json | 4 ++++ testfiles/config.ron | 4 ++++ testfiles/test.owo | 5 ++++- 5 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 testfiles/config.json create mode 100644 testfiles/config.ron diff --git a/crates/app/src/config/config_file.rs b/crates/app/src/config/config_file.rs index 15f660c..7078d32 100644 --- a/crates/app/src/config/config_file.rs +++ b/crates/app/src/config/config_file.rs @@ -5,7 +5,9 @@ use std::{ use serde::{Deserialize, Serialize}; -use super::error::Config; +use crate::error_reporting::{report_serde_json_err, report_serde_ron_err}; + +use super::error::{self, Config}; #[derive(Debug, Serialize, Deserialize)] pub struct Configs { @@ -40,14 +42,20 @@ pub(super) fn find_config_file() -> Result { } impl Configs { - pub fn read(p: PathBuf) -> Result { + pub fn read(p: PathBuf) -> Result { match p .extension() .map(|v| v.to_str().expect("config path to be UTF-8")) { - Some("ron") => Ok(serde_json::from_str(&fs::read_to_string(p)?)?), - Some("json") => Ok(ron::from_str(&fs::read_to_string(p)?)?), - e => Err(Config::UnknownExtension(e.map(str::to_owned))), + Some("ron") => { + let f = fs::read_to_string(p)?; + ron::from_str(&f).or_else(|e| report_serde_ron_err(&f, &e)) + } + Some("json") => { + let f = fs::read_to_string(p)?; + serde_json::from_str(&f).or_else(|e| report_serde_json_err(&f, &e)) + } + e => Err(error::Config::UnknownExtension(e.map(str::to_owned))), } } } diff --git a/crates/app/src/error_reporting.rs b/crates/app/src/error_reporting.rs index bc4c44b..3981b17 100644 --- a/crates/app/src/error_reporting.rs +++ b/crates/app/src/error_reporting.rs @@ -25,7 +25,7 @@ fn report_serde_err(src: &str, line: usize, col: usize, msg: String) -> ! { .finish() .eprint(("test", Source::from(src))) .expect("writing error to stderr failed"); - process::exit(1); + process::exit(1) } /// Reconstruct a byte offset from the line + column numbers typical from serde crates diff --git a/testfiles/config.json b/testfiles/config.json new file mode 100644 index 0000000..3456c5c --- /dev/null +++ b/testfiles/config.json @@ -0,0 +1,4 @@ +{ + "example_value": "42", + "no_startup_message": true +} \ No newline at end of file diff --git a/testfiles/config.ron b/testfiles/config.ron new file mode 100644 index 0000000..7b47189 --- /dev/null +++ b/testfiles/config.ron @@ -0,0 +1,4 @@ +( + example_value: 42 + no_startup_message: false, +) diff --git a/testfiles/test.owo b/testfiles/test.owo index d81cc07..51f07d1 100644 --- a/testfiles/test.owo +++ b/testfiles/test.owo @@ -1 +1,4 @@ -42 +generator | { + foo, + bar +} |