Skip to content

Commit

Permalink
Clippy magic
Browse files Browse the repository at this point in the history
  • Loading branch information
rochacbruno committed Oct 23, 2024
1 parent bcb0d9e commit 892a90c
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 25 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
```
4 changes: 4 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
check:
cargo fmt -- --check
cargo clippy

pedantic:
cargo fmt -- --check
cargo clippy -- -W clippy::pedantic

Expand Down
2 changes: 1 addition & 1 deletion src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ fn handle_request(
}

fn render_not_found(error_path: &PathBuf) -> Result<Response<Cursor<Vec<u8>>>, 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())?;
Expand Down
45 changes: 22 additions & 23 deletions src/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand All @@ -399,5 +398,5 @@ fn collect_content(content_dir: &std::path::PathBuf, site_data: &mut Data) {
);
}
}
})
}
}

0 comments on commit 892a90c

Please sign in to comment.