Saving file and dealing with all errors.
This commit is contained in:
		
					parent
					
						
							
								9ed44a9a68
							
						
					
				
			
			
				commit
				
					
						542923ccef
					
				
			
		
					 1 changed files with 14 additions and 9 deletions
				
			
		
							
								
								
									
										23
									
								
								src/main.rs
									
										
									
									
									
								
							
							
						
						
									
										23
									
								
								src/main.rs
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -1,4 +1,5 @@
 | 
			
		|||
use eframe::egui;
 | 
			
		||||
use std::io::Write as _;
 | 
			
		||||
 | 
			
		||||
use billy_sheet::gui::SheetGui;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -40,16 +41,20 @@ impl eframe::App for Gui {
 | 
			
		|||
                        _frame.close();
 | 
			
		||||
                    }
 | 
			
		||||
                    if ui.button("Save in json").clicked() {
 | 
			
		||||
                        let sheet_str = serde_json::to_string(&self.sheet);
 | 
			
		||||
                        let path = std::path::Path::new("./sheet.json");
 | 
			
		||||
                        if path.exists() {
 | 
			
		||||
                            if let Err(a) = std::fs::remove_file(path) {
 | 
			
		||||
                                println!("{a}");
 | 
			
		||||
                            } else {
 | 
			
		||||
                                
 | 
			
		||||
                        let result = serde_json::to_string_pretty(&self.sheet);
 | 
			
		||||
                        match result {
 | 
			
		||||
                            Ok(sheet_str) => {
 | 
			
		||||
                                let path = std::path::Path::new("./sheet.json");
 | 
			
		||||
                                match std::fs::File::create(path) {
 | 
			
		||||
                                    Ok(mut file) => {
 | 
			
		||||
                                        if let Err(err) = write!(file, "{}", sheet_str) {
 | 
			
		||||
                                            println!("{err}");
 | 
			
		||||
                                        }
 | 
			
		||||
                                    }
 | 
			
		||||
                                    Err(error) => println!("{error}"),
 | 
			
		||||
                                }
 | 
			
		||||
                            }
 | 
			
		||||
                        } else {
 | 
			
		||||
 | 
			
		||||
                            Err(error) => println!("{error}"),
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                });
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue