From 8a0e7bd9b1ba6e18af7259fe7cd72e19a10a1ae4 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Wed, 7 Oct 2026 16:48:26 +0200 Subject: [PATCH] fix: validate CSV before logger startup --- docs/architecture.md | 3 ++- docs/tiny-wb-operations.md | 2 +- src/main.rs | 44 +++++++++++++++++++++----------------- tests/startup.rs | 8 +++++-- 4 files changed, 33 insertions(+), 24 deletions(-) diff --git a/docs/architecture.md b/docs/architecture.md index 0ff7862..59f0569 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -84,7 +84,8 @@ boundary can grow into a tabbed collection without coupling CSV parsing or acquisition to Ratatui. The terminal guard restores terminal modes on ordinary returns and unwinding. -Configuration, logging and enumeration errors occur before terminal takeover. +Configuration and direct-view CSV errors occur before logger initialization; +logging and enumeration errors occur before terminal takeover. Process termination or release-mode panic abort cannot be recovered by a guard. ## Acquisition and protocol boundary diff --git a/docs/tiny-wb-operations.md b/docs/tiny-wb-operations.md index 9e6b9a6..d301ec0 100644 --- a/docs/tiny-wb-operations.md +++ b/docs/tiny-wb-operations.md @@ -162,7 +162,7 @@ frequency-label convention explicitly after reviewing the [frequency assignment notes](usb-protocol.md#frequency-assignment). The direct renderer accepts this header or headerless files, ignores blank lines, and reports malformed, non-finite, empty, or decreasing-frequency data -with its CSV line number before taking over the terminal. +with its CSV line number before initializing logging or taking over the terminal. ### Logs diff --git a/src/main.rs b/src/main.rs index 785c9c2..775bd45 100644 --- a/src/main.rs +++ b/src/main.rs @@ -59,6 +59,25 @@ fn main() -> Result<(), AppError> { eprintln!("Cannot load {}: {error}", config_path.display()); error })?; + let loaded_plot = if let StartMode::View(path) = &mode { + let file = File::open(path).map_err(|source| AppError::Csv { + path: path.clone(), + source, + })?; + let samples = + tiny_wb::export::read_csv(BufReader::new(file)).map_err(|source| AppError::Csv { + path: path.clone(), + source, + })?; + Some( + samples + .into_iter() + .map(|sample| (sample.frequency_mhz, sample.level_dbm)) + .collect(), + ) + } else { + None + }; let _logger = Logger::try_with_str(&config.logging.filter)? .log_to_file(FileSpec::default().directory(&config.logging.directory)) .write_mode(WriteMode::BufferAndFlush) @@ -74,26 +93,11 @@ fn main() -> Result<(), AppError> { }; ui::App::new(ports, config, config_path) } - StartMode::View(path) => { - let file = File::open(&path).map_err(|source| AppError::Csv { - path: path.clone(), - source, - })?; - let samples = tiny_wb::export::read_csv(BufReader::new(file)).map_err(|source| { - AppError::Csv { - path: path.clone(), - source, - } - })?; - ui::App::viewer( - config, - path, - samples - .into_iter() - .map(|sample| (sample.frequency_mhz, sample.level_dbm)) - .collect(), - ) - } + StartMode::View(path) => ui::App::viewer( + config, + path, + loaded_plot.expect("viewer data was loaded for view mode"), + ), StartMode::Help => unreachable!("help returned before configuration loading"), }; let _session = TerminalSession; diff --git a/tests/startup.rs b/tests/startup.rs index 8b0f20c..0565115 100644 --- a/tests/startup.rs +++ b/tests/startup.rs @@ -29,16 +29,20 @@ fn malformed_csv_exits_without_entering_the_terminal_or_enumerating_ports() { fs::create_dir_all(&directory).unwrap(); let path = directory.join("broken.csv"); fs::write(&path, "frequency_MHz,level_dBm\n240,not-a-level\n").unwrap(); + let config_path = directory.join("viewer.toml"); + fs::write(&config_path, "[logging]\ndirectory = 'not-a-directory'\n").unwrap(); + fs::write(directory.join("not-a-directory"), "ordinary file").unwrap(); let result = Command::new(env!("CARGO_BIN_EXE_tiny-wb")) .current_dir(&directory) + .env("TINY_WB_CONFIG", config_path) .arg("--view") .arg(&path) .output() .unwrap(); assert!(!result.status.success()); let error = String::from_utf8_lossy(&result.stderr); - assert!(error.contains("broken.csv")); - assert!(error.contains("line 2")); + assert!(error.contains("broken.csv"), "unexpected stderr: {error}"); + assert!(error.contains("line 2"), "unexpected stderr: {error}"); assert!(!result.stdout.windows(2).any(|bytes| bytes == b"\x1b[")); fs::remove_dir_all(directory).unwrap(); }