Skip to content

Commit b736c23

Browse files
committed
clean up implementation
1 parent 959e030 commit b736c23

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

src/webserver/database/sqlpage_functions/function_traits.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,24 @@ impl<'a, T: BorrowFromStr<'a> + Sized + 'a> FunctionParamType<'a> for SqlPageFun
8484
}
8585
}
8686

87+
impl<'a, T: BorrowFromStr<'a> + Sized + 'a> FunctionParamType<'a>
88+
for Option<SqlPageFunctionParam<T>>
89+
{
90+
type TargetType = Option<T>;
91+
92+
fn from_args(
93+
arg: &mut std::vec::IntoIter<Option<Cow<'a, str>>>,
94+
) -> anyhow::Result<Self::TargetType> {
95+
let param = <Option<Cow<'a, str>>>::from_args(arg)?;
96+
let res = if let Some(param) = param {
97+
Some(T::borrow_from_str(param)?)
98+
} else {
99+
None
100+
};
101+
Ok(res)
102+
}
103+
}
104+
87105
pub(super) trait FunctionResultType<'a> {
88106
fn into_cow_result(self) -> anyhow::Result<Option<Cow<'a, str>>>;
89107
}

src/webserver/database/sqlpage_functions/functions.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use super::function_traits::BorrowFromStr;
21
use super::{ExecutionContext, RequestInfo};
32
use crate::webserver::{
43
database::{
5-
blob_to_data_url::vec_to_data_uri_with_mime, execute_queries::DbConn,
6-
sqlpage_functions::url_parameters::URLParameters,
4+
blob_to_data_url::vec_to_data_uri_with_mime,
5+
execute_queries::DbConn,
6+
sqlpage_functions::{http_fetch_request::HttpFetchRequest, url_parameters::URLParameters},
77
},
88
http_client::make_http_client,
99
request_variables::SetVariablesMap,
@@ -27,8 +27,8 @@ super::function_definition_macro::sqlpage_functions! {
2727
environment_variable(name: Cow<str>);
2828
exec((&RequestInfo), program_name: Cow<str>, args: Vec<Cow<str>>);
2929

30-
fetch((&RequestInfo), http_request: Option<Cow<str>>);
31-
fetch_with_meta((&RequestInfo), http_request: Option<Cow<str>>);
30+
fetch((&RequestInfo), http_request: Option<SqlPageFunctionParam<HttpFetchRequest<'_>>>);
31+
fetch_with_meta((&RequestInfo), http_request: Option<SqlPageFunctionParam<HttpFetchRequest<'_>>>);
3232

3333
hash_password(password: Option<String>);
3434
header((&RequestInfo), name: Cow<str>);
@@ -186,14 +186,11 @@ fn prepare_request_body(
186186

187187
async fn fetch(
188188
request: &RequestInfo,
189-
http_request: Option<Cow<'_, str>>,
189+
http_request: Option<HttpFetchRequest<'_>>,
190190
) -> anyhow::Result<Option<String>> {
191-
let Some(http_request_str) = http_request else {
191+
let Some(http_request) = http_request else {
192192
return Ok(None);
193193
};
194-
let http_request =
195-
super::http_fetch_request::HttpFetchRequest::borrow_from_str(http_request_str)
196-
.with_context(|| "Invalid http fetch request")?;
197194
let client = make_http_client(&request.app_state.config)
198195
.with_context(|| "Unable to create an HTTP client")?;
199196
let req = build_request(&client, &http_request)?;
@@ -266,15 +263,13 @@ fn decode_response(response: Vec<u8>, encoding: Option<&str>) -> anyhow::Result<
266263

267264
async fn fetch_with_meta(
268265
request: &RequestInfo,
269-
http_request: Option<Cow<'_, str>>,
266+
http_request: Option<HttpFetchRequest<'_>>,
270267
) -> anyhow::Result<Option<String>> {
271268
use serde::{ser::SerializeMap, Serializer};
272-
let Some(http_request_str) = http_request else {
269+
270+
let Some(http_request) = http_request else {
273271
return Ok(None);
274272
};
275-
let http_request =
276-
super::http_fetch_request::HttpFetchRequest::borrow_from_str(http_request_str)
277-
.with_context(|| "Invalid http fetch request")?;
278273

279274
let client = make_http_client(&request.app_state.config)
280275
.with_context(|| "Unable to create an HTTP client")?;

0 commit comments

Comments
 (0)