cli: work on basic cli and serde error hanlding

This commit is contained in:
Schrottkatze 2024-01-11 10:44:12 +01:00
parent 1fc16e35eb
commit deb152da90
5 changed files with 148 additions and 3 deletions

56
Cargo.lock generated
View file

@ -56,6 +56,27 @@ dependencies = [
"windows-sys", "windows-sys",
] ]
[[package]]
name = "app"
version = "0.1.0"
dependencies = [
"ariadne",
"clap",
"ron",
"serde",
"serde_json",
]
[[package]]
name = "ariadne"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd002a6223f12c7a95cdd4b1cb3a0149d22d37f7a9ecdb2cb691a071fe236c29"
dependencies = [
"unicode-width",
"yansi",
]
[[package]] [[package]]
name = "autocfg" name = "autocfg"
version = "1.1.0" version = "1.1.0"
@ -308,6 +329,12 @@ dependencies = [
"tiff", "tiff",
] ]
[[package]]
name = "itoa"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c"
[[package]] [[package]]
name = "jpeg-decoder" name = "jpeg-decoder"
version = "0.3.0" version = "0.3.0"
@ -462,6 +489,12 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "ryu"
version = "1.0.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f98d2aa92eebf49b69786be48e4477826b256916e84a57ff2a4f21923b48eb4c"
[[package]] [[package]]
name = "scopeguard" name = "scopeguard"
version = "1.2.0" version = "1.2.0"
@ -488,6 +521,17 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "serde_json"
version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb0652c533506ad7a2e353cce269330d6afd8bdfb6d75e0ace5b35aacbd7b9e9"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]] [[package]]
name = "simd-adler32" name = "simd-adler32"
version = "0.3.7" version = "0.3.7"
@ -543,6 +587,12 @@ version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "unicode-width"
version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85"
[[package]] [[package]]
name = "utf8parse" name = "utf8parse"
version = "0.2.1" version = "0.2.1"
@ -621,6 +671,12 @@ version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04"
[[package]]
name = "yansi"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec"
[[package]] [[package]]
name = "zune-inflate" name = "zune-inflate"
version = "0.2.54" version = "0.2.54"

View file

@ -1,5 +1,5 @@
[workspace] [workspace]
members = [ members = [ "crates/app",
"crates/executor", "crates/executor",
"crates/pl-cli", "crates/pl-cli",
"crates/rpl" "crates/rpl"
@ -8,6 +8,9 @@ resolver = "2"
[workspace.dependencies] [workspace.dependencies]
clap = { version = "4", features = [ "derive" ] } clap = { version = "4", features = [ "derive" ] }
serde = { version = "1.0", features = [ "derive" ] }
ron = "0.8"
serde_json = "1.0"
[lints.rust] [lints.rust]
unsafe_code = "deny" unsafe_code = "deny"

13
crates/app/Cargo.toml Normal file
View file

@ -0,0 +1,13 @@
[package]
name = "app"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
clap = { workspace = true, features = [ "derive" ] }
serde = { workspace = true, features = [ "derive" ] }
ron = { workspace = true }
serde_json = { workspace = true }
ariadne = "0.4"

73
crates/app/src/main.rs Normal file
View file

@ -0,0 +1,73 @@
use crate::{config::Configs, error_reporting::report_serde_json_err};
mod cli {
use clap::{Parser, Subcommand};
#[derive(Parser)]
struct Args {
#[command(subcommand)]
command: Command,
}
#[derive(Clone, Subcommand)]
enum Command {}
}
mod config {
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct Configs<'a> {
example_value: i32,
example_string: &'a str,
}
impl Configs<'_> {
pub fn read_json(config_text: &str) -> serde_json::Result<Configs<'_>> {
serde_json::from_str(config_text)
}
}
}
mod error_reporting {
use std::{ops::Range, process};
use ariadne::{Report, Source};
pub fn report_serde_json_err(src: &str, err: serde_json::Error) -> ! {
use ariadne::{Color, ColorGenerator, Fmt, Label, Report, ReportKind, Source};
let offset = try_reconstruct_loc(src, err.line(), err.column());
Report::build(ariadne::ReportKind::Error, "test", offset)
.with_label(Label::new(("test", offset..offset)))
.with_message(err.to_string())
.finish()
.print(("test", Source::from(src)))
.unwrap();
process::exit(1);
}
fn try_reconstruct_loc(src: &str, line_nr: usize, col_nr: usize) -> usize {
let (line_nr, col_nr) = (line_nr - 1, col_nr - 1);
src.lines().enumerate().fold(0, |acc, (i, line)| {
if i < line_nr {
acc + line.len()
} else if i == line_nr {
acc + col_nr
} else {
acc
}
})
}
}
fn main() {
const TEST_JSON_CONFIG: &str = "{ \"example_value\": 42, \"example_string\": \"meow\" }";
const TEST_JSON_CONFIG_BROKEN: &str = "{
\"example_value\": \"42\",
\"example_string\": \"meow\"
}";
dbg!(Configs::read_json(TEST_JSON_CONFIG_BROKEN)
.map_err(|e| report_serde_json_err(TEST_JSON_CONFIG_BROKEN, e)));
}

View file

@ -6,5 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
serde = { version = "1.0.193", features = [ "derive" ] } serde = { workspace = true, features = [ "derive" ] }
ron = "0.8" ron = { workspace = true }