Skip to content

Commit

Permalink
Reorganize wghub-rust-web for smaller files
Browse files Browse the repository at this point in the history
  • Loading branch information
tradeJmark committed Sep 2, 2023
1 parent e603886 commit 4c656f2
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 77 deletions.
78 changes: 10 additions & 68 deletions rust/wghub-rust-web/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,81 +1,23 @@
pub mod ws;
mod wrapper;
mod utils;

use js_sys::{JsString, Array};

use utils::Blobbable;
use wasm_bindgen::prelude::*;
use web_sys::Blob;
use wghub_rust::{model::config::{HubConfig, SpokeData, HubData, SpokeConfig, SpokeCommonData}, create_hub_config_file, create_spoke_config_file};

//All of these wrapper classes will be able to go away once wasm-bindgen
//merges in https://github.com/rustwasm/wasm-bindgen/pull/3554 to allow
//Vecs of Strings and bound custom types.
use wghub_rust::{create_hub_config_file, create_spoke_config_file};
use wrapper::HubConfigWrapper;
pub use utils::set_panic_hook;

pub fn zip_spoke_data(spoke_ip_addresses: Vec<JsString>, spoke_public_keys: Vec<JsString>) -> Vec<SpokeData> {
spoke_ip_addresses.iter()
.zip(spoke_public_keys.iter())
.map(|(ip_address, public_key)| SpokeData::new(ip_address.into(), public_key.into()))
.collect()
}

#[wasm_bindgen(js_name = "HubConfig")]
pub struct HubConfigWrapper(HubConfig);
#[wasm_bindgen(js_class = "HubConfig")]
impl HubConfigWrapper {
#[wasm_bindgen(constructor)]
pub fn new(name: String, hub: HubData, spoke_ip_addresses: Vec<JsString>, spoke_public_keys: Vec<JsString>) -> HubConfigWrapper {
HubConfigWrapper(HubConfig::new(name, hub, zip_spoke_data(spoke_ip_addresses, spoke_public_keys)))
}
}
use crate::wrapper::SpokeConfigWrapper;

#[wasm_bindgen(js_name = "generateHubConfigFile")]
pub fn generate_hub_config_file(config: &HubConfigWrapper) -> Blob {
create_hub_config_file(&config.0).blobbify()
}

#[wasm_bindgen(js_name = "SpokeCommonData")]
pub struct SpokeCommonDataWrapper(SpokeCommonData);
#[wasm_bindgen(js_class = "SpokeCommonData")]
impl SpokeCommonDataWrapper {
#[wasm_bindgen(constructor)]
pub fn new(dns_servers: Vec<JsString>, search_domains: Vec<JsString>, allowed_ips: Vec<JsString>) -> SpokeCommonDataWrapper {
SpokeCommonDataWrapper(SpokeCommonData {dns_servers: dns_servers.all_into(), search_domains: search_domains.all_into(), allowed_ips: allowed_ips.all_into()})
}
}

#[wasm_bindgen(js_name = "SpokeConfig")]
pub struct SpokeConfigWrapper(SpokeConfig);
#[wasm_bindgen(js_class = "SpokeConfig")]
impl SpokeConfigWrapper {
#[wasm_bindgen(constructor)]
pub fn new(hub: HubData, spoke: SpokeData, common: SpokeCommonDataWrapper) -> SpokeConfigWrapper {
SpokeConfigWrapper(SpokeConfig { hub, spoke, common: common.0 })
}
create_hub_config_file(&config.clone().into()).blobbify()
}

#[wasm_bindgen(js_name = "generateSpokeConfigFile")]
pub fn generate_spoke_config_file(config: &SpokeConfigWrapper) -> Blob {
create_spoke_config_file(&config.0).blobbify()
}

trait AllInto<R> {
fn all_into(self) -> Vec<R>;
}

impl<C, R> AllInto<R> for C where
C: IntoIterator,
C::Item: Into<R>
{
fn all_into(self) -> Vec<R> {
self.into_iter().map(|t| t.into()).collect()
}
}

trait Blobbable {
fn blobbify(self) -> Blob;
}

impl Blobbable for String {
fn blobbify(self) -> Blob {
let array = Array::of1(&self.into());
Blob::new_with_str_sequence(&array).unwrap()
}
create_spoke_config_file(&config.clone().into()).blobbify()
}
43 changes: 35 additions & 8 deletions rust/wghub-rust-web/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,37 @@
use js_sys::Array;
use web_sys::Blob;

pub fn set_panic_hook() {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
// we will get better error messages if our code ever panics.
//
// For more details see
// https://github.com/rustwasm/console_error_panic_hook#readme
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
// we will get better error messages if our code ever panics.
//
// For more details see
// https://github.com/rustwasm/console_error_panic_hook#readme
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}

pub trait AllInto<R> {
fn all_into(self) -> Vec<R>;
}

impl<C, R> AllInto<R> for C where
C: IntoIterator,
C::Item: Into<R>
{
fn all_into(self) -> Vec<R> {
self.into_iter().map(|t| t.into()).collect()
}
}

pub trait Blobbable {
fn blobbify(self) -> Blob;
}

