From e7db9c38f390c86d1c7f61a59400b329e8a36b28 Mon Sep 17 00:00:00 2001 From: Schrottkatze Date: Thu, 11 Jan 2024 10:44:12 +0100 Subject: [PATCH] cli: work on basic cli and serde error hanlding --- Cargo.lock | 56 ++++++++++++++++++++++++++++++++ Cargo.toml | 5 ++- crates/app/Cargo.toml | 13 ++++++++ crates/app/src/main.rs | 73 ++++++++++++++++++++++++++++++++++++++++++ crates/rpl/Cargo.toml | 4 +-- 5 files changed, 148 insertions(+), 3 deletions(-) create mode 100644 crates/app/Cargo.toml create mode 100644 crates/app/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index b0b723c..7e1aaf8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -56,6 +56,27 @@ dependencies = [ "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]] name = "autocfg" version = "1.1.0" @@ -308,6 +329,12 @@ dependencies = [ "tiff", ] +[[package]] +name = "itoa" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" + [[package]] name = "jpeg-decoder" version = "0.3.0" @@ -462,6 +489,12 @@ dependencies = [ "serde", ] +[[package]] +name = "ryu" +version = "1.0.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f98d2aa92eebf49b69786be48e4477826b256916e84a57ff2a4f21923b48eb4c" + [[package]] name = "scopeguard" version = "1.2.0" @@ -488,6 +521,17 @@ dependencies = [ "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]] name = "simd-adler32" version = "0.3.7" @@ -543,6 +587,12 @@ version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +[[package]] +name = "unicode-width" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85" + [[package]] name = "utf8parse" version = "0.2.1" @@ -621,6 +671,12 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" +[[package]] +name = "yansi" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec" + [[package]] name = "zune-inflate" version = "0.2.54" diff --git a/Cargo.toml b/Cargo.toml index 6aa4943..4e17091 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -members = [ +members = [ "crates/app", "crates/executor", "crates/pl-cli", "crates/rpl" @@ -8,6 +8,9 @@ resolver = "2" [workspace.dependencies] clap = { version = "4", features = [ "derive" ] } +serde = { version = "1.0", features = [ "derive" ] } +ron = "0.8" +serde_json = "1.0" [lints.rust] unsafe_code = "deny" diff --git a/crates/app/Cargo.toml b/crates/app/Cargo.toml new file mode 100644 index 0000000..4f05ebc --- /dev/null +++ b/crates/app/Cargo.toml @@ -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" diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs new file mode 100644 index 0000000..a96ef3f --- /dev/null +++ b/crates/app/src/main.rs @@ -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> { + 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))); +} diff --git a/crates/rpl/Cargo.toml b/crates/rpl/Cargo.toml index e406f11..d125c78 100644 --- a/crates/rpl/Cargo.toml +++ b/crates/rpl/Cargo.toml @@ -6,5 +6,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -serde = { version = "1.0.193", features = [ "derive" ] } -ron = "0.8" +serde = { workspace = true, features = [ "derive" ] } +ron = { workspace = true }