Skip to content

Commit

Permalink
⚙️(Dependencies): Update dependencies and switch from serde to bincode
Browse files Browse the repository at this point in the history
Replace serde with bincode for serialization and add base64 for encoding/decoding, updating the `load_from_url_search` and `save_to_url_search` functions accordingly. This change enhances the application's data handling capabilities.

Signed-off-by: Benign X <[email protected]>
  • Loading branch information
W-Mai committed Nov 18, 2024
1 parent 0e86f46 commit 576a754
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ levenshtein = "1.0.5"

dyn-clone = "1.0.16"
web-sys = { version = "0.3.72", features = ["History"] }
serde = { version = "1.0.215", features = ["derive"] }
bincode = "2.0.0-rc.3"
base64 = "0.22.1"

# native:
[target.'cfg(all(not(target_arch = "wasm32"), platform = "macos"))'.dependencies]
Expand Down Expand Up @@ -58,7 +59,6 @@ eframe = { version = "0.29.1", default-features = false, features = [
"persistence"
] }
wasm-bindgen-futures = "0.4"
serde_qs = "0.13.0"

[profile.release]
opt-level = 2 # fast and small wasm
Expand Down
49 changes: 34 additions & 15 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ use crate::cus_component::{toggle, CodeEditor};
use crate::interfaces::{
ICodeEditor, IParser, IVisData, IVisDataGenerator, IVisualizer, ParseError,
};
use bincode::{Decode, Encode};
use eframe::{egui, Storage};
use egui_extras::{Size, StripBuilder};
use log::error;
use serde::{Deserialize, Serialize};
use std::time::Duration;
use std::vec;

#[cfg(target_arch = "wasm32")]
use base64::prelude::*;

const DEFAULT_CODE: &str = include_str!("default_code");

struct MainAppCache {
Expand All @@ -25,19 +28,19 @@ struct MainAppCache {
transfer_data: TransferData,
}

#[derive(Clone, PartialEq, Serialize, Deserialize)]
#[derive(Clone, PartialEq, Decode, Encode)]

struct MainAppParams {
vis_progress: i64,
vis_progress_max: i64,

lcd_coords: bool,
show_inter_dash: bool,
colorful_block: bool,

trans_matrix: [[f64; 3]; 3],
}

#[derive(Clone, PartialEq, Default, Serialize, Deserialize)]
#[derive(Clone, PartialEq, Default, Decode, Encode)]
struct TransferData {
code: String,
params: MainAppParams,
Expand Down Expand Up @@ -399,12 +402,24 @@ impl MainApp {
fn load_from_url_search(&mut self) {
use eframe::web::web_location;
let location = web_location();
if let Ok(t) = serde_qs::from_str::<TransferData>(&location.query) {
self.code = AnyData::new(t.code);
self.params = t.params;
} else {
error!("Invalid query string");
let query = &location.query;
if query.is_empty() {
return;
}

if let Ok(data) = BASE64_URL_SAFE_NO_PAD.decode(query) {
let config = bincode::config::standard();
if let Ok((t, _s)) =
bincode::decode_from_slice(&data, config) as Result<(TransferData, _), _>
{
self.code = AnyData::new(t.code);
self.params = t.params;

return;
}
}

error!("Invalid query string");
}

fn save_to_url_search(&mut self) {
Expand All @@ -420,12 +435,16 @@ impl MainApp {

self.cache.transfer_data = transfer_data;

let mut t = serde_qs::to_string(&self.cache.transfer_data).unwrap();
t.insert(0, '?');
let config = bincode::config::standard();
if let Ok(data) = bincode::encode_to_vec(&self.cache.transfer_data, config) {
let mut t = BASE64_URL_SAFE_NO_PAD.encode(data);

t.insert(0, '?');

use eframe::wasm_bindgen::JsValue;
history
.push_state_with_url(&JsValue::NULL, "", Some(&t))
.unwrap()
use eframe::wasm_bindgen::JsValue;
history
.push_state_with_url(&JsValue::NULL, "", Some(&t))
.unwrap()
}
}
}

0 comments on commit 576a754

Please sign in to comment.