impl Blobbable for String {
fn blobbify(self) -> Blob {
let array = Array::of1(&self.into());
Blob::new_with_str_sequence(&array).unwrap()
}
}
61 changes: 61 additions & 0 deletions rust/wghub-rust-web/src/wrapper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//All of these wrapper classes will be able to go away once wasm-bindgen
//merges in https://github.com/rustwasm/wasm-bindgen/pull/3554 to allow
//Vecs of Strings and bound custom types.

use wasm_bindgen::prelude::*;
use js_sys::JsString;
use wghub_rust::model::config::{SpokeData, HubConfig, HubData, SpokeCommonData, SpokeConfig};

use crate::utils::AllInto;

#[wasm_bindgen(js_name = "HubConfig")]
#[derive(Clone)]
pub struct HubConfigWrapper(HubConfig);
#[wasm_bindgen(js_class = "HubConfig")]
impl HubConfigWrapper {
#[wasm_bindgen(constructor)]
pub fn new(name: String, hub: HubData, spoke_ip_addresses: Vec<JsString>, spoke_public_keys: Vec<JsString>) -> HubConfigWrapper {
HubConfigWrapper(HubConfig::new(name, hub, Self::zip_spoke_data(spoke_ip_addresses, spoke_public_keys)))
}

fn zip_spoke_data(spoke_ip_addresses: Vec<JsString>, spoke_public_keys: Vec<JsString>) -> Vec<SpokeData> {
spoke_ip_addresses.into_iter()
.zip(spoke_public_keys.into_iter())
.map(|(ip_address, public_key)| SpokeData::new(ip_address.into(), public_key.into()))
.collect()
}
}

impl Into<HubConfig> for HubConfigWrapper {
fn into(self) -> HubConfig {
self.0
}
}

#[derive(Clone)]
#[wasm_bindgen(js_name = "SpokeCommonData")]
pub struct SpokeCommonDataWrapper(SpokeCommonData);
#[wasm_bindgen(js_class = "SpokeCommonData")]
impl SpokeCommonDataWrapper {
#[wasm_bindgen(constructor)]
pub fn new(dns_servers: Vec<JsString>, search_domains: Vec<JsString>, allowed_ips: Vec<JsString>) -> SpokeCommonDataWrapper {
SpokeCommonDataWrapper(SpokeCommonData {dns_servers: dns_servers.all_into(), search_domains: search_domains.all_into(), allowed_ips: allowed_ips.all_into()})
}
}

#[wasm_bindgen(js_name = "SpokeConfig")]
#[derive(Clone)]
pub struct SpokeConfigWrapper(SpokeConfig);
#[wasm_bindgen(js_class = "SpokeConfig")]
impl SpokeConfigWrapper {
#[wasm_bindgen(constructor)]
pub fn new(hub: HubData, spoke: SpokeData, common: SpokeCommonDataWrapper) -> SpokeConfigWrapper {
SpokeConfigWrapper(SpokeConfig { hub, spoke, common: common.0 })
}
}

impl Into<SpokeConfig> for SpokeConfigWrapper {
fn into(self) -> SpokeConfig {
self.0
}
}
2 changes: 1 addition & 1 deletion rust/wghub-rust-web/src/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use ws_stream_wasm::{WsMeta, WsStream, WsMessage};
use futures_util::{SinkExt, StreamExt, stream::SplitSink, lock::Mutex};
use wghub_rust::model::{message::{ClientMessage, ServerMessage, WGHubMessage}, SpokeID};

use crate::AllInto;
use crate::utils::AllInto;

#[wasm_bindgen(module = "redux")]
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions rust/wghub-rust/src/model/config/hub_config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::{SpokeData, HubData};

#[derive(Clone)]
pub struct HubConfig {
pub name: String,
pub hub: HubData,
Expand Down
1 change: 1 addition & 0 deletions rust/wghub-rust/src/model/config/hub_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use wasm_bindgen::prelude::*;

#[cfg_attr(feature = "wasm", wasm_bindgen(getter_with_clone))]
#[derive(Clone)]
pub struct HubData {
pub public_key: String,
pub ip_address: String,
Expand Down
1 change: 1 addition & 0 deletions rust/wghub-rust/src/model/config/spoke_common_data.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[derive(Clone)]
pub struct SpokeCommonData {
pub dns_servers: Vec<String>,
pub search_domains: Vec<String>,
Expand Down
1 change: 1 addition & 0 deletions rust/wghub-rust/src/model/config/spoke_config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::{HubData, SpokeData, SpokeCommonData};

#[derive(Clone)]
pub struct SpokeConfig {
pub hub: HubData,
pub spoke: SpokeData,
Expand Down
1 change: 1 addition & 0 deletions rust/wghub-rust/src/model/config/spoke_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use wasm_bindgen::prelude::*;

#[cfg_attr(feature = "wasm", wasm_bindgen(getter_with_clone))]
#[derive(Clone)]
pub struct SpokeData {
pub ip_address: String,
pub public_key: String
Expand Down

0 comments on commit 4c656f2

Please sign in to comment.