Skip to content

Commit f4a28de

Browse files
authored
Some minor code cleanup (#1407)
2 parents 60ee5da + e8fb404 commit f4a28de

File tree

3 files changed

+25
-31
lines changed

3 files changed

+25
-31
lines changed

src/blogs.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ static POSTS_EXT: &str = "md";
77

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

@@ -32,7 +32,7 @@ pub(crate) struct Manifest {
3232
}
3333

3434
#[derive(Serialize)]
35-
pub(crate) struct Blog {
35+
pub struct Blog {
3636
title: String,
3737
index_title: String,
3838
link_text: String,
@@ -82,7 +82,7 @@ impl Blog {
8282
}
8383
}
8484

85-
Ok(Blog {
85+
Ok(Self {
8686
title: manifest.title,
8787
index_title: manifest.index_title,
8888
description: manifest.description,
@@ -121,7 +121,7 @@ impl Blog {
121121

122122
/// Recursively load blogs in a directory. A blog is a directory with a `blog.yml`
123123
/// file inside it.
124-
pub(crate) fn load(base: &Path) -> eyre::Result<Vec<Blog>> {
124+
pub fn load(base: &Path) -> eyre::Result<Vec<Blog>> {
125125
let mut blogs = Vec::new();
126126
load_recursive(base, base, &mut blogs)?;
127127
Ok(blogs)
@@ -140,8 +140,7 @@ fn load_recursive(base: &Path, current: &Path, blogs: &mut Vec<Blog>) -> eyre::R
140140
if file_name == MANIFEST_FILE {
141141
let prefix = parent
142142
.strip_prefix(base)
143-
.map(|p| p.to_path_buf())
144-
.unwrap_or_else(|_| PathBuf::new());
143+
.map_or_else(|_| PathBuf::new(), Path::to_path_buf);
145144
blogs.push(Blog::load(prefix, parent)?);
146145
}
147146
}

src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use self::blogs::Blog;
55
use self::posts::Post;
66
use chrono::Timelike;
77
use eyre::{eyre, WrapErr};
8-
use handlebars::{handlebars_helper, Handlebars};
8+
use handlebars::{handlebars_helper, DirectorySourceOptions, Handlebars};
99
use rayon::prelude::*;
1010
use sass_rs::{compile_file, Options};
1111
use serde_derive::Serialize;
@@ -54,7 +54,7 @@ impl<'a> Generator<'a> {
5454
) -> eyre::Result<Self> {
5555
let mut handlebars = Handlebars::new();
5656
handlebars.set_strict_mode(true);
57-
handlebars.register_templates_directory("templates", Default::default())?;
57+
handlebars.register_templates_directory("templates", DirectorySourceOptions::default())?;
5858
handlebars.register_helper("month_name", Box::new(hb_month_name_helper));
5959

6060
Ok(Generator {
@@ -96,8 +96,8 @@ impl<'a> Generator<'a> {
9696
}
9797

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

102102
let css = compile_file(&scss_file, Options::default())
103103
.map_err(|error| eyre!(error))
@@ -113,7 +113,7 @@ impl<'a> Generator<'a> {
113113
fn concat_vendor_css(&self, files: Vec<&str>) -> eyre::Result<()> {
114114
let mut concatted = String::new();
115115
for filestem in files {
116-
let vendor_path = format!("./static/styles/{}.css", filestem);
116+
let vendor_path = format!("./static/styles/{filestem}.css");
117117
let contents = fs::read_to_string(vendor_path).wrap_err("couldn't read vendor css")?;
118118
concatted.push_str(&contents);
119119
}

src/posts.rs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ struct YamlHeader {
1515
}
1616

1717
#[derive(Debug, Clone, Serialize)]
18-
pub(crate) struct Post {
18+
pub struct Post {
1919
pub(crate) filename: String,
2020
pub(crate) layout: String,
2121
pub(crate) title: String,
@@ -114,26 +114,21 @@ impl Post {
114114
}
115115

116116
// If they supplied team, it should look like `team-text <team-url>`
117-
let (team, team_url) = match team_string {
118-
Some(s) => {
119-
lazy_static::lazy_static! {
120-
static ref R: Regex = Regex::new(r"(?P<name>[^<]*) <(?P<url>[^>]+)>").unwrap();
121-
}
122-
let captures = match R.captures(&s) {
123-
Some(c) => c,
124-
None => panic!(
125-
"team from path `{}` should have format `$name <$url>`",
126-
path.display()
127-
),
128-
};
129-
(
130-
Some(captures["name"].to_string()),
131-
Some(captures["url"].to_string()),
132-
)
117+
let (team, team_url) = team_string.map_or((None, None), |s| {
118+
lazy_static::lazy_static! {
119+
static ref R: Regex = Regex::new(r"(?P<name>[^<]*) <(?P<url>[^>]+)>").unwrap();
133120
}
134-
135-
None => (None, None),
136-
};
121+
let Some(captures) = R.captures(&s) else {
122+
panic!(
123+
"team from path `{}` should have format `$name <$url>`",
124+
path.display()
125+
)
126+
};
127+
(
128+
Some(captures["name"].to_string()),
129+
Some(captures["url"].to_string()),
130+
)
131+
});
137132

138133
Ok(Self {
139134
filename,

0 commit comments

Comments
 (0)