-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reorganize wghub-rust-web for smaller files
- Loading branch information
1 parent
e603886
commit 4c656f2
Showing
9 changed files
with
112 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters