diff options
| author | Fuwn <[email protected]> | 2023-04-16 21:15:12 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2023-04-16 21:15:12 -0700 |
| commit | c7adc5f014d4635c8468a863a124ddce4a9fe0e7 (patch) | |
| tree | 2f5a8aafdbf3fa0712d01ecb95ccd10eb691fd10 /src | |
| parent | fix(amenadiel): modules macro unwrap on none (diff) | |
| download | locus-c7adc5f014d4635c8468a863a124ddce4a9fe0e7.tar.xz locus-c7adc5f014d4635c8468a863a124ddce4a9fe0e7.zip | |
refactor(response): rewrite unnecessary macro to function
Diffstat (limited to 'src')
| -rw-r--r-- | src/macros.rs | 43 | ||||
| -rw-r--r-- | src/main.rs | 1 | ||||
| -rw-r--r-- | src/modules/blog/module.rs | 12 | ||||
| -rw-r--r-- | src/modules/contact.rs | 2 | ||||
| -rw-r--r-- | src/modules/cryptocurrency.rs | 14 | ||||
| -rw-r--r-- | src/modules/interests.rs | 6 | ||||
| -rw-r--r-- | src/modules/remarks.rs | 6 | ||||
| -rw-r--r-- | src/modules/search.rs | 2 | ||||
| -rw-r--r-- | src/modules/sitemap.rs | 6 | ||||
| -rw-r--r-- | src/modules/skills.rs | 2 | ||||
| -rw-r--r-- | src/modules/stocks.rs | 20 | ||||
| -rw-r--r-- | src/response.rs | 54 |
12 files changed, 91 insertions, 77 deletions
diff --git a/src/macros.rs b/src/macros.rs index 0d1e49d..55fefcd 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -16,47 +16,6 @@ // Copyright (C) 2022-2022 Fuwn <[email protected]> // SPDX-License-Identifier: GPL-3.0-only -use once_cell::sync::Lazy; - -pub static QUOTES: Lazy<Vec<String>> = Lazy::new(|| { - serde_json::from_str(include_str!("../content/json/quotes.json")).unwrap() -}); - -#[derive(yarte::Template)] -#[template(path = "main")] -pub struct Main<'a> { - pub body: &'a str, - pub hits: &'a i32, - pub quote: &'a str, - pub commit: &'a str, - pub mini_commit: &'a str, -} - -#[macro_export] -macro_rules! success { - ($body:expr, $context:ident) => {{ - $crate::route::cache(&$context, &$body); - - ::windmark::Response::success( - $crate::macros::Main { - body: &$body, - hits: &$crate::route::hits_from($context.url.path()), - quote: { - use ::rand::prelude::SliceRandom; - - &$crate::macros::QUOTES - .choose(&mut ::rand::thread_rng()) - .unwrap() - .to_string() - }, - commit: &format!("/tree/{}", env!("VERGEN_GIT_SHA")), - mini_commit: env!("VERGEN_GIT_SHA").get(0..5).unwrap_or("UNKNOWN"), - } - .to_string(), - ) - }}; -} - #[macro_export] macro_rules! batch_mount { ("pages", $router:ident, $(($path:literal, $description:literal, $file:literal)),* $(,)?) => { @@ -69,7 +28,7 @@ macro_rules! batch_mount { include_str!(concat!("../../content/static/", $file, ".gmi")); $crate::route::cache(&context, &content); - $crate::success!(content, context) + $crate::response::success(&content, &context) }); )* }; diff --git a/src/main.rs b/src/main.rs index 224583c..10fea13 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,6 +31,7 @@ mod macros; mod modules; +mod response; mod route; mod timing; mod xml; diff --git a/src/modules/blog/module.rs b/src/modules/blog/module.rs index a5cbcba..433a1f5 100644 --- a/src/modules/blog/module.rs +++ b/src/modules/blog/module.rs @@ -25,7 +25,7 @@ use std::{ use crate::{ modules::blog::config::Blog, route::track_mount, - success, + response::success, xml::{Item as XmlItem, Writer as XmlWriter}, }; @@ -101,7 +101,7 @@ pub fn module(router: &mut windmark::Router) { let blog_clone = blogs.clone(); track_mount(router, "/blog", "Fuwn's blogs", move |context| { - success!( + success( &format!( "# Blogs ({})\n\n{}", blog_clone.len(), @@ -137,7 +137,7 @@ pub fn module(router: &mut windmark::Router) { .collect::<Vec<_>>() .join("\n") ), - context + &context ) }); @@ -182,7 +182,7 @@ pub fn module(router: &mut windmark::Router) { move |context| { let fixed_blog_name = fixed_blog_name_clone.clone(); - success!( + success( &format!( "# {} ({})\n\n{}\n\n{}\n\n## Really Simple Syndication\n\nAccess \ {0}'s RSS feed\n\n=> {} here!", @@ -229,7 +229,7 @@ pub fn module(router: &mut windmark::Router) { .join("\n"), format_args!("/blog/{}.xml", fixed_blog_name), ), - context + &context ) }, ); @@ -290,7 +290,7 @@ pub fn module(router: &mut windmark::Router) { } ), move |context| { - success!(format!("{}\n{}", header.0, contents,), context) + success(&format!("{}\n{}", header.0, contents,), &context) }, ); } diff --git a/src/modules/contact.rs b/src/modules/contact.rs index f82922e..76781b9 100644 --- a/src/modules/contact.rs +++ b/src/modules/contact.rs @@ -51,7 +51,7 @@ pub fn module(router: &mut windmark::Router) { "/contact", "A Few Skills of Fuwn", move |context| { - crate::success!(format!("# Contact\n\n{}", contacts.join("\n")), context) + crate::response::success(&format!("# Contact\n\n{}", contacts.join("\n")), &context) }, ); } diff --git a/src/modules/cryptocurrency.rs b/src/modules/cryptocurrency.rs index 752c251..87f6ca0 100644 --- a/src/modules/cryptocurrency.rs +++ b/src/modules/cryptocurrency.rs @@ -19,7 +19,7 @@ use once_cell::sync::Lazy; use serde::Deserialize; -use crate::{route::track_mount, success}; +use crate::{route::track_mount, response::success}; static REFERRALS: Lazy<Vec<Referral>> = Lazy::new(|| { serde_json::from_str(include_str!( @@ -42,8 +42,8 @@ pub fn module(router: &mut windmark::Router) { "Want to start investing in cryptocurrency? Support me by using one of my \ referral links!", |context| { - success!( - format!( + success( + &format!( "# Referrals\n\n=> /cryptocurrency Home\n=> /stocks Stock Market \ Dashboard\n\nWant to start investing? Support me by using one of \ my referral links!\n\n{}", @@ -55,7 +55,7 @@ pub fn module(router: &mut windmark::Router) { .collect::<Vec<String>>() .join("\n") ), - context + &context ) }, ); @@ -65,12 +65,12 @@ pub fn module(router: &mut windmark::Router) { "/cryptocurrency", "Relevant information regarding cryptocurrency investing", |context| { - success!( - format!( + success( + &format!( "# Cryptocurrency\n\n=> /stocks Stock Market Dashboard\n=> \ /cryptocurrency/referrals Referrals", ), - context + &context ) }, ); diff --git a/src/modules/interests.rs b/src/modules/interests.rs index 6762e55..90d96eb 100644 --- a/src/modules/interests.rs +++ b/src/modules/interests.rs @@ -51,13 +51,13 @@ pub fn module(router: &mut windmark::Router) { "/interests", "A Few Interests of Fuwn", move |context| { - crate::success!( - format!( + crate::response::success( + &format!( "# Interests\n\nA collection of things that I think are pretty neat \ and I am considering using more in the future.\n\n{}", contacts.join("\n") ), - context + &context ) }, ); diff --git a/src/modules/remarks.rs b/src/modules/remarks.rs index 29ad684..0b5fc26 100644 --- a/src/modules/remarks.rs +++ b/src/modules/remarks.rs @@ -37,8 +37,8 @@ pub fn module(router: &mut windmark::Router) { "Fuwn's thoughts which are too short to be their own blog; but just long \ enough to be a remark.", |context| { - crate::success!( - format!( + crate::response::success( + &format!( "# Remarks\n\nFuwn's thoughts which are too short to be their own \ blog; but just long enough to be a remark.\n\n{}", REMARKS @@ -57,7 +57,7 @@ pub fn module(router: &mut windmark::Router) { .collect::<Vec<String>>() .join("\n") ), - context + &context ) }, ); diff --git a/src/modules/search.rs b/src/modules/search.rs index 5514b2f..d5fdc1f 100644 --- a/src/modules/search.rs +++ b/src/modules/search.rs @@ -161,7 +161,7 @@ pub(super) fn module(router: &mut windmark::Router) { } } - crate::success!(response, context) + crate::response::success(&response, &context) }, ); } diff --git a/src/modules/sitemap.rs b/src/modules/sitemap.rs index 10ae569..19c6b82 100644 --- a/src/modules/sitemap.rs +++ b/src/modules/sitemap.rs @@ -22,8 +22,8 @@ pub fn module(router: &mut windmark::Router) { "/sitemap", "A map of all publicly available routes on this Gemini capsule", |context| { - crate::success!( - format!( + crate::response::success( + &format!( "# Sitemap\n\nA map of all publicly available routes on this Gemini \ capsule\n\n{}", (*crate::route::ROUTES.lock().unwrap()) @@ -32,7 +32,7 @@ pub fn module(router: &mut windmark::Router) { .collect::<Vec<_>>() .join("\n") ), - context + &context ) }, ); diff --git a/src/modules/skills.rs b/src/modules/skills.rs index ce97f3e..45b42d9 100644 --- a/src/modules/skills.rs +++ b/src/modules/skills.rs @@ -64,7 +64,7 @@ pub fn module(router: &mut windmark::Router) { "/skills", "A Few Skills of Fuwn", move |context| { - crate::success!(format!("# Skills\n\n{}", skills.join("\n")), context) + crate::response::success(&format!("# Skills\n\n{}", skills.join("\n")), &context) }, ); } diff --git a/src/modules/stocks.rs b/src/modules/stocks.rs index ff1a77d..20ab850 100644 --- a/src/modules/stocks.rs +++ b/src/modules/stocks.rs @@ -19,7 +19,7 @@ use once_cell::sync::Lazy; use serde::Deserialize; -use crate::{route::track_mount, success}; +use crate::{route::track_mount, response::success}; static REFERRALS: Lazy<Vec<Referral>> = Lazy::new(|| { serde_json::from_str(include_str!( @@ -112,8 +112,8 @@ pub fn module(router: &mut windmark::Router) { "Want to start investing in the stock market? Support me by using one of \ my referral links!", |context| { - success!( - format!( + success( + &format!( "# Referrals\n\n=> /stocks Dashboard\n=> /cryptocurrency \ Cryptocurrency Dashboard\n=> /stocks/telegram Telegram Groups\n=> \ /stocks/search Search\n\nWant to start investing? Support me by \ @@ -126,7 +126,7 @@ pub fn module(router: &mut windmark::Router) { .collect::<Vec<String>>() .join("\n") ), - context + &context ) }, ); @@ -137,14 +137,14 @@ pub fn module(router: &mut windmark::Router) { "Explore and search the stock market using Gemini!", |context| { async move { - success!( - format!( + success( + &format!( "# The Stock Market\n\n=> /stocks/search Symbol Search\n=> /stocks/referrals Referrals\n=> /cryptocurrency Cryptocurrency Dashboard\n=> /stocks/telegram Telegram Groups\n\n## Popular \ Symbols\n\n### AAPL\n\n{}\n\n### TSLA\n\n{}\n\n## Credits\n\nFinancial data provided by\n\n=> https://finnhub.io/ Finnhub", symbol_to_string("AAPL").await, symbol_to_string("TSLA").await ), - context + &context ) } }, @@ -190,12 +190,12 @@ pub fn module(router: &mut windmark::Router) { } } - success!( - format!( + success( + &format!( "{}\n\n## Credits\n\nFinancial data provided by\n\n=> https://finnhub.io/ Finnhub", response ), - context + &context ) } }, diff --git a/src/response.rs b/src/response.rs new file mode 100644 index 0000000..5661938 --- /dev/null +++ b/src/response.rs @@ -0,0 +1,54 @@ +// 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 + +use once_cell::sync::Lazy; + +static QUOTES: Lazy<Vec<String>> = Lazy::new(|| { + serde_json::from_str(include_str!("../content/json/quotes.json")).unwrap() +}); + +#[derive(yarte::Template)] +#[template(path = "main")] +struct Main<'a> { + pub body: &'a str, + pub hits: &'a i32, + pub quote: &'a str, + pub commit: &'a str, + pub mini_commit: &'a str, +} + +pub fn success( + body: &impl ToString, + context: &windmark::context::RouteContext, +) -> windmark::Response { + crate::route::cache(context, &body.to_string()); + + windmark::Response::success( + Main { + body: &body.to_string(), + hits: &crate::route::hits_from(context.url.path()), + quote: { + use rand::prelude::SliceRandom; + + "ES.choose(&mut rand::thread_rng()).unwrap().to_string() + }, + commit: &format!("/tree/{}", env!("VERGEN_GIT_SHA")), + mini_commit: env!("VERGEN_GIT_SHA").get(0..5).unwrap_or("UNKNOWN"), + } + .to_string(), + ) +} |