Skip to content

Commit

Permalink
feat: convert string into our data format
Browse files Browse the repository at this point in the history
  • Loading branch information
c-git committed Feb 7, 2024
1 parent 5a87e04 commit 26f5d37
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 17 deletions.
24 changes: 24 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
39 changes: 24 additions & 15 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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:?}")),
}
}
}
}
Expand Down Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions src/app/data.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use anyhow::Context;

#[derive(serde::Deserialize, serde::Serialize, Default, Debug)]
pub struct Data {
rows: Vec<LogRow>,
Expand Down Expand Up @@ -57,7 +59,13 @@ impl TryFrom<&str> for Data {
type Error = anyhow::Error;

fn try_from(value: &str) -> Result<Self, Self::Error> {
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)
}
}

0 comments on commit 26f5d37

Please sign in to comment.