Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some minor code cleanup #1407

Merged
merged 6 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/blogs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ static POSTS_EXT: &str = "md";

#[derive(Deserialize)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub(crate) struct Manifest {
pub struct Manifest {
/// Title to display in the "top row".
pub(crate) title: String,

Expand All @@ -32,7 +32,7 @@ pub(crate) struct Manifest {
}

#[derive(Serialize)]
pub(crate) struct Blog {
pub struct Blog {
title: String,
index_title: String,
link_text: String,
Expand Down Expand Up @@ -82,7 +82,7 @@ impl Blog {
}
}

Ok(Blog {
Ok(Self {
title: manifest.title,
index_title: manifest.index_title,
description: manifest.description,
Expand Down Expand Up @@ -121,7 +121,7 @@ impl Blog {

/// Recursively load blogs in a directory. A blog is a directory with a `blog.yml`
/// file inside it.
pub(crate) fn load(base: &Path) -> eyre::Result<Vec<Blog>> {
pub fn load(base: &Path) -> eyre::Result<Vec<Blog>> {
let mut blogs = Vec::new();
load_recursive(base, base, &mut blogs)?;
Ok(blogs)
Expand All @@ -140,8 +140,7 @@ fn load_recursive(base: &Path, current: &Path, blogs: &mut Vec<Blog>) -> eyre::R
if file_name == MANIFEST_FILE {
let prefix = parent
.strip_prefix(base)
.map(|p| p.to_path_buf())
.unwrap_or_else(|_| PathBuf::new());
.map_or_else(|_| PathBuf::new(), Path::to_path_buf);
blogs.push(Blog::load(prefix, parent)?);
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use self::blogs::Blog;
use self::posts::Post;
use chrono::Timelike;
use eyre::{eyre, WrapErr};
use handlebars::{handlebars_helper, Handlebars};
use handlebars::{handlebars_helper, DirectorySourceOptions, Handlebars};
use rayon::prelude::*;
use sass_rs::{compile_file, Options};
use serde_derive::Serialize;
Expand Down Expand Up @@ -54,7 +54,7 @@ impl<'a> Generator<'a> {
) -> eyre::Result<Self> {
let mut handlebars = Handlebars::new();
handlebars.set_strict_mode(true);
handlebars.register_templates_directory("templates", Default::default())?;
handlebars.register_templates_directory("templates", DirectorySourceOptions::default())?;
handlebars.register_helper("month_name", Box::new(hb_month_name_helper));

Ok(Generator {
Expand Down Expand Up @@ -96,8 +96,8 @@ impl<'a> Generator<'a> {
}

fn compile_sass(&self, filename: &str) -> eyre::Result<()> {
let scss_file = format!("./src/styles/{}.scss", filename);
let css_file = format!("./static/styles/{}.css", filename);
let scss_file = format!("./src/styles/{filename}.scss");
let css_file = format!("./static/styles/{filename}.css");

let css = compile_file(&scss_file, Options::default())
.map_err(|error| eyre!(error))
Expand All @@ -113,7 +113,7 @@ impl<'a> Generator<'a> {
fn concat_vendor_css(&self, files: Vec<&str>) -> eyre::Result<()> {
let mut concatted = String::new();
for filestem in files {
let vendor_path = format!("./static/styles/{}.css", filestem);
let vendor_path = format!("./static/styles/{filestem}.css");
let contents = fs::read_to_string(vendor_path).wrap_err("couldn't read vendor css")?;
concatted.push_str(&contents);
}
Expand Down
35 changes: 15 additions & 20 deletions src/posts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct YamlHeader {
}

#[derive(Debug, Clone, Serialize)]
pub(crate) struct Post {
pub struct Post {
pub(crate) filename: String,
pub(crate) layout: String,
pub(crate) title: String,
Expand Down Expand Up @@ -114,26 +114,21 @@ impl Post {
}

// If they supplied team, it should look like `team-text <team-url>`
let (team, team_url) = match team_string {
Some(s) => {
lazy_static::lazy_static! {
static ref R: Regex = Regex::new(r"(?P<name>[^<]*) <(?P<url>[^>]+)>").unwrap();
}
let captures = match R.captures(&s) {
Some(c) => c,
None => panic!(
"team from path `{}` should have format `$name <$url>`",
path.display()
),
};
(
Some(captures["name"].to_string()),
Some(captures["url"].to_string()),
)
let (team, team_url) = team_string.map_or((None, None), |s| {
lazy_static::lazy_static! {
static ref R: Regex = Regex::new(r"(?P<name>[^<]*) <(?P<url>[^>]+)>").unwrap();
}

None => (None, None),
};
let Some(captures) = R.captures(&s) else {
panic!(
"team from path `{}` should have format `$name <$url>`",
path.display()
)
};
(
Some(captures["name"].to_string()),
Some(captures["url"].to_string()),
)
});

Ok(Self {
filename,
Expand Down