diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index c674337..5dd8c89 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -40,7 +40,9 @@ jobs: uses: Swatinem/rust-cache@v2 - name: Clippy - run: cargo clippy -- -W clippy::pedantic + # Relaxed on clippy requirement, will run it manually from time to time. + # run: cargo clippy -- -W clippy::pedantic + run: cargo clippy - name: Build 🔧 run: cargo build --release diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 65890aa..86facb3 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -9,6 +9,7 @@ Thank you for considering contributing to the Marmite Site Generator project! Co 3. [How to Contribute](#how-to-contribute) 4. [Pull Requests](#pull-requests) 5. [Commit Messages](#commit-messages) +6. [Code Quality](#code-quality) ## Code of Conduct @@ -79,3 +80,37 @@ Your commit messages should be descriptive and concise. Use the following format fix: Corrected YAML parsing error when loading the configuration feat: Added support for multiple markdown templates ``` + +### Code Quality + +Before pushing your changes ensure it meets the minimal code quality. + +1. Format the code **Required** + +```bash +cargo fmt +``` + +2. Apply clippy fixes **optional** + +``` +cargo clippy +``` +or +``` +cargo clippy -- -W clippy:pedantic +``` + +> **hint**: you can add `--fix` for clippy to try to apply fixes. + +#### Just + +There is a `justfile` in the root of repo, you can use it for checkings. + +```bash +cargo install just +just check + +# Ensure your changes are committed before running. +just fix +``` diff --git a/justfile b/justfile index d71490b..2346010 100644 --- a/justfile +++ b/justfile @@ -1,4 +1,8 @@ check: + cargo fmt -- --check + cargo clippy + +pedantic: cargo fmt -- --check cargo clippy -- -W clippy::pedantic diff --git a/src/server.rs b/src/server.rs index 184b549..a8ea26b 100644 --- a/src/server.rs +++ b/src/server.rs @@ -76,7 +76,7 @@ fn handle_request( } fn render_not_found(error_path: &PathBuf) -> Result>>, String> { - match File::open(&error_path) { + match File::open(error_path) { Ok(mut file) => { let mut buffer = Vec::new(); std::io::copy(&mut file, &mut buffer).map_err(|e| e.to_string())?; diff --git a/src/site.rs b/src/site.rs index c2063e0..6f23f01 100644 --- a/src/site.rs +++ b/src/site.rs @@ -100,27 +100,7 @@ fn render_templates(site_data: &Data, tera: &Tera, output_dir: &Path) -> Result< } // Check and guarantees that page 404 was generated even if 404.md is removed - let file_404_path = output_dir.join("404.html"); - if !file_404_path.exists() { - let mut content_context = global_context.clone(); - let page_404_content = Content { - html: String::from("Page not found :/"), - title: String::from("Page not found"), - date: None, - slug: String::from(""), - extra: None, - tags: vec![], - }; - content_context.insert("title", &page_404_content.title); - content_context.insert("content", &page_404_content); - render_html( - "content.html", - "404.html", - tera, - &content_context, - output_dir, - )?; - } + handle_404(&global_context, tera, output_dir)?; // Render tagged_contents let mut unique_tags: Vec<(String, usize)> = Vec::new(); @@ -175,6 +155,25 @@ fn render_templates(site_data: &Data, tera: &Tera, output_dir: &Path) -> Result< Ok(()) } +fn handle_404(global_context: &Context, tera: &Tera, output_dir: &Path) -> Result<(), String> { + let file_404_path = output_dir.join("404.html"); + if !file_404_path.exists() { + let mut context = global_context.clone(); + let page_404_content = Content { + html: String::from("Page not found :/"), + title: String::from("Page not found"), + date: None, + slug: String::new(), + extra: None, + tags: vec![], + }; + context.insert("title", &page_404_content.title); + context.insert("content", &page_404_content); + render_html("content.html", "404.html", tera, &context, output_dir)?; + }; + Ok(()) +} + fn render_html( template: &str, filename: &str, @@ -388,7 +387,7 @@ fn collect_content(content_dir: &std::path::PathBuf, site_data: &mut Data) { } }); - NAME_BASED_SLUG_FILES.into_iter().for_each(|slugged_file| { + for slugged_file in NAME_BASED_SLUG_FILES { let slugged_path = content_dir.join(slugged_file); if slugged_path.exists() { if let Err(e) = process_file(slugged_path.as_path(), site_data, true) { @@ -399,5 +398,5 @@ fn collect_content(content_dir: &std::path::PathBuf, site_data: &mut Data) { ); } } - }) + } }