Skip to content

Commit 7a65f88

Browse files
committed
factor url parameter setting code
1 parent a59a391 commit 7a65f88

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

src/webserver/database/sqlpage_functions/functions.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -377,11 +377,7 @@ async fn link<'a>(
377377
let encoded = serde_json::from_str::<URLParameters>(&parameters).with_context(|| {
378378
format!("link: invalid URL parameters: not a valid json object:\n{parameters}")
379379
})?;
380-
let encoded_str = encoded.get();
381-
if !encoded_str.is_empty() {
382-
url.push('?');
383-
url.push_str(encoded_str);
384-
}
380+
encoded.append_to_url(&mut url);
385381
}
386382
if let Some(hash) = hash {
387383
url.push('#');
@@ -632,11 +628,7 @@ async fn set_variable<'a>(
632628
}
633629

634630
let mut url = context.path.clone();
635-
let encoded_str = params.get();
636-
if !encoded_str.is_empty() {
637-
url.push('?');
638-
url.push_str(encoded_str);
639-
}
631+
params.append_to_url(&mut url);
640632

641633
Ok(url)
642634
}

src/webserver/database/sqlpage_functions/url_parameter_deserializer.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,11 @@ impl URLParameters {
6868
}
6969
}
7070

71-
pub fn get(&self) -> &str {
72-
&self.0
71+
pub fn append_to_url(&self, url: &mut String) {
72+
if !self.0.is_empty() {
73+
url.push('?');
74+
url.push_str(&self.0);
75+
}
7376
}
7477
}
7578

@@ -107,6 +110,12 @@ impl<'de> Deserialize<'de> for URLParameters {
107110
}
108111
}
109112

113+
impl std::fmt::Display for URLParameters {
114+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
115+
write!(f, "{}", self.0)
116+
}
117+
}
118+
110119
#[test]
111120
fn test_url_parameters_deserializer() {
112121
use serde_json::json;
@@ -166,12 +175,12 @@ fn test_url_parameters_deserializer_issue_879() {
166175
fn test_push_single_or_vec() {
167176
let mut params = URLParameters(String::new());
168177
params.push_single_or_vec("k", SingleOrVec::Single("v".to_string()));
169-
assert_eq!(params.get(), "k=v");
178+
assert_eq!(params.to_string(), "k=v");
170179

171180
let mut params = URLParameters(String::new());
172181
params.push_single_or_vec(
173182
"arr",
174183
SingleOrVec::Vec(vec!["a".to_string(), "b".to_string()]),
175184
);
176-
assert_eq!(params.get(), "arr[]=a&arr[]=b");
185+
assert_eq!(params.to_string(), "arr[]=a&arr[]=b");
177186
}

0 commit comments

Comments
 (0)