forked from katzen-cafe/iowo
app: fix error_reporting not being used
This commit is contained in:
parent
4df0118aa4
commit
8a541546d9
5 changed files with 26 additions and 7 deletions
|
@ -5,7 +5,9 @@ use std::{
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
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)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
pub struct Configs {
|
pub struct Configs {
|
||||||
|
@ -40,14 +42,20 @@ pub(super) fn find_config_file() -> Result<PathBuf, Config> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Configs {
|
impl Configs {
|
||||||
pub fn read(p: PathBuf) -> Result<Self, Config> {
|
pub fn read(p: PathBuf) -> Result<Self, error::Config> {
|
||||||
match p
|
match p
|
||||||
.extension()
|
.extension()
|
||||||
.map(|v| v.to_str().expect("config path to be UTF-8"))
|
.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("ron") => {
|
||||||
Some("json") => Ok(ron::from_str(&fs::read_to_string(p)?)?),
|
let f = fs::read_to_string(p)?;
|
||||||
e => Err(Config::UnknownExtension(e.map(str::to_owned))),
|
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))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,7 @@ fn report_serde_err(src: &str, line: usize, col: usize, msg: String) -> ! {
|
||||||
.finish()
|
.finish()
|
||||||
.eprint(("test", Source::from(src)))
|
.eprint(("test", Source::from(src)))
|
||||||
.expect("writing error to stderr failed");
|
.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
|
/// Reconstruct a byte offset from the line + column numbers typical from serde crates
|
||||||
|
|
4
testfiles/config.json
Normal file
4
testfiles/config.json
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
{
|
||||||
|
"example_value": "42",
|
||||||
|
"no_startup_message": true
|
||||||
|
}
|
4
testfiles/config.ron
Normal file
4
testfiles/config.ron
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
(
|
||||||
|
example_value: 42
|
||||||
|
no_startup_message: false,
|
||||||
|
)
|
|
@ -1 +1,4 @@
|
||||||
42
|
generator | {
|
||||||
|
foo,
|
||||||
|
bar
|
||||||
|
} |
|
||||||
|
|
Loading…
Reference in a new issue