Move config to own module

Signed-off-by: Marcel Müller <neikos@neikos.email>
This commit is contained in:
Marcel Müller 2025-02-01 11:47:40 +01:00
parent 082e162320
commit 68fbc3ce25
4 changed files with 43 additions and 41 deletions

2
Cargo.lock generated
View file

@ -776,7 +776,7 @@ checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2"
[[package]]
name = "plaixt"
version = "0.0.0"
version = "0.1.0"
dependencies = [
"camino",
"clap",

View file

@ -8,7 +8,7 @@ resolver = "2"
unsafe_code = "forbid"
[workspace.package]
version = "0.0.0"
version = "0.1.0"
edition = "2021"
description = "PLAIn teXT tools for your data"
license = "EUPL-1.2"

View 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))
})?,
})
}

View file

@ -1,16 +1,13 @@
#![allow(dead_code)]
use camino::Utf8Path;
use camino::Utf8PathBuf;
use clap::Parser;
use clap::Subcommand;
use clap::ValueHint;
use human_panic::Metadata;
use kdl::KdlDocument;
use miette::LabeledSpan;
use miette::WrapErr;
use tracing::info;
mod config;
mod parsing;
#[derive(Debug, Parser)]
@ -35,11 +32,6 @@ enum ArgMode {
Dump,
}
#[derive(Debug)]
pub struct Config {
root_folder: Utf8PathBuf,
}
#[tokio::main]
async fn main() -> miette::Result<()> {
human_panic::setup_panic!(
@ -51,7 +43,7 @@ async fn main() -> miette::Result<()> {
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 load_records = async {
@ -69,32 +61,3 @@ async fn main() -> miette::Result<()> {
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))
})?,
})
}