Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mips-lib/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn main() {
let args = Args::parse();
let path = PathBuf::from(args.model);

let cs = ComponentStore::load_file(&path);
let cs = ComponentStore::load_file(&path).unwrap_or(ComponentStore { store: vec![] });

#[cfg(feature = "gui-egui")]
syncrim::gui_egui::gui(cs, &path, Library::default()).ok();
Expand Down
2 changes: 1 addition & 1 deletion riscv/examples/empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct Args {

fn main() {
let path = PathBuf::from("riscv.json");
let cs = ComponentStore::load_file(&path);
let cs = ComponentStore::load_file(&path).unwrap();
#[cfg(feature = "gui-egui")]
{
use riscv::components::*;
Expand Down
2 changes: 1 addition & 1 deletion riscv/examples/riscv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ fn main() {
}
}
let path = PathBuf::from("riscv.json");
let mut cs = ComponentStore::load_file(&path);
let mut cs = ComponentStore::load_file(&path).unwrap();
let mut i = 0;
let mut store = cs.store.clone();
for component in store.clone() {
Expand Down
50 changes: 43 additions & 7 deletions src/component_store.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,56 @@
use crate::common::ComponentStore;

use std::{fs::File, io::prelude::*, path::PathBuf};
use std::{fmt::Display, fs::File, io::prelude::*, path::PathBuf};

use log::*;

#[derive(Debug)]
pub enum ComponentStoreLoadError {
Json(serde_json::Error),
Io(std::io::Error, PathBuf),
}
impl Display for ComponentStoreLoadError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ComponentStoreLoadError::Json(error) => {
write!(f, "Error while decoding json: {}", error)
}
ComponentStoreLoadError::Io(error, path) => {
write!(f, "Error while reading file {:?} : {}", path, error)
}
}
}
}

impl std::error::Error for ComponentStoreLoadError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
ComponentStoreLoadError::Json(error) => Some(error),
ComponentStoreLoadError::Io(error, _) => Some(error),
}
}
}

impl From<serde_json::Error> for ComponentStoreLoadError {
fn from(value: serde_json::Error) -> Self {
Self::Json(value)
}
}

impl ComponentStore {
pub fn load(json: &str) -> Self {
serde_json::from_str(json).unwrap()
pub fn load(json: &str) -> Result<Self, serde_json::Error> {
serde_json::from_str(json)
}

pub fn load_file(path: &PathBuf) -> Self {
let mut file = File::open(path).unwrap();
pub fn load_file(path: &PathBuf) -> Result<Self, ComponentStoreLoadError> {
let mut file =
File::open(path).map_err(|err| ComponentStoreLoadError::Io(err, path.clone()))?;
let mut json = String::new();
file.read_to_string(&mut json).unwrap();
file.read_to_string(&mut json)
.map_err(|err| ComponentStoreLoadError::Io(err, path.clone()))?;

ComponentStore::load(&json)
let cs = ComponentStore::load(&json)?;
Ok(cs)
}

pub fn save_file(&self, path: &PathBuf) {
Expand Down
8 changes: 5 additions & 3 deletions src/gui_egui/keymap.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::common::{ComponentStore, Simulator};
use crate::component_store::ComponentStoreLoadError;
use crate::gui_egui::editor::{Editor, EditorMode};
use crate::gui_egui::editor_wire_mode::reset_wire_mode;
use crate::gui_egui::gui::create_contexts;
Expand Down Expand Up @@ -179,7 +180,7 @@ impl Shortcuts {
file_new_fn(gui);
}
if ctx.input_mut(|i| i.consume_shortcut(&self.file_open)) {
file_open_fn(gui);
let _ = file_open_fn(gui);
}
if ctx.input_mut(|i| i.consume_shortcut(&self.file_save)) {
file_save_fn(gui);
Expand Down Expand Up @@ -245,12 +246,12 @@ impl Shortcuts {
}

pub fn file_new_fn(_gui: &mut Gui) {}
pub fn file_open_fn(gui: &mut Gui) {
pub fn file_open_fn(gui: &mut Gui) -> Result<(), ComponentStoreLoadError> {
let files = FileDialog::new().add_filter("json", &["json"]).pick_file();
if let Some(path_buf) = files {
gui.path = path_buf;
}
let cs = ComponentStore::load_file(&gui.path);
let cs = ComponentStore::load_file(&gui.path)?;
let contexts = create_contexts(&cs.store);
match gui.editor_use {
true => {
Expand All @@ -276,6 +277,7 @@ pub fn file_open_fn(gui: &mut Gui) {
}
}
}
Ok(())
}
pub fn file_save_fn(gui: &mut Gui) {
match gui.editor_use {
Expand Down
2 changes: 1 addition & 1 deletion src/gui_egui/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ fn shared_buttons_file(gui: &mut Gui, ui: &mut Ui) {
keymap::file_new_fn(gui);
}
if btn(ui, "Open", gui.shortcuts.file_open).clicked() {
keymap::file_open_fn(gui);
let _ = keymap::file_open_fn(gui);
}
ui.menu_button("Open Recent", |_ui| {
// Recent here
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() {
let args = Args::parse();
let path = PathBuf::from(args.model);

let cs = ComponentStore::load_file(&path);
let cs = ComponentStore::load_file(&path).unwrap();

#[cfg(feature = "gui-egui")]
syncrim::gui_egui::gui(cs, &path, Library::default()).ok();
Expand Down
Loading