Skip to content

Commit 72e8ac8

Browse files
committed
Refactor: Move SingleOrVec to a dedicated module and update imports
1 parent 790f729 commit 72e8ac8

File tree

8 files changed

+92
-91
lines changed

8 files changed

+92
-91
lines changed

src/webserver/database/execute_queries.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ use super::sql::{
1515
use crate::dynamic_component::parse_dynamic_rows;
1616
use crate::utils::add_value_to_map;
1717
use crate::webserver::database::sql_to_json::row_to_string;
18-
use crate::webserver::http::SingleOrVec;
1918
use crate::webserver::http_request_info::RequestInfo;
19+
use crate::webserver::single_or_vec::SingleOrVec;
2020

2121
use super::syntax_tree::{extract_req_param, StmtParam};
2222
use super::{error_highlighting::display_db_error, Database, DbItem};

src/webserver/database/sqlpage_functions/functions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use crate::webserver::{
44
blob_to_data_url::vec_to_data_uri_with_mime, execute_queries::DbConn,
55
sqlpage_functions::url_parameter_deserializer::URLParameters,
66
},
7-
http::SingleOrVec,
87
http_client::make_http_client,
98
request_variables::ParamMap,
9+
single_or_vec::SingleOrVec,
1010
ErrorWithStatus,
1111
};
1212
use anyhow::{anyhow, Context};

src/webserver/database/syntax_tree.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use std::str::FromStr;
1616

1717
use sqlparser::ast::FunctionArg;
1818

19-
use crate::webserver::http::SingleOrVec;
2019
use crate::webserver::http_request_info::RequestInfo;
20+
use crate::webserver::single_or_vec::SingleOrVec;
2121

