Skip to content

Commit

Permalink
optimize with default derive
Browse files Browse the repository at this point in the history
  • Loading branch information
Mon-ius committed Feb 19, 2024
1 parent 806ed71 commit 23b2229
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 56 deletions.
2 changes: 1 addition & 1 deletion rws-cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rws-cli"
version = "0.1.12"
version = "0.1.13"
edition = "2021"

[dependencies]
Expand Down
80 changes: 29 additions & 51 deletions rws/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,94 +3,72 @@ use crate::wireguard::crypto;
use serde::Deserialize;
use std::fmt;

#[derive(Deserialize)]
#[derive(Deserialize, Default)]
pub struct WClientBuilder<'w> {
_id: Option<&'w str>,
_sec: Option<&'w str>,
_key: Option<&'w str>,
_token: Option<&'w str>,
_model: Option<&'w str>,
_type: Option<&'w str>,
_tos: Option<&'w str>,
_locale: Option<&'w str>,
_id: &'w str,
_sec: &'w str,
_key: &'w str,
_token: &'w str,
_model: &'w str,
_type: &'w str,
_tos: &'w str,
_locale: &'w str,
}

impl<'w> fmt::Debug for WClientBuilder<'w> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Client")
.field("install_id", &self._id.unwrap_or("default_id"))
.field("private_key", &self._sec.unwrap_or("default_private_key"))
.field("public_key", &self._key.unwrap_or("default_public_key"))
.field("install_id", &self._id)
.field("private_key", &self._sec)
.field("public_key", &self._key)
.field("fcm_token", &format!("{}:APA91b{}",
&self._id.unwrap_or("default_id"),
&self._token.unwrap_or("default_token"))
&self._id,
&self._token)
)
.field("device_model", &self._model.unwrap_or("default_model"))
.field("device_type", &self._type.unwrap_or("default_type"))
.field("datetime", &self._tos.unwrap_or("default_tos"))
.field("locale", &self._locale.unwrap_or("default_locale"))
.field("device_model", &self._model)
.field("device_type", &self._type)
.field("datetime", &self._tos)
.field("locale", &self._locale)
.finish()
}
}

impl<'w> WClientBuilder<'w> {
pub fn new() -> WClientBuilder<'w> {
Self {
_id: None,
_sec: None,
_key: None,
_token: None,
_model: None,
_type: None,
_tos: None,
_locale: None,
}
}

pub fn w_id(mut self, _id: &'w str) -> Self {
self._id = Some(_id);
self._id = _id;
self
}
pub fn w_sec(mut self, _sec: &'w str) -> Self {
self._sec = Some(_sec);
self._sec = _sec;
self
}
pub fn w_key(mut self, _key: &'w str) -> Self {
self._key = Some(_key);
self._key = _key;
self
}
pub fn w_token(mut self, _token: &'w str) -> Self {
self._token = Some(_token);
self._token = _token;
self
}
pub fn w_model(mut self, _model: &'w str) -> Self {
self._model = Some(_model);
self._model = _model;
self
}
pub fn w_type(mut self, _type: &'w str) -> Self {
self._type = Some(_type);
self._type = _type;
self
}
pub fn w_tos(mut self, _tos: &'w str) -> Self {
self._tos = Some(_tos);
self._tos = _tos;
self
}
pub fn w_locale(mut self, _locale: &'w str) -> Self {
self._locale = Some(_locale);
self._locale = _locale;
self
}

pub fn build(self) -> WClientBuilder<'w> {
WClientBuilder {
_id: Some(self._id.unwrap_or("default_id")),
_sec: Some(self._sec.unwrap_or("default_private_key")),
_key: Some(self._key.unwrap_or("default_public_key")),
_token: Some(self._token.unwrap_or("default_token")),
_model: Some(self._model.unwrap_or("default_model")),
_type: Some(self._type.unwrap_or("default_type")),
_tos: Some(self._tos.unwrap_or("default_tos")),
_locale: Some(self._locale.unwrap_or("default_locale")),
}
self
}

pub fn random_id(self) -> Self {
Expand Down Expand Up @@ -122,7 +100,7 @@ impl<'w> WClientBuilder<'w> {
.w_locale(z)
}
pub fn random() -> Self {
WClientBuilder::new()
WClientBuilder::default()
.random_id()
.wg_key()
.random_token()
Expand All @@ -131,7 +109,7 @@ impl<'w> WClientBuilder<'w> {
.build()
}
pub fn from_id(_id:&'w str) -> Self {
WClientBuilder::new()
WClientBuilder::default()
.w_id(_id)
.wg_key()
.random_token()
Expand Down
9 changes: 5 additions & 4 deletions rws/src/warp/api.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use serde::Deserialize;

#[derive(Deserialize, Debug)]
pub struct WarpAPI {
_endpoint: String,
_ver: String,
#[derive(Deserialize)]
pub struct Cloudflare<'w> {
_endpoint: Option<&'w str>,
_ver: Option<&'w str>,
_headers: Option<&'w str>
}

0 comments on commit 23b2229

Please sign in to comment.