Skip to content

Commit 5442ef8

Browse files
committed
refactor: move row id into display settings
1 parent a130133 commit 5442ef8

File tree

3 files changed

+15
-16
lines changed

3 files changed

+15
-16
lines changed

src/app.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ pub struct LogViewerApp {
3030
track_item_align: Option<Align>,
3131
shortcuts: Shortcuts,
3232
should_scroll_to_end_on_load: bool,
33-
// TODO 4: Add UI to set / unset row_idx_field_name
34-
/// When set adds a field with this name and populates it with the row numbers
35-
row_idx_field_name: Option<String>,
3633
/// Allows the user to dim the warning by clicking on it
3734
should_highlight_field_warning: bool,
3835

@@ -55,7 +52,6 @@ impl Default for LogViewerApp {
5552
track_item_align: Some(Align::Center),
5653
shortcuts: Default::default(),
5754
should_scroll_to_end_on_load: Default::default(),
58-
row_idx_field_name: Some("row#".to_string()),
5955
should_highlight_field_warning: true,
6056
should_focus_search: Default::default(),
6157
should_scroll: Default::default(),
@@ -309,7 +305,7 @@ impl LogViewerApp {
309305
LoadingStatus::Success(data) => {
310306
self.loading_status =
311307
// TODO 1: Make a copy of the loading type desired and match on it to get the data to load
312-
match Data::try_from((self.row_idx_field_name.as_ref(), &data[..])) {
308+
match Data::try_from((&self.data_display_options, &data[..])) {
313309
Ok(mut data) => {
314310
if let Some(old_data) = self.data.as_mut() {
315311
// Preserve settings across loads of the data

src/app/data.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@ use filter::{FieldSpecifier, FilterConfig};
99
use log::warn;
1010
use serde_json::Value;
1111

12-
use super::calculate_hash;
12+
use super::{calculate_hash, data_display_options::DataDisplayOptions};
1313
mod data_iter;
1414
pub mod filter;
1515

16-
type LogRowIdxFieldName<'a> = Option<&'a String>;
1716
type RowSlice<'a> = &'a [(String, String)];
1817

1918
#[derive(serde::Deserialize, serde::Serialize, Default, Debug, PartialEq, Eq)]
@@ -359,35 +358,34 @@ fn matching_fields(fields_and_values: RowSlice<'_>, filter: &FilterConfig) -> Op
359358
}
360359
}
361360

362-
impl TryFrom<(LogRowIdxFieldName<'_>, usize, &str)> for LogRow {
361+
impl TryFrom<(&DataDisplayOptions, usize, &str)> for LogRow {
363362
type Error = anyhow::Error;
364363

365364
fn try_from(
366-
(log_row_idx_key, row_idx_val, value): (LogRowIdxFieldName<'_>, usize, &str),
365+
(data_display_options, row_idx_val, value): (&DataDisplayOptions, usize, &str),
367366
) -> Result<Self, Self::Error> {
368367
let mut result = Self {
369368
data: serde_json::from_str(value)?,
370369
cached_display_list: None,
371370
};
372-
if let Some(key) = log_row_idx_key {
371+
if let Some(key) = data_display_options.row_idx_field_name.as_ref() {
373372
result.or_insert(key.to_string(), row_idx_val.into());
374373
}
375374
Ok(result)
376375
}
377376
}
378377

379-
impl TryFrom<(LogRowIdxFieldName<'_>, &str)> for Data {
378+
impl TryFrom<(&DataDisplayOptions, &str)> for Data {
380379
type Error = anyhow::Error;
381380

382381
fn try_from(
383-
(log_row_idx_key, value): (LogRowIdxFieldName<'_>, &str),
382+
(data_display_options, value): (&DataDisplayOptions, &str),
384383
) -> Result<Self, Self::Error> {
385384
let mut result = Data::default();
386385
for (i, line) in value.lines().enumerate() {
387-
result.rows.push(
388-
LogRow::try_from((log_row_idx_key, i, line))
389-
.with_context(|| format!("failed to parse line {}", i + 1))?,
390-
);
386+
let row = LogRow::try_from((data_display_options, i, line))
387+
.with_context(|| format!("failed to parse line {}", i + 1))?;
388+
result.rows.push(row);
391389
}
392390
Ok(result)
393391
}

src/app/data_display_options.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::collections::BTreeSet;
22

33
#[derive(serde::Deserialize, serde::Serialize, Debug, PartialEq, Eq)]
4+
#[serde(default)] // if we add new fields, give them default values when deserializing old state
45
pub struct DataDisplayOptions {
56
main_list_fields: Vec<String>,
67

@@ -11,6 +12,9 @@ pub struct DataDisplayOptions {
1112
///
1213
/// WARNING: This must be a valid index into the list as this is assumed in method implementations
1314
emphasize_if_matching_field_idx: Option<usize>,
15+
16+
/// When set adds a field with this name and populates it with the row numbers
17+
pub row_idx_field_name: Option<String>,
1418
}
1519

1620
impl DataDisplayOptions {
@@ -58,6 +62,7 @@ impl Default for DataDisplayOptions {
5862
.map(String::from)
5963
.collect(),
6064
emphasize_if_matching_field_idx: Some(2),
65+
row_idx_field_name: Some("row#".to_string()),
6166
}
6267
}
6368
}

0 commit comments

Comments
 (0)