2222
use super::{
2323
execute_queries::DbConn, sql::function_args_to_stmt_params,

src/webserver/http.rs

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ use anyhow::{bail, Context};
3434
use chrono::{DateTime, Utc};
3535
use futures_util::stream::Stream;
3636
use futures_util::StreamExt;
37-
use serde::de::Error as _;
38-
use std::borrow::Cow;
39-
use std::mem;
4037
use std::path::PathBuf;
4138
use std::pin::Pin;
4239
use std::sync::Arc;
@@ -227,89 +224,6 @@ async fn render_sql(
227224
resp_recv.await.map_err(ErrorInternalServerError)
228225
}
229226

230-
#[derive(Debug, serde::Serialize, PartialEq, Clone)]
231-
#[serde(untagged)]
232-
pub enum SingleOrVec {
233-
Single(String),
234-
Vec(Vec<String>),
235-
}
236-
237-
impl<'de> serde::Deserialize<'de> for SingleOrVec {
238-
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
239-
where
240-
D: serde::Deserializer<'de>,
241-
{
242-
let value = serde_json::Value::deserialize(deserializer)?;
243-
match value {
244-
serde_json::Value::String(s) => Ok(SingleOrVec::Single(s)),
245-
serde_json::Value::Array(values) => {
246-
let mut strings = Vec::with_capacity(values.len());
247-
for (idx, item) in values.into_iter().enumerate() {
248-
match item {
249-
serde_json::Value::String(s) => strings.push(s),
250-
other => {
251-
return Err(D::Error::custom(format!(
252-
"expected an array of strings, but item at index {idx} is {other}"
253-
)))
254-
}
255-
}
256-
}
257-
Ok(SingleOrVec::Vec(strings))
258-
}
259-
other => Err(D::Error::custom(format!(
260-
"expected a string or an array of strings, but found {other}"
261-
))),
262-
}
263-
}
264-
}
265-
266-
impl std::fmt::Display for SingleOrVec {
267-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
268-
match self {
269-
SingleOrVec::Single(x) => write!(f, "{x}"),
270-
SingleOrVec::Vec(v) => {
271-
write!(f, "[")?;
272-
let mut it = v.iter();
273-
if let Some(first) = it.next() {
274-
write!(f, "{first}")?;
275-
}
276-
for item in it {
277-
write!(f, ", {item}")?;
278-
}
279-
write!(f, "]")
280-
}
281-
}
282-
}
283-
}
284-
285-
impl SingleOrVec {
286-
pub(crate) fn merge(&mut self, other: Self) {
287-
match (self, other) {
288-
(Self::Single(old), Self::Single(new)) => *old = new,
289-
(old, mut new) => {
290-
let mut v = old.take_vec();
291-
v.extend_from_slice(&new.take_vec());
292-
*old = Self::Vec(v);
293-
}
294-
}
295-
}
296-
297-
fn take_vec(&mut self) -> Vec<String> {
298-
match self {
299-
SingleOrVec::Single(x) => vec![mem::take(x)],
300-
SingleOrVec::Vec(v) => mem::take(v),
301-
}
302-
}
303-
304-
#[must_use]
305-
pub fn as_json_str(&self) -> Cow<'_, str> {
306-
match self {
307-
SingleOrVec::Single(x) => Cow::Borrowed(x),
308-
SingleOrVec::Vec(v) => Cow::Owned(serde_json::to_string(v).unwrap()),
309-
}
310-
}
311-
}
312-
313227
async fn process_sql_request(
314228
req: &mut ServiceRequest,
315229
sql_path: PathBuf,

src/webserver/http_request_info.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,8 @@ async fn is_file_field_empty(
278278

279279
#[cfg(test)]
280280
mod test {
281-
use super::super::http::SingleOrVec;
282281
use super::*;
282+
use crate::webserver::single_or_vec::SingleOrVec;
283283
use crate::{app_config::AppConfig, webserver::server_timing::ServerTiming};
284284
use actix_web::{http::header::ContentType, test::TestRequest};
285285

src/webserver/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,5 @@ pub use database::migrations::apply;
4848
pub mod oidc;
4949
pub mod response_writer;
5050
pub mod routing;
51+
mod single_or_vec;
5152
mod static_content;

src/webserver/request_variables.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::collections::{hash_map::Entry, HashMap};
22

3-
use super::http::SingleOrVec;
3+
use crate::webserver::single_or_vec::SingleOrVec;
44

55
pub type ParamMap = HashMap<String, SingleOrVec>;
66

src/webserver/single_or_vec.rs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
use serde::de::Error;
2+
use std::borrow::Cow;
3+
use std::mem;
4+
5+
#[derive(Debug, serde::Serialize, PartialEq, Clone)]
6+
#[serde(untagged)]
7+
pub enum SingleOrVec {
8+
Single(String),
9+
Vec(Vec<String>),
10+
}
11+
12+
impl<'de> serde::Deserialize<'de> for SingleOrVec {
13+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
14+
where
15+
D: serde::Deserializer<'de>,
16+
{
17+
let value = serde_json::Value::deserialize(deserializer)?;
18+
match value {
19+
serde_json::Value::String(s) => Ok(SingleOrVec::Single(s)),
20+
serde_json::Value::Array(values) => {
21+
let mut strings = Vec::with_capacity(values.len());
22+
for (idx, item) in values.into_iter().enumerate() {
23+
match item {
24+
serde_json::Value::String(s) => strings.push(s),
25+
other => {
26+
return Err(D::Error::custom(format!(
27+
"expected an array of strings, but item at index {idx} is {other}"
28+
)))
29+
}
30+
}
31+
}
32+
Ok(SingleOrVec::Vec(strings))
33+
}
34+
other => Err(D::Error::custom(format!(
35+
"expected a string or an array of strings, but found {other}"
36+
))),
37+
}
38+
}
39+
}
40+
41+
impl std::fmt::Display for SingleOrVec {
42+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43+
match self {
44+
SingleOrVec::Single(x) => write!(f, "{x}"),
45+
SingleOrVec::Vec(v) => {
46+
write!(f, "[")?;
47+
let mut it = v.iter();
48+
if let Some(first) = it.next() {
49+
write!(f, "{first}")?;
50+
}
51+
for item in it {
52+
write!(f, ", {item}")?;
53+
}
54+
write!(f, "]")
55+
}
56+
}
57+
}
58+
}
59+
60+
impl SingleOrVec {
61+
pub(crate) fn merge(&mut self, other: Self) {
62+
match (self, other) {
63+
(Self::Single(old), Self::Single(new)) => *old = new,
64+
(old, mut new) => {
65+
let mut v = old.take_vec();
66+
v.extend_from_slice(&new.take_vec());
67+
*old = Self::Vec(v);
68+
}
69+
}
70+
}
71+
72+
fn take_vec(&mut self) -> Vec<String> {
73+
match self {
74+
SingleOrVec::Single(x) => vec![mem::take(x)],
75+
SingleOrVec::Vec(v) => mem::take(v),
76+
}
77+
}
78+
79+
#[must_use]
80+
pub fn as_json_str(&self) -> Cow<'_, str> {
81+
match self {
82+
SingleOrVec::Single(x) => Cow::Borrowed(x),
83+
SingleOrVec::Vec(v) => Cow::Owned(serde_json::to_string(v).unwrap()),
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)