From 784111e22cc43d403fbc6703fa9a2a4c5c4fa3b5 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Tue, 19 Apr 2022 23:17:52 -0700 Subject: refactor(modules): move modules to module --- src/macros.rs | 15 ++-- src/main.rs | 2 +- src/modules.rs | 186 ---------------------------------------------- src/modules/mod.rs | 19 +++++ src/modules/multi_blog.rs | 186 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 216 insertions(+), 192 deletions(-) delete mode 100644 src/modules.rs create mode 100644 src/modules/mod.rs create mode 100644 src/modules/multi_blog.rs (limited to 'src') diff --git a/src/macros.rs b/src/macros.rs index bfaf9c2..7dd6048 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -16,6 +16,12 @@ // Copyright (C) 2022-2022 Fuwn // SPDX-License-Identifier: GPL-3.0-only +use std::lazy::SyncLazy; + +pub static QUOTES: SyncLazy> = SyncLazy::new(|| { + serde_json::from_str(include_str!("../content/json/quotes.json")).unwrap() +}); + #[macro_export] macro_rules! success { ($body:expr, $context:ident) => {{ @@ -28,11 +34,10 @@ macro_rules! success { quote: { use rand::seq::SliceRandom; - let quotes: Vec = - serde_json::from_str(include_str!("../content/json/quotes.json")) - .unwrap(); - - "es.choose(&mut rand::thread_rng()).unwrap().to_string() + &$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"), diff --git a/src/main.rs b/src/main.rs index 044002e..bb17767 100644 --- a/src/main.rs +++ b/src/main.rs @@ -303,7 +303,7 @@ async fn main() -> Result<(), Box> { ); time_mount = Instant::now(); - router.attach_stateless(modules::multi_blog); + router.attach_stateless(modules::multi_blog::multi_blog); info!( "blog mounts took {}ms", diff --git a/src/modules.rs b/src/modules.rs deleted file mode 100644 index 4831493..0000000 --- a/src/modules.rs +++ /dev/null @@ -1,186 +0,0 @@ -// This file is part of Locus . -// Copyright (C) 2022-2022 Fuwn -// -// 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 . -// -// Copyright (C) 2022-2022 Fuwn -// SPDX-License-Identifier: GPL-3.0-only - -use std::{ - collections::HashMap, - fs::{self, read_dir}, - io::Read, -}; - -use windmark::Response; - -use crate::{success, track_mount, Main}; - -#[allow(clippy::too_many_lines)] -pub fn multi_blog(router: &mut windmark::Router) { - let paths = read_dir("content/pages/blog").unwrap(); - let mut blogs: HashMap> = HashMap::new(); - - for path in paths { - let blog = path.unwrap().path().display().to_string(); - let blog_paths = read_dir(&blog).unwrap(); - let mut entries: HashMap<_, String> = HashMap::new(); - - blog_paths - .map(|e| e.unwrap().path().display().to_string()) - .for_each(|file| { - let mut contents = String::new(); - - fs::File::open(&file) - .unwrap() - .read_to_string(&mut contents) - .unwrap(); - - entries.insert( - file.replace(&blog, "").replace(".gmi", "").replace( - { - #[cfg(windows)] - { - '\\' - } - - #[cfg(unix)] - { - '/' - } - }, - "", - ), - contents, - ); - }); - - blogs.insert( - blog - .replace( - { - #[cfg(windows)] - { - "content/pages/blog\\" - } - - #[cfg(unix)] - { - "content/pages/blog/" - } - }, - "", - ) - .split('_') - .map(|s| { - // https://gist.github.com/jpastuszek/2704f3c5a3864b05c48ee688d0fd21d7 - let mut c = s.chars(); - - match c.next() { - None => String::new(), - Some(f) => - f.to_uppercase() - .chain(c.flat_map(char::to_lowercase)) - .collect(), - } - }) - .collect::>() - .join(" "), - entries, - ); - } - - let blog_clone = blogs.clone(); - - track_mount( - router, - "/blog", - "Fuwn's blogs", - Box::new(move |context| { - success!( - &format!( - "# BLOGS ({})\n\n{}", - blog_clone.len(), - blog_clone - .iter() - .map(|(title, _)| title.clone()) - .collect::>() - .into_iter() - .map(|i| { - format!( - "=> {} {}", - format_args!("/blog/{}", i.replace(' ', "_").to_lowercase(),), - i - ) - }) - .collect::>() - .join("\n") - ), - context - ) - }), - ); - - for (blog, entries) in blogs { - let fixed_blog_name = blog.replace(' ', "_").to_lowercase(); - let entries_clone = entries.clone(); - let fixed_blog_name_clone = fixed_blog_name.clone(); - let blog_clone = blog.clone(); - - track_mount( - router, - &format!("/blog/{}", fixed_blog_name), - &format!("{} ― One of Fuwn's blogs", &blog), - Box::new(move |context| { - success!( - &format!( - "# {} ({})\n\n{}", - blog.to_uppercase(), - entries_clone.len(), - entries_clone - .iter() - .map(|(title, _)| title.clone()) - .collect::>() - .into_iter() - .map(|i| { - format!( - "=> {} {}", - format_args!( - "/blog/{}/{}", - fixed_blog_name_clone, - i.to_lowercase() - ), - i - ) - }) - .collect::>() - .join("\n") - ), - context - ) - }), - ); - - for (title, contents) in entries { - track_mount( - router, - &format!("/blog/{}/{}", fixed_blog_name, title.to_lowercase()), - &format!( - "{}, {} ― An entry to one of Fuwn's blogs", - blog_clone, title - ), - Box::new(move |context| success!(contents, context)), - ); - } - } -} diff --git a/src/modules/mod.rs b/src/modules/mod.rs new file mode 100644 index 0000000..bab3cb1 --- /dev/null +++ b/src/modules/mod.rs @@ -0,0 +1,19 @@ +// This file is part of Locus . +// Copyright (C) 2022-2022 Fuwn +// +// 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 . +// +// Copyright (C) 2022-2022 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +pub mod multi_blog; diff --git a/src/modules/multi_blog.rs b/src/modules/multi_blog.rs new file mode 100644 index 0000000..4831493 --- /dev/null +++ b/src/modules/multi_blog.rs @@ -0,0 +1,186 @@ +// This file is part of Locus . +// Copyright (C) 2022-2022 Fuwn +// +// 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 . +// +// Copyright (C) 2022-2022 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +use std::{ + collections::HashMap, + fs::{self, read_dir}, + io::Read, +}; + +use windmark::Response; + +use crate::{success, track_mount, Main}; + +#[allow(clippy::too_many_lines)] +pub fn multi_blog(router: &mut windmark::Router) { + let paths = read_dir("content/pages/blog").unwrap(); + let mut blogs: HashMap> = HashMap::new(); + + for path in paths { + let blog = path.unwrap().path().display().to_string(); + let blog_paths = read_dir(&blog).unwrap(); + let mut entries: HashMap<_, String> = HashMap::new(); + + blog_paths + .map(|e| e.unwrap().path().display().to_string()) + .for_each(|file| { + let mut contents = String::new(); + + fs::File::open(&file) + .unwrap() + .read_to_string(&mut contents) + .unwrap(); + + entries.insert( + file.replace(&blog, "").replace(".gmi", "").replace( + { + #[cfg(windows)] + { + '\\' + } + + #[cfg(unix)] + { + '/' + } + }, + "", + ), + contents, + ); + }); + + blogs.insert( + blog + .replace( + { + #[cfg(windows)] + { + "content/pages/blog\\" + } + + #[cfg(unix)] + { + "content/pages/blog/" + } + }, + "", + ) + .split('_') + .map(|s| { + // https://gist.github.com/jpastuszek/2704f3c5a3864b05c48ee688d0fd21d7 + let mut c = s.chars(); + + match c.next() { + None => String::new(), + Some(f) => + f.to_uppercase() + .chain(c.flat_map(char::to_lowercase)) + .collect(), + } + }) + .collect::>() + .join(" "), + entries, + ); + } + + let blog_clone = blogs.clone(); + + track_mount( + router, + "/blog", + "Fuwn's blogs", + Box::new(move |context| { + success!( + &format!( + "# BLOGS ({})\n\n{}", + blog_clone.len(), + blog_clone + .iter() + .map(|(title, _)| title.clone()) + .collect::>() + .into_iter() + .map(|i| { + format!( + "=> {} {}", + format_args!("/blog/{}", i.replace(' ', "_").to_lowercase(),), + i + ) + }) + .collect::>() + .join("\n") + ), + context + ) + }), + ); + + for (blog, entries) in blogs { + let fixed_blog_name = blog.replace(' ', "_").to_lowercase(); + let entries_clone = entries.clone(); + let fixed_blog_name_clone = fixed_blog_name.clone(); + let blog_clone = blog.clone(); + + track_mount( + router, + &format!("/blog/{}", fixed_blog_name), + &format!("{} ― One of Fuwn's blogs", &blog), + Box::new(move |context| { + success!( + &format!( + "# {} ({})\n\n{}", + blog.to_uppercase(), + entries_clone.len(), + entries_clone + .iter() + .map(|(title, _)| title.clone()) + .collect::>() + .into_iter() + .map(|i| { + format!( + "=> {} {}", + format_args!( + "/blog/{}/{}", + fixed_blog_name_clone, + i.to_lowercase() + ), + i + ) + }) + .collect::>() + .join("\n") + ), + context + ) + }), + ); + + for (title, contents) in entries { + track_mount( + router, + &format!("/blog/{}/{}", fixed_blog_name, title.to_lowercase()), + &format!( + "{}, {} ― An entry to one of Fuwn's blogs", + blog_clone, title + ), + Box::new(move |context| success!(contents, context)), + ); + } + } +} -- cgit v1.2.3