Move config to own module
Signed-off-by: Marcel Müller <neikos@neikos.email>
This commit is contained in:
parent
082e162320
commit
68fbc3ce25
4 changed files with 43 additions and 41 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
|
@ -776,7 +776,7 @@ checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "plaixt"
|
name = "plaixt"
|
||||||
version = "0.0.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"camino",
|
"camino",
|
||||||
"clap",
|
"clap",
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ resolver = "2"
|
||||||
unsafe_code = "forbid"
|
unsafe_code = "forbid"
|
||||||
|
|
||||||
[workspace.package]
|
[workspace.package]
|
||||||
version = "0.0.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
description = "PLAIn teXT tools for your data"
|
description = "PLAIn teXT tools for your data"
|
||||||
license = "EUPL-1.2"
|
license = "EUPL-1.2"
|
||||||
|
|
|
||||||
39
crates/plaixt/src/config.rs
Normal file
39
crates/plaixt/src/config.rs
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
use camino::Utf8Path;
|
||||||
|
use camino::Utf8PathBuf;
|
||||||
|
use kdl::KdlDocument;
|
||||||
|
use miette::Context;
|
||||||
|
use miette::LabeledSpan;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Config {
|
||||||
|
pub(crate) root_folder: Utf8PathBuf,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn parse_config(path: &Utf8Path) -> miette::Result<Config> {
|
||||||
|
let data = tokio::fs::read_to_string(path)
|
||||||
|
.await
|
||||||
|
.map_err(|e| miette::miette!(e))
|
||||||
|
.wrap_err_with(|| miette::miette!("Could not read configuration at \"{path}\""))?;
|
||||||
|
|
||||||
|
let doc: KdlDocument = data
|
||||||
|
.parse()
|
||||||
|
.map_err(|e| miette::Error::from(e).with_source_code(data.clone()))?;
|
||||||
|
|
||||||
|
Ok(Config {
|
||||||
|
root_folder: doc
|
||||||
|
.get("root_folder")
|
||||||
|
.ok_or_else(|| miette::miette!("\"root_folder\" configuration value not found"))
|
||||||
|
.and_then(|val| {
|
||||||
|
val.get(0)
|
||||||
|
.and_then(|v| v.as_string().map(Into::into))
|
||||||
|
.ok_or_else(|| {
|
||||||
|
miette::diagnostic!(
|
||||||
|
labels = vec![LabeledSpan::new_primary_with_span(None, val.span())],
|
||||||
|
"root_folder is expected to be a path"
|
||||||
|
)
|
||||||
|
.into()
|
||||||
|
})
|
||||||
|
.map_err(|e: miette::Report| e.with_source_code(data))
|
||||||
|
})?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
@ -1,16 +1,13 @@
|
||||||
#![allow(dead_code)]
|
#![allow(dead_code)]
|
||||||
|
|
||||||
use camino::Utf8Path;
|
|
||||||
use camino::Utf8PathBuf;
|
use camino::Utf8PathBuf;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use clap::Subcommand;
|
use clap::Subcommand;
|
||||||
use clap::ValueHint;
|
use clap::ValueHint;
|
||||||
use human_panic::Metadata;
|
use human_panic::Metadata;
|
||||||
use kdl::KdlDocument;
|
|
||||||
use miette::LabeledSpan;
|
|
||||||
use miette::WrapErr;
|
|
||||||
use tracing::info;
|
use tracing::info;
|
||||||
|
|
||||||
|
mod config;
|
||||||
mod parsing;
|
mod parsing;
|
||||||
|
|
||||||
#[derive(Debug, Parser)]
|
#[derive(Debug, Parser)]
|
||||||
|
|
@ -35,11 +32,6 @@ enum ArgMode {
|
||||||
Dump,
|
Dump,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Config {
|
|
||||||
root_folder: Utf8PathBuf,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> miette::Result<()> {
|
async fn main() -> miette::Result<()> {
|
||||||
human_panic::setup_panic!(
|
human_panic::setup_panic!(
|
||||||
|
|
@ -51,7 +43,7 @@ async fn main() -> miette::Result<()> {
|
||||||
|
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
|
|
||||||
let config = parse_config(&args.config).await?;
|
let config = config::parse_config(&args.config).await?;
|
||||||
let root_folder = args.root_folder.as_ref().unwrap_or(&config.root_folder);
|
let root_folder = args.root_folder.as_ref().unwrap_or(&config.root_folder);
|
||||||
|
|
||||||
let load_records = async {
|
let load_records = async {
|
||||||
|
|
@ -69,32 +61,3 @@ async fn main() -> miette::Result<()> {
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn parse_config(path: &Utf8Path) -> miette::Result<Config> {
|
|
||||||
let data = tokio::fs::read_to_string(path)
|
|
||||||
.await
|
|
||||||
.map_err(|e| miette::miette!(e))
|
|
||||||
.wrap_err_with(|| miette::miette!("Could not read configuration at \"{path}\""))?;
|
|
||||||
|
|
||||||
let doc: KdlDocument = data
|
|
||||||
.parse()
|
|
||||||
.map_err(|e| miette::Error::from(e).with_source_code(data.clone()))?;
|
|
||||||
|
|
||||||
Ok(Config {
|
|
||||||
root_folder: doc
|
|
||||||
.get("root_folder")
|
|
||||||
.ok_or_else(|| miette::miette!("\"root_folder\" configuration value not found"))
|
|
||||||
.and_then(|val| {
|
|
||||||
val.get(0)
|
|
||||||
.and_then(|v| v.as_string().map(Into::into))
|
|
||||||
.ok_or_else(|| {
|
|
||||||
miette::diagnostic!(
|
|
||||||
labels = vec![LabeledSpan::new_primary_with_span(None, val.span())],
|
|
||||||
"root_folder is expected to be a path"
|
|
||||||
)
|
|
||||||
.into()
|
|
||||||
})
|
|
||||||
.map_err(|e: miette::Report| e.with_source_code(data))
|
|
||||||
})?,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue