Skip to content

Commit

Permalink
Merge very similar traits providing wrap_all and unwrap_all into sing…
Browse files Browse the repository at this point in the history
…le IntoCollection trait
  • Loading branch information
tradeJmark committed Sep 2, 2023
1 parent 0b1e3bb commit d722b45
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
18 changes: 7 additions & 11 deletions rust/wghub-rust-web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub struct SpokeCommonDataWrapper(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.unwrap_all(), search_domains: search_domains.unwrap_all(), allowed_ips: allowed_ips.unwrap_all()})
SpokeCommonDataWrapper(SpokeCommonData {dns_servers: dns_servers.all_into(), search_domains: search_domains.all_into(), allowed_ips: allowed_ips.all_into()})
}
}

Expand All @@ -56,22 +56,18 @@ pub fn generate_spoke_config_file(config: &SpokeConfigWrapper) -> Blob {
create_spoke_config_file(&config.0).blobbify()
}

trait JsStringCollection {
fn unwrap_all(self) -> Vec<String>;
trait IntoCollection<T: Into<R>, R> {
fn all_into(self) -> Vec<R>;
}

impl JsStringCollection for Vec<JsString> {
fn unwrap_all(self) -> Vec<String> {
impl IntoCollection<JsString, String> for Vec<JsString> {
fn all_into(self) -> Vec<String> {
self.iter().map(|s| s.into()).collect()
}
}

trait JsStringableCollection {
fn wrap_all(self) -> Vec<JsString>;
}

impl JsStringableCollection for Vec<String> {
fn wrap_all(self) -> Vec<JsString> {
impl IntoCollection<String, JsString> for Vec<String> {
fn all_into(self) -> Vec<JsString> {
self.into_iter().map(|s| s.into()).collect()
}
}
Expand Down
14 changes: 7 additions & 7 deletions 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::{JsStringableCollection, JsStringCollection};
use crate::IntoCollection;

#[wasm_bindgen(module = "redux")]
extern "C" {
Expand Down Expand Up @@ -59,18 +59,18 @@ extern "C" {

impl Into<wghub_rust::model::Hub> for Hub {
fn into(self) -> wghub_rust::model::Hub {
let dns_servers = Some(self.dns_servers().unwrap_all());
let search_domains = Some(self.search_domains().unwrap_all());
let allowed_ips = Some(self.allowed_ips().unwrap_all());
let dns_servers = Some(self.dns_servers().all_into());
let search_domains = Some(self.search_domains().all_into());
let allowed_ips = Some(self.allowed_ips().all_into());
wghub_rust::model::Hub::new(self.name(), self.description(), self.public_key(), self.endpoint(), self.ip_address(), dns_servers, search_domains, allowed_ips)
}
}

impl From<wghub_rust::model::Hub> for Hub {
fn from(value: wghub_rust::model::Hub) -> Self {
let dns_servers = Some(value.dns_servers.wrap_all());
let search_domains = Some(value.search_domains.wrap_all());
let allowed_ips = Some(value.allowed_ips.wrap_all());
let dns_servers = Some(value.dns_servers.all_into());
let search_domains = Some(value.search_domains.all_into());
let allowed_ips = Some(value.allowed_ips.all_into());
create_hub(
value.name,
value.description,
Expand Down

0 comments on commit d722b45

Please sign in to comment.