Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 104: save complete config in file #106

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
67 changes: 66 additions & 1 deletion uvls/src/webview.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::{core::*, ide, smt};
use crate::{
core::*,
ide,
smt::{self, uvl2smt, SmtSolver},
};
use axum::{
extract::{ws::WebSocketUpgrade, Path},
response::Html,
Expand Down Expand Up @@ -178,6 +182,7 @@ pub enum UIAction {
ShowSym(ModuleSymbol, u8),
SolverActive,
Save,
SaveAll,
Show,
ExpandAll,
CollapseAll,
Expand Down Expand Up @@ -506,6 +511,66 @@ async fn ui_event_loop(
})
});
}
UIAction::SaveAll => {
let output_name = ui_state.read().file_name.clone();
let tgt_path = tgt_path.clone();

let config_module = tx_config.borrow().module.clone();
let smt_module = uvl2smt(&config_module.module, &config_module.values);
let solver = SmtSolver::new(
smt_module.to_source(&config_module),
&CancellationToken::new(),
)
.await;

match solver {
Ok(mut smt_solver) => match smt_solver.check_sat().await {
Ok(true) => {
let query = smt_module
.variables
.iter()
.enumerate()
.fold(String::new(), |acc, (i, _)| format!("{acc} v{i}"));

let values = smt_solver
.values(query.clone())
.await
.unwrap_or(String::from(""));

let values_parsed: HashMap<ModuleSymbol, ConfigValue> = smt_module
.parse_values(&values, &config_module.module)
.collect();

// Store solution in file
tokio::task::spawn_blocking(move || {
let config_module = ConfigModule {
module: config_module.module.clone(),
values: values_parsed.clone(),
source_map: Default::default(),
};

if !config_module.ok {
return;
}
let ser = config_module.serialize();
#[derive(Serialize)]
struct RawConfig {
file: String,
config: ConfigEntry,
}
let config = RawConfig {
file: tgt_path,
config: ConfigEntry::Import(Default::default(), ser),
};
let out = serde_json::to_string_pretty(&config).unwrap();
let _ = std::fs::write(output_name, out);
});
}
_ => {}
},
_ => (),
}
}
UIAction::Save => {
let module = tx_config.borrow().module.clone();
let output_name = ui_state.read().file_name.clone();
Expand Down
10 changes: 10 additions & 0 deletions uvls/src/webview/frontend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,16 @@ pub fn App(cx: Scope<AppProps>) -> Element {
"{solver_state}"
}
}
li{
button{
class:"btn-save",
onclick: move |_|{
ui_tx.send(UIAction::SaveAll);
},
"Save All"

}
}
li{
button{
class:"btn-save",
Expand Down