Skip to content

Commit 9690e3c

Browse files
authored
fix(rust) :: format the files cargo fmt could not see (#1378)
* fix(functions) :: don't use macro to import and build SqlPageFunctionName * fix(rust) :: format codebase
1 parent 0f7eba5 commit 9690e3c

16 files changed

Lines changed: 173 additions & 76 deletions

.git-blame-ignore-revs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# ignore commits from showing up on git diffs.
2+
3+
# === large formatting commits ===
4+
# TODO :: add commit once merged into mainline

AGENTS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ official documentation website sql tables:
140140
#### Project Conventions
141141

142142
- Built-in UI component templates: `sqlpage/templates/*.handlebars`; header/control components: `src/render.rs`.
143-
- SQLPage functions: one `async fn` module under `src/webserver/database/sqlpage_functions/functions/`, registered with `sqlpage_functions!` in `functions.rs`.
143+
- SQLPage functions: one `async fn` module under `src/webserver/database/sqlpage_functions/functions/`, declared with `mod` and registered with `sqlpage_functions!` in `functions.rs`. See its [README](./src/webserver/database/sqlpage_functions/README.md).
144144
- [Configuration](./configuration.md): see [AppConfig](./src/app_config.rs)
145145
- Routing: file-based in `src/webserver/routing.rs`. Missing paths use the nearest ancestor `404.sql`; without one, HTML uses `src/default_404.sql` and other formats receive a plain-text 404.
146146
- Follow patterns from similar modules before introducing new abstractions.

src/webserver/database/sqlpage_functions/README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,20 @@ pub(super) async fn example(request: &RequestInfo, value: Option<Cow<'_, str>>)
1414
}
1515
```
1616

17-
To add `sqlpage.example`, create `functions/example.rs` and add it to the
18-
[`sqlpage_functions!`](function_traits.rs) call in [`functions.rs`](functions.rs):
17+
To add `sqlpage.example`, create `functions/example.rs`, declare its module in
18+
[`functions.rs`](functions.rs) and add it to the [`sqlpage_functions!`](function_traits.rs) call in
19+
the same file:
1920

2021
```rust
22+
mod example;
23+
2124
sqlpage_functions! {
2225
// ...
2326
example,
2427
}
2528
```
2629

27-
The [`sqlpage_functions!`](function_traits.rs) macro declares the modules and generates the
30+
The [`sqlpage_functions!`](function_traits.rs) macro generates the
2831
`SqlPageFunctionName` enum the SQL engine dispatches on. Per-function argument extraction, dispatch,
2932
and return-value conversion are handled generically in [`function_traits.rs`](function_traits.rs) by
3033
the `Extract`, `Handler`, and `IntoCowResult` traits. A function's argument and return types are read

src/webserver/database/sqlpage_functions/function_traits.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,9 @@ impl<'a, T: IntoCow<'a>> IntoCow<'a> for Option<T> {
222222
}
223223
}
224224

225-
/// Declares the listed function modules and builds the [`SqlPageFunctionName`] dispatch enum from
226-
/// them.
225+
/// Builds the [`SqlPageFunctionName`] dispatch enum from the listed function modules.
227226
macro_rules! sqlpage_functions {
228227
($($func:ident),* $(,)?) => {
229-
$(
230-
mod $func;
231-
)*
232228

233229
/// One variant per built-in `sqlpage.*` function.
234230
#[derive(Debug, PartialEq, Eq, Clone, Copy)]

src/webserver/database/sqlpage_functions/functions.rs

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,53 @@
11
//! Built-in `SQLPage` SQL functions.
22
//!
33
//! Every function is a plain `async fn` in its own module under [`functions/`](self). To add one,
4-
//! create `functions/<name>.rs` with an `async fn <name>` and add it to the
5-
//! [`sqlpage_functions!`](super::function_traits::sqlpage_functions) call below. The macro declares
6-
//! the module and adds it to the dispatch enum. Argument conversion and
7-
//! dispatch are handled generically in [`super::function_traits`].
4+
//! create `functions/<name>.rs` with an `async fn <name>`, declare the module below and add it to
5+
//! the [`sqlpage_functions!`](super::function_traits::sqlpage_functions) call. Argument conversion
6+
//! and dispatch are handled generically in [`super::function_traits`].
87
98
use std::fmt::Write;
109

1110
use super::function_traits::sqlpage_functions;
1211

12+
mod basic_auth_password;
13+
mod basic_auth_username;
14+
mod client_ip;
15+
mod configuration_directory;
16+
mod cookie;
17+
mod current_working_directory;
18+
mod environment_variable;
19+
mod exec;
20+
mod fetch;
21+
mod fetch_with_meta;
22+
mod hash_password;
23+
mod header;
24+
mod headers;
25+
mod hmac;
26+
mod link;
27+
mod oidc_logout_url;
28+
mod path;
29+
mod persist_uploaded_file;
30+
mod protocol;
31+
mod random_string;
32+
mod read_file_as_data_url;
33+
mod read_file_as_text;
34+
mod regex_match;
35+
mod request_body;
36+
mod request_body_base64;
37+
mod request_method;
38+
mod run_sql;
39+
mod send_mail;
40+
mod set_variable;
41+
mod uploaded_file_mime_type;
42+
mod uploaded_file_name;
43+
mod uploaded_file_path;
44+
mod url_encode;
45+
mod user_info;
46+
mod user_info_token;
47+
mod variables;
48+
mod version;
49+
mod web_root;
50+
1351
sqlpage_functions! {
1452
basic_auth_password,
1553
basic_auth_username,
@@ -82,3 +120,33 @@ fn supported_function_list() -> String {
82120
}
83121
supported
84122
}
123+
124+
#[cfg(test)]
125+
mod tests {
126+
use super::SqlPageFunctionName;
127+
use std::collections::BTreeSet;
128+
129+
#[test]
130+
fn functions_directory_matches_registered_functions() {
131+
let directory = concat!(
132+
env!("CARGO_MANIFEST_DIR"),
133+
"/src/webserver/database/sqlpage_functions/functions"
134+
);
135+
let files: BTreeSet<String> = std::fs::read_dir(directory)
136+
.expect("functions directory")
137+
.map(|entry| entry.expect("directory entry").path())
138+
.filter(|path| path.extension().is_some_and(|extension| extension == "rs"))
139+
.map(|path| {
140+
path.file_stem()
141+
.expect("file stem")
142+
.to_string_lossy()
143+
.into_owned()
144+
})
145+
.collect();
146+
let registered: BTreeSet<String> = SqlPageFunctionName::ALL
147+
.iter()
148+
.map(|function| function.name().to_owned())
149+
.collect();
150+
assert_eq!(files, registered);
151+
}
152+
}

src/webserver/database/sqlpage_functions/functions/cookie.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ use std::borrow::Cow;
22

33
use crate::webserver::{http_request_info::RequestInfo, single_or_vec::SingleOrVec};
44

5-
pub(super) async fn cookie<'a>(request: &'a RequestInfo, name: Cow<'a, str>) -> Option<Cow<'a, str>> {
5+
pub(super) async fn cookie<'a>(
6+
request: &'a RequestInfo,
7+
name: Cow<'a, str>,
8+
) -> Option<Cow<'a, str>> {
69
request.cookies.get(&*name).map(SingleOrVec::as_json_str)
710
}

src/webserver/database/sqlpage_functions/functions/environment_variable.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ use std::borrow::Cow;
33
use anyhow::Context;
44

55
/// Returns the value of an environment variable.
6-
pub(super) async fn environment_variable(name: Cow<'_, str>) -> anyhow::Result<Option<Cow<'_, str>>> {
6+
pub(super) async fn environment_variable(
7+
name: Cow<'_, str>,
8+
) -> anyhow::Result<Option<Cow<'_, str>>> {
79
match std::env::var(&*name) {
810
Ok(value) => Ok(Some(Cow::Owned(value))),
911
Err(std::env::VarError::NotPresent) if name.contains(['=', '\0']) => anyhow::bail!(

src/webserver/database/sqlpage_functions/functions/fetch.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ use tracing::Instrument;
66

77
use crate::webserver::{
88
database::sqlpage_functions::http_fetch_request::HttpFetchRequest,
9-
http_client::make_http_client,
10-
http_request_info::RequestInfo,
9+
http_client::make_http_client, http_request_info::RequestInfo,
1110
};
1211

1312
pub(super) fn build_request<'a>(
@@ -94,8 +93,8 @@ pub(super) async fn fetch(
9493

9594
async {
9695
let response_result = send_request(request, &http_request)?.await;
97-
let mut response = response_result
98-
.map_err(|e| anyhow!("Unable to fetch {}: {e}", http_request.url))?;
96+
let mut response =
97+
response_result.map_err(|e| anyhow!("Unable to fetch {}: {e}", http_request.url))?;
9998

10099
tracing::Span::current().record(
101100
otel::HTTP_RESPONSE_STATUS_CODE,

src/webserver/database/sqlpage_functions/functions/header.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ use std::borrow::Cow;
22

33
use crate::webserver::{http_request_info::RequestInfo, single_or_vec::SingleOrVec};
44

5-
pub(super) async fn header<'a>(request: &'a RequestInfo, name: Cow<'a, str>) -> Option<Cow<'a, str>> {
5+
pub(super) async fn header<'a>(
6+
request: &'a RequestInfo,
7+
name: Cow<'a, str>,
8+
) -> Option<Cow<'a, str>> {
69
let lower_name = name.to_ascii_lowercase();
710
request
811
.headers

src/webserver/database/sqlpage_functions/functions/persist_uploaded_file.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ pub(super) async fn persist_uploaded_file<'a>(
7272
}
7373

7474
#[cfg(unix)]
75-
pub(super) async fn set_file_mode(path: &std::path::Path, mode: Option<&str>) -> anyhow::Result<()> {
75+
pub(super) async fn set_file_mode(
76+
path: &std::path::Path,
77+
mode: Option<&str>,
78+
) -> anyhow::Result<()> {
7679
use std::os::unix::fs::PermissionsExt;
7780
let mode = if let Some(mode) = mode {
7881
u32::from_str_radix(mode, 8)
@@ -87,6 +90,9 @@ pub(super) async fn set_file_mode(path: &std::path::Path, mode: Option<&str>) ->
8790
}
8891

8992
#[cfg(not(unix))]
90-
pub(super) async fn set_file_mode(_path: &std::path::Path, _mode: Option<&str>) -> anyhow::Result<()> {
93+
pub(super) async fn set_file_mode(
94+
_path: &std::path::Path,
95+
_mode: Option<&str>,
96+
) -> anyhow::Result<()> {
9197
Ok(())
9298
}

0 commit comments

Comments
 (0)