aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFuwn <[email protected]>2023-04-16 00:47:52 -0700
committerFuwn <[email protected]>2023-04-16 00:47:52 -0700
commit0a0e09102f2ee86e6d7f424ad6f7048e7583f527 (patch)
tree334eb370ff09deae2b7997592d60aab0bf6d3db9
parentfix(timing): push module (what ?) (diff)
downloadlocus-0a0e09102f2ee86e6d7f424ad6f7048e7583f527.tar.xz
locus-0a0e09102f2ee86e6d7f424ad6f7048e7583f527.zip
refactor: make maros procedural
-rw-r--r--Cargo.toml4
-rw-r--r--Makefile.toml3
-rw-r--r--amenadiel/Cargo.toml14
-rw-r--r--amenadiel/src/lib.rs77
-rw-r--r--src/macros.rs14
-rw-r--r--src/main.rs2
-rw-r--r--src/modules.rs51
7 files changed, 115 insertions, 50 deletions
diff --git a/Cargo.toml b/Cargo.toml
index bd27e7b..b0c7e82 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -13,6 +13,9 @@ license = "GPL-3.0-only"
keywords = ["gemini"]
categories = ["web-programming"]
+[workspace]
+members = ["amenadiel"]
+
# Slower builds, faster executables
[profile.release]
opt-level = 3
@@ -43,6 +46,7 @@ germ = { version = "0.3.7", default-features = false, features = [
"ast"
] } # Gemini Tool-kit
chardetng = "0.1.17" # Character Encoding Detection
+amenadiel = { version = "*", path = "./amenadiel" } # Procedural Macros
[build-dependencies]
vergen = { version = "8.0.0", features = [
diff --git a/Makefile.toml b/Makefile.toml
index e7313c0..6cb8868 100644
--- a/Makefile.toml
+++ b/Makefile.toml
@@ -1,3 +1,6 @@
+[config]
+default_to_workspace = false
+
[tasks.fmt]
args = ["fmt"]
command = "cargo"
diff --git a/amenadiel/Cargo.toml b/amenadiel/Cargo.toml
new file mode 100644
index 0000000..c94115d
--- /dev/null
+++ b/amenadiel/Cargo.toml
@@ -0,0 +1,14 @@
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[package]
+name = "amenadiel"
+version = "0.0.0"
+authors = ["Fuwn <[email protected]>"]
+edition = "2021"
+license = "GPL-3.0-only"
+
+[lib]
+proc-macro = true
+
+[dependencies]
+quote = "1.0.26" # Quasi-quoting
diff --git a/amenadiel/src/lib.rs b/amenadiel/src/lib.rs
new file mode 100644
index 0000000..0c97269
--- /dev/null
+++ b/amenadiel/src/lib.rs
@@ -0,0 +1,77 @@
+// This file is part of Locus <https://github.com/gemrest/locus>.
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, version 3.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+//
+// Copyright (C) 2022-2023 Fuwn <[email protected]>
+// SPDX-License-Identifier: GPL-3.0-only
+
+#![deny(
+ warnings,
+ nonstandard_style,
+ unused,
+ future_incompatible,
+ rust_2018_idioms,
+ unsafe_code
+)]
+#![deny(clippy::all, clippy::nursery, clippy::pedantic)]
+#![recursion_limit = "128"]
+#![feature(proc_macro_span)]
+
+use proc_macro::TokenStream;
+use quote::quote;
+
+/// Legitimise and mount Locus modules
+///
+/// # Panics
+///
+/// This *should* never panic, but if it does, we don't care.
+#[proc_macro]
+pub fn modules(input: TokenStream) -> TokenStream {
+ let mut tokens = input.into_iter();
+ let mut span = if let Some(span) = tokens.next() {
+ span.span()
+ } else {
+ return quote::quote!().into();
+ };
+
+ for token in tokens {
+ span = span.join(token.span()).unwrap();
+ }
+
+ let mut legitimised_modules = quote!();
+ let mut modules = quote!();
+
+ span.source_text().unwrap().lines().for_each(|line| {
+ if line.contains("search") {
+ legitimised_modules.extend(quote!(
+ pub mod search;
+ ));
+ modules.extend(quote!(router.attach_stateless(search::module);));
+ } else {
+ let module_name =
+ quote::format_ident!("{}", line.trim_end_matches(',').trim_start());
+
+ legitimised_modules.extend(quote!(mod #module_name;));
+ modules.extend(quote!(router.attach_stateless(#module_name::module);));
+ }
+ });
+
+ quote! {
+ #legitimised_modules
+
+ pub fn module(router: &mut ::windmark::Router) {
+ #modules
+ }
+ }
+ .into()
+}
diff --git a/src/macros.rs b/src/macros.rs
index 30cb7a5..4b69e9d 100644
--- a/src/macros.rs
+++ b/src/macros.rs
@@ -102,17 +102,3 @@ macro_rules! batch_mount {
)*
};
}
-
-#[macro_export]
-macro_rules! stateless {
- ($router:ident, $module:tt) => {{
- $router.attach_stateless($module::module);
- }};
-}
-
-#[macro_export]
-macro_rules! statelesses {
- ($router:ident, $($module:tt),* $(,)?) => {
- $($crate::stateless!($router, $module);)*
- };
-}
diff --git a/src/main.rs b/src/main.rs
index efbe44d..faeddcc 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -84,7 +84,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
timing::time_section(&mut time_mount, "creating router");
timing::time_mounts("module", &mut time_mount, || {
- stateless!(router, modules);
+ router.attach_stateless(modules::module);
});
std::thread::spawn(modules::search::index);
diff --git a/src/modules.rs b/src/modules.rs
index 2d0b678..33afc88 100644
--- a/src/modules.rs
+++ b/src/modules.rs
@@ -16,38 +16,19 @@
// Copyright (C) 2022-2022 Fuwn <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only
-mod blog;
-mod contact;
-mod interests;
-mod random;
-mod remarks;
-// mod robots;
-mod api;
-mod cryptocurrency;
-mod router;
-pub mod search;
-mod sitemap;
-mod skills;
-mod r#static;
-mod stocks;
-mod uptime;
-
-pub fn module(router: &mut windmark::Router) {
- crate::statelesses!(
- router,
- uptime,
- sitemap,
- search,
- remarks,
- blog,
- random,
- r#static,
- router,
- skills,
- contact,
- interests,
- api,
- stocks,
- cryptocurrency,
- );
-}
+amenadiel::modules!(
+ uptime,
+ sitemap,
+ search,
+ remarks,
+ blog,
+ random,
+ r#static,
+ router,
+ skills,
+ contact,
+ interests,
+ api,
+ stocks,
+ cryptocurrency
+);