svg-filters & basic parser #15
5 changed files with 26 additions and 7 deletions
|
@ -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<PathBuf, Config> {
|
|||
}
|
||||
|
||||
impl Configs {
|
||||
pub fn read(p: PathBuf) -> Result<Self, Config> {
|
||||
pub fn read(p: PathBuf) -> Result<Self, error::Config> {
|
||||
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))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
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