From 26f5d37775bfdd8bdb5a04af5c9c0459c7e59ad8 Mon Sep 17 00:00:00 2001 From: One <43485962+c-git@users.noreply.github.com> Date: Tue, 6 Feb 2024 22:41:34 -0500 Subject: [PATCH] feat: convert string into our data format --- Cargo.lock | 24 ++++++++++++++++++++++++ Cargo.toml | 1 + src/app.rs | 39 ++++++++++++++++++++++++--------------- src/app/data.rs | 12 ++++++++++-- 4 files changed, 59 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 127a4f3..00f8892 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1731,6 +1731,12 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "itoa" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" + [[package]] name = "jni" version = "0.21.1" @@ -1863,6 +1869,7 @@ dependencies = [ "poll-promise", "rfd", "serde", + "serde_json", "tokio", "wasm-bindgen-futures", ] @@ -2535,6 +2542,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "ryu" +version = "1.0.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f98d2aa92eebf49b69786be48e4477826b256916e84a57ff2a4f21923b48eb4c" + [[package]] name = "same-file" version = "1.0.6" @@ -2576,6 +2589,17 @@ dependencies = [ "syn 2.0.48", ] +[[package]] +name = "serde_json" +version = "1.0.113" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69801b70b1c3dac963ecb03a364ba0ceda9cf60c71cfe475e99864759c8b8a79" +dependencies = [ + "itoa", + "ryu", + "serde", +] + [[package]] name = "serde_repr" version = "0.1.18" diff --git a/Cargo.toml b/Cargo.toml index 02fef8e..a55da83 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ egui_extras = "0.25.0" futures = "0.3.30" anyhow = "1.0.79" rfd = { version = "0.13.0", features = ["tokio"] } +serde_json = "1.0.113" # native: [target.'cfg(not(target_arch = "wasm32"))'.dependencies] diff --git a/src/app.rs b/src/app.rs index 75e3aec..c5be2e9 100644 --- a/src/app.rs +++ b/src/app.rs @@ -128,13 +128,15 @@ impl LogViewerApp { fn ui_loading(&mut self, ui: &mut egui::Ui) { match &self.loading_status { LoadingStatus::NotInProgress => { - if ui.button("📂 Open log file...").clicked() { - let ctx = ui.ctx().clone(); - self.loading_status = self.initiate_loading(ctx); - } - if ui.button("Clear Data").clicked() { - self.data = None; - } + ui.horizontal(|ui| { + if ui.button("📂 Open log file...").clicked() { + let ctx = ui.ctx().clone(); + self.loading_status = self.initiate_loading(ctx); + } + if ui.button("Clear Data").clicked() { + self.data = None; + } + }); } LoadingStatus::InProgress(promise) => { if promise.ready().is_some() { @@ -149,15 +151,22 @@ impl LogViewerApp { } } LoadingStatus::Failed(err_msg) => { - let msg = format!("Loading failed: {err_msg:?}"); + let msg = format!("Loading failed: {err_msg}"); + let msg = msg.replace(r"\n", "\n"); + let msg = msg.replace(r#"\""#, "\""); if ui.button("Clear Error Status").clicked() { self.loading_status = LoadingStatus::NotInProgress; } ui.label(msg); } LoadingStatus::Success(data) => { - dbg!(data); - self.loading_status = LoadingStatus::NotInProgress; + self.loading_status = match Data::try_from(&data[..]) { + Ok(data) => { + self.data = Some(data); + LoadingStatus::NotInProgress + } + Err(e) => LoadingStatus::Failed(format!("{e:?}")), + } } } } @@ -228,11 +237,11 @@ impl eframe::App for LogViewerApp { egui::CentralPanel::default().show(ctx, |ui| { // The central panel the region left after adding TopPanel's and SidePanel's - ui.horizontal(|ui| { - ui.heading("Log Viewer"); - ui.separator(); - self.ui_loading(ui); - }); + ui.heading("Log Viewer"); + ui.separator(); + self.ui_loading(ui); + ui.separator(); + StripBuilder::new(ui) .size(Size::remainder().at_least(100.0)) // for the table .size(Size::exact(100.0)) // for the details diff --git a/src/app/data.rs b/src/app/data.rs index 05fdc59..8ae6b98 100644 --- a/src/app/data.rs +++ b/src/app/data.rs @@ -1,3 +1,5 @@ +use anyhow::Context; + #[derive(serde::Deserialize, serde::Serialize, Default, Debug)] pub struct Data { rows: Vec, @@ -57,7 +59,13 @@ impl TryFrom<&str> for Data { type Error = anyhow::Error; fn try_from(value: &str) -> Result { - dbg!(value); - todo!() + let mut result = Data::default(); + for (i, line) in value.lines().enumerate() { + result.rows.push( + serde_json::from_str(line) + .with_context(|| format!("failed to parse line {}", i + 1))?, + ); + } + Ok(result) } }