Skip to content

Commit

Permalink
make value Optional
Browse files Browse the repository at this point in the history
  • Loading branch information
j03-dev committed Oct 13, 2023
1 parent bc033d5 commit d6f123d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub async fn webhook_core(data: Json<MsgFromFb>, state: &State<AppState>) -> &'s
.get(payload.get_action().as_str())
{
action_fn
.execute(user_id, payload.get_value(), user_conn)
.execute(user_id, &payload.get_value(), user_conn)
.await;
}
}
Expand Down
27 changes: 9 additions & 18 deletions src/response_models/payload.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,31 @@
use url::form_urlencoded;

#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Default, Clone, PartialEq)]
pub struct Payload {
action: String,
key: String,
value: String,
value: Option<String>,
}

impl Payload {
pub fn new(action: &str, key: &str, value: &str) -> Self {
pub fn new(action: &str, value: Option<String>) -> Self {
Self {
action: action.into(),
key: key.into(),
value: value.into(),
value,
}
}

pub fn get_action(&self) -> &String {
&self.action
}

pub fn get_key(&self) -> &String {
&self.key
}

pub fn get_value(&self) -> &String {
&self.value
pub fn get_value(&self) -> String {
self.value.clone().unwrap_or("none".to_string())
}

pub fn to_uri_string(&self) -> String {
form_urlencoded::Serializer::new(String::new())
.append_pair("action", self.get_action())
.append_pair("key", self.get_key())
.append_pair("value", self.get_value())
.append_pair("value", &self.get_value())
.finish()
}

Expand All @@ -42,20 +35,18 @@ impl Payload {
.collect();

let mut action = None;
let mut key = None;
let mut value = None;

for (name, val) in parsed {
match &name[..] {
"action" => action = Some(val),
"key" => key = Some(val),
"value" => value = Some(val),
_ => return Err(format!("Unknown field in URI: {}", name)),
}
}

match (action, key, value) {
(Some(action), Some(key), Some(value)) => Ok(Self::new(&action, &key, &value)),
match (action, value) {
(Some(action), Some(value)) => Ok(Self::new(&action, Some(value))),
_ => Err("Missing fields in URI".to_string()),
}
}
Expand Down

0 comments on commit d6f123d

Please sign in to comment.