fix: handle PathBuf correctly
This commit is contained in:
67
src/main.rs
67
src/main.rs
@ -13,6 +13,7 @@ use anyhow::{Context, Result};
|
||||
|
||||
use model::Model;
|
||||
use draw::run_tui;
|
||||
use serde_json::Value;
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
@ -35,42 +36,46 @@ impl Runnable for CmdLineArgs {
|
||||
let model = get_initial_model(&self.file_path)?;
|
||||
|
||||
// Pre-TUI import: parse JSON or CSV and open wizard
|
||||
let import_value = if let Some(ref path) = self.import_path {
|
||||
match std::fs::read_to_string(path) {
|
||||
Err(e) => {
|
||||
eprintln!("Cannot read '{}': {e}", path.display());
|
||||
return Ok(());
|
||||
}
|
||||
Ok(content) => {
|
||||
if path.to_string_lossy().ends_with(".csv") {
|
||||
// Parse CSV and wrap as JSON array
|
||||
match crate::import::csv_parser::parse_csv(&path.to_string_lossy()) {
|
||||
Ok(records) => Some(serde_json::Value::Array(records)),
|
||||
Err(e) => {
|
||||
eprintln!("CSV parse error: {e}");
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Parse JSON
|
||||
match serde_json::from_str::<serde_json::Value>(&content) {
|
||||
Err(e) => {
|
||||
eprintln!("JSON parse error: {e}");
|
||||
return Ok(());
|
||||
}
|
||||
Ok(json) => Some(json),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let import_value = self.import_path.and_then(get_import_data);
|
||||
|
||||
run_tui(model, self.file_path, import_value)
|
||||
}
|
||||
}
|
||||
|
||||
fn csv_path_p(path:&PathBuf) -> bool {
|
||||
path.extension().is_some_and(|ext| ext.eq_ignore_ascii_case("csv"))
|
||||
}
|
||||
|
||||
fn get_import_data(path: PathBuf) -> Option<Value> {
|
||||
match std::fs::read_to_string(&path) {
|
||||
Err(e) => {
|
||||
eprintln!("Cannot read '{}': {e}", path.display());
|
||||
None
|
||||
}
|
||||
Ok(content) => {
|
||||
if csv_path_p(&path) {
|
||||
// Parse CSV and wrap as JSON array
|
||||
match crate::import::csv_parser::parse_csv(&path) {
|
||||
Ok(records) => Some(serde_json::Value::Array(records)),
|
||||
Err(e) => {
|
||||
eprintln!("CSV parse error: {e}");
|
||||
None
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Parse JSON
|
||||
match serde_json::from_str::<serde_json::Value>(&content) {
|
||||
Err(e) => {
|
||||
eprintln!("JSON parse error: {e}");
|
||||
None
|
||||
}
|
||||
Ok(json) => Some(json),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct HeadlessArgs {
|
||||
file_path: Option<PathBuf>,
|
||||
commands: Vec<String>,
|
||||
|
||||
Reference in New Issue
Block a user