Skip to content

Commit 9722dae

Browse files
committed
fixed path issue (saving file)
1 parent 3395947 commit 9722dae

File tree

3 files changed

+24
-19
lines changed

3 files changed

+24
-19
lines changed

src/gui.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use core::f32;
22
use std::ops::RangeInclusive;
3+
use std::path::PathBuf;
34
use std::sync::mpsc::{Sender};
45
use std::sync::{Arc, RwLock};
56
use std::time::Duration;
@@ -71,7 +72,7 @@ pub struct MyApp {
7172
plotting_range: i32,
7273
console: Vec<Print>,
7374
dropped_files: Vec<egui::DroppedFile>,
74-
picked_path: String,
75+
picked_path: PathBuf,
7576
data: DataContainer,
7677
gui_conf: GuiSettingsContainer,
7778
print_lock: Arc<RwLock<Vec<Print>>>,
@@ -80,7 +81,7 @@ pub struct MyApp {
8081
baud_lock: Arc<RwLock<u32>>,
8182
connected_lock: Arc<RwLock<bool>>,
8283
data_lock: Arc<RwLock<DataContainer>>,
83-
save_tx: Sender<String>,
84+
save_tx: Sender<PathBuf>,
8485
send_tx: Sender<String>,
8586
clear_tx: Sender<bool>,
8687
eol: String,
@@ -97,15 +98,15 @@ impl MyApp {
9798
baud_lock: Arc<RwLock<u32>>,
9899
connected_lock: Arc<RwLock<bool>>,
99100
gui_conf: GuiSettingsContainer,
100-
save_tx: Sender<String>,
101+
save_tx: Sender<PathBuf>,
101102
send_tx: Sender<String>,
102103
clear_tx: Sender<bool>,
103104
) -> Self {
104105
Self {
105106
dark_mode: true,
106107
ready: false,
107108
dropped_files: vec![],
108-
picked_path: "".to_string(),
109+
picked_path: PathBuf::new(),
109110
device: "".to_string(),
110111
data: DataContainer::default(),
111112
console: vec![Print::MESSAGE(format!("waiting for serial connection..,").to_string())],
@@ -381,20 +382,22 @@ impl eframe::App for MyApp {
381382
ui.end_row();
382383
if ui.button("Save to file").clicked() {
383384
match rfd::FileDialog::new().save_file() {
384-
Some(path) =>
385-
// TODO: here we should really include .csv as extension!
385+
Some(mut path) =>
386386
{
387-
let extension = ".csv".to_string();
388-
let mut final_path: String;
389-
if path.display().to_string().ends_with(".csv") {
390-
final_path = path.display().to_string();
391-
} else {
392-
final_path = path.display().to_string();
393-
final_path.push_str(&extension);
387+
let extension = "csv";
388+
match path.extension() {
389+
None => {
390+
path.set_extension(&extension);
391+
}
392+
Some(ext) => {
393+
if ext != "csv" {
394+
path.set_extension(&extension);
395+
}
396+
}
394397
}
395-
self.picked_path = final_path;
398+
self.picked_path = path;
396399
}
397-
None => self.picked_path = "".to_string()
400+
None => self.picked_path = PathBuf::new()
398401
}
399402
match self.save_tx.send(self.picked_path.clone()) {
400403
Ok(_) => {}

src/io.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use std::error::Error;
2+
use std::path::PathBuf;
23
use csv::{WriterBuilder};
34
use crate::DataContainer;
45

56

6-
pub fn save_to_csv(data: &DataContainer, file_path: &String) -> Result<(), Box<dyn Error>> {
7+
pub fn save_to_csv(data: &DataContainer, file_path: &PathBuf) -> Result<(), Box<dyn Error>> {
78
let mut wtr = WriterBuilder::new()
89
.has_headers(false)
910
.from_path(file_path)?;

src/main.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ mod serial;
1212
mod data;
1313

1414
use std::cmp::max;
15+
use std::path::PathBuf;
1516
use std::thread;
1617
use eframe::egui::{vec2, Visuals};
1718
use std::sync::mpsc::{Receiver, Sender};
@@ -62,11 +63,11 @@ fn split(payload: &str) -> Vec<&str> {
6263
fn main_thread(data_lock: Arc<RwLock<DataContainer>>,
6364
raw_data_lock: Arc<RwLock<Vec<Packet>>>,
6465
print_lock: Arc<RwLock<Vec<Print>>>,
65-
save_rx: Receiver<String>,
66+
save_rx: Receiver<PathBuf>,
6667
clear_rx: Receiver<bool>) {
6768
// reads data from mutex, samples and saves if needed
6869
let mut acquire = false;
69-
let mut file_path = "serial_monitor_test.csv".to_string();
70+
let mut file_path = PathBuf::from("serial_monitor_test.csv");
7071
let mut data = DataContainer::default();
7172
let mut failed_format_counter = 0;
7273
loop {
@@ -185,7 +186,7 @@ fn main() {
185186
let print_lock = Arc::new(RwLock::new(vec![Print::EMPTY]));
186187
let connected_lock = Arc::new(RwLock::new(false));
187188

188-
let (save_tx, save_rx): (Sender<String>, Receiver<String>) = mpsc::channel();
189+
let (save_tx, save_rx): (Sender<PathBuf>, Receiver<PathBuf>) = mpsc::channel();
189190
let (send_tx, send_rx): (Sender<String>, Receiver<String>) = mpsc::channel();
190191
let (clear_tx, clear_rx): (Sender<bool>, Receiver<bool>) = mpsc::channel();
191192

0 commit comments

Comments
 (0)