Skip to content

Commit 5a2d9ec

Browse files
authored
Merge pull request #1229 from epage/refactor
refactor: Basic clean up
2 parents 18366c0 + 890644a commit 5a2d9ec

File tree

23 files changed

+416
-429
lines changed

23 files changed

+416
-429
lines changed

Cargo.lock

+1-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
members = [
33
"crates/*",
44
]
5-
resolver = "2"
5+
resolver = "3"
66

77
[workspace.package]
88
repository = "https://github.com/cobalt-org/cobalt.rs"
99
license = "MIT OR Apache-2.0"
10-
edition = "2021"
11-
rust-version = "1.76" # MSRV
10+
edition = "2024"
11+
rust-version = "1.85" # MSRV
1212
include = [
1313
"build.rs",
1414
"src/**/*",
@@ -159,7 +159,6 @@ jsonfeed = "0.2"
159159
pulldown-cmark = {version="0.12", default-features = false, features = ["html"] }
160160
engarde = { version = "0.1", path = "crates/engarde" }
161161
regex = "1.11"
162-
lazy_static = "1.5"
163162
itertools = "0.14"
164163
ignore = "0.4"
165164
serde = "1.0"

crates/config/src/document.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ fn deprecated_split_front_matter(content: &str) -> (Option<&str>, &str) {
119119
.unwrap()
120120
});
121121
if FRONT_MATTER_DIVIDE.is_match(content) {
122-
log::warn!("Trailing separators are deprecated. We recommend frontmatters be surrounded, above and below, with ---");
122+
log::warn!(
123+
"Trailing separators are deprecated. We recommend frontmatters be surrounded, above and below, with ---"
124+
);
123125

124126
let mut splits = FRONT_MATTER_DIVIDE.splitn(content, 2);
125127

crates/core/src/source.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,15 @@ mod tests {
9898
use super::*;
9999

100100
macro_rules! assert_includes_dir {
101-
($root:expr, $ignores:expr, $test:expr, $included:expr) => {
101+
($root:expr_2021, $ignores:expr_2021, $test:expr_2021, $included:expr_2021) => {
102102
let root = $root;
103103
let ignores = $ignores.clone();
104104
let files = Source::new(std::path::Path::new(root), ignores).unwrap();
105105
assert_eq!(files.includes_dir(std::path::Path::new($test)), $included);
106106
};
107107
}
108108
macro_rules! assert_includes_file {
109-
($root:expr, $ignores:expr, $test:expr, $included:expr) => {
109+
($root:expr_2021, $ignores:expr_2021, $test:expr_2021, $included:expr_2021) => {
110110
let root = $root;
111111
let ignores = $ignores.clone();
112112
let files = Source::new(std::path::Path::new(root), ignores).unwrap();

crates/engarde/src/syntax.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::path::Path;
22
use syntect::easy::HighlightLines;
33
use syntect::highlighting::ThemeSet;
44
use syntect::html::{
5-
append_highlighted_html_for_styled_line, start_highlighted_html_snippet, IncludeBackground,
5+
IncludeBackground, append_highlighted_html_for_styled_line, start_highlighted_html_snippet,
66
};
77
use syntect::parsing::{SyntaxReference, SyntaxSet};
88
use syntect::util::LinesWithEndings;
@@ -116,8 +116,7 @@ mod test {
116116
}
117117
";
118118

119-
const CODEBLOCK_RENDERED: &str =
120-
"<pre style=\"background-color:#2b303b;\">\n\
119+
const CODEBLOCK_RENDERED: &str = "<pre style=\"background-color:#2b303b;\">\n\
121120
<code><span style=\"color:#b48ead;\">mod </span>\
122121
<span style=\"color:#c0c5ce;\">test {\n\
123122
</span><span style=\"color:#c0c5ce;\"> </span>\

crates/file-serve/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ impl Server {
119119
*server = Some(tiny_http::Server::http(self.addr()).map_err(Error::new)?);
120120
}
121121
Ok(Some(_)) | Err(TryLockError::WouldBlock) => {
122-
return Err(Error::new("the server is running"))
122+
return Err(Error::new("the server is running"));
123123
}
124124
Err(error @ TryLockError::Poisoned(_)) => return Err(Error::new(error)),
125125
}

src/bin/cobalt/build.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ impl BuildArgs {
3030
let config = cobalt::cobalt_model::Config::from_config(config)?;
3131

3232
build(config)?;
33-
info!("Build successful");
33+
log::info!("Build successful");
3434

3535
Ok(())
3636
}
3737
}
3838

3939
pub(crate) fn build(config: cobalt::Config) -> Result<()> {
40-
info!(
40+
log::info!(
4141
"Building from `{}` into `{}`",
4242
config.source.display(),
4343
config.destination.display()
@@ -69,8 +69,8 @@ pub(crate) fn clean(config: &cobalt::Config) -> Result<()> {
6969
let destdir = match destdir {
7070
Ok(destdir) => destdir,
7171
Err(e) => {
72-
debug!("No `{}` to clean", config.destination.display());
73-
debug!("{}", e);
72+
log::debug!("No `{}` to clean", config.destination.display());
73+
log::debug!("{}", e);
7474
return Ok(());
7575
}
7676
};
@@ -84,7 +84,7 @@ pub(crate) fn clean(config: &cobalt::Config) -> Result<()> {
8484

8585
fs::remove_dir_all(&destdir)?;
8686

87-
info!("directory `{}` removed", destdir.display());
87+
log::info!("directory `{}` removed", destdir.display());
8888

8989
Ok(())
9090
}

src/bin/cobalt/main.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
#![warn(warnings)]
22

3-
#[macro_use]
4-
extern crate lazy_static;
5-
6-
#[macro_use]
7-
extern crate log;
8-
93
mod args;
104
mod build;
115
mod debug;
@@ -59,7 +53,7 @@ impl Cli {
5953
anstream::AutoStream::choice(&std::io::stderr()),
6054
anstream::ColorChoice::Never
6155
);
62-
args::init_logging(self.logging.clone(), colored_stderr);
56+
args::init_logging(self.logging, colored_stderr);
6357

6458
match &self.command {
6559
Command::Init(cmd) => cmd.run(),

src/bin/cobalt/new.rs

+16-13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::collections;
22
use std::fs;
33
use std::io::Write;
44
use std::path;
5+
use std::sync::LazyLock;
56

67
use anyhow::Context as _;
78
use cobalt::cobalt_model;
@@ -21,7 +22,7 @@ impl InitArgs {
2122
pub(crate) fn run(&self) -> Result<()> {
2223
create_new_project(&self.directory)
2324
.with_context(|| anyhow::format_err!("Could not create a new cobalt project"))?;
24-
info!("Created new project at {}", self.directory.display());
25+
log::info!("Created new project at {}", self.directory.display());
2526

2627
Ok(())
2728
}
@@ -187,13 +188,12 @@ layout: default.liquid
187188
{% endfor %}
188189
";
189190

190-
lazy_static! {
191-
static ref DEFAULT: collections::HashMap<&'static str, &'static str> =
192-
[("pages", INDEX_MD), ("posts", POST_MD)]
193-
.iter()
194-
.cloned()
195-
.collect();
196-
}
191+
static DEFAULT: LazyLock<collections::HashMap<&'static str, &'static str>> = LazyLock::new(|| {
192+
[("pages", INDEX_MD), ("posts", POST_MD)]
193+
.iter()
194+
.cloned()
195+
.collect()
196+
});
197197

198198
pub(crate) fn create_new_project<P: AsRef<path::Path>>(dest: P) -> Result<()> {
199199
create_new_project_for_path(dest.as_ref())
@@ -276,7 +276,7 @@ pub(crate) fn create_new_document(
276276
cobalt_model::files::read_file(&source_path)
277277
.with_context(|| anyhow::format_err!("Failed to read default: {:?}", source_path))?
278278
} else {
279-
debug!(
279+
log::debug!(
280280
"No custom default provided ({:?}), falling back to built-in",
281281
source_path
282282
);
@@ -323,7 +323,7 @@ pub(crate) fn create_new_document(
323323
fs::create_dir_all(parent)?;
324324
}
325325
create_file(&file.abs_path, &doc)?;
326-
info!("Created new {} {}", collection_slug, file.rel_path);
326+
log::info!("Created new {} {}", collection_slug, file.rel_path);
327327

328328
Ok(())
329329
}
@@ -333,7 +333,7 @@ fn create_file<P: AsRef<path::Path>>(path: P, content: &str) -> Result<()> {
333333
}
334334

335335
fn create_file_for_path(path: &path::Path, content: &str) -> Result<()> {
336-
trace!("Creating file {:?}", path);
336+
log::trace!("Creating file {:?}", path);
337337

338338
let mut file = fs::OpenOptions::new()
339339
.write(true)
@@ -405,7 +405,7 @@ pub(crate) fn rename_document(
405405
cobalt_model::files::write_document_file(doc, &target.abs_path)?;
406406

407407
if !full_front.is_draft {
408-
warn!("Renaming a published page might invalidate links");
408+
log::warn!("Renaming a published page might invalidate links");
409409
}
410410
fs::remove_file(source)?;
411411

@@ -437,7 +437,10 @@ fn prepend_date_to_filename(
437437
.first()
438438
.expect("at least one element is enforced by config validator"))
439439
);
440-
trace!("`publish_date_in_filename` setting is activated, prefix filename with date, new filename: {}", file_name);
440+
log::trace!(
441+
"`publish_date_in_filename` setting is activated, prefix filename with date, new filename: {}",
442+
file_name
443+
);
441444
fs::rename(file, file.with_file_name(file_name))?;
442445
Ok(())
443446
}

src/bin/cobalt/serve.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ impl ServeArgs {
4646
let server = server.build();
4747

4848
let mut config = self.config.load_config()?;
49-
debug!("Overriding config `site.base_url` with `/`");
49+
log::debug!("Overriding config `site.base_url` with `/`");
5050
let host = format!("http://{}/", server.addr());
5151
config.site.base_url = Some(host.into());
5252
let mut config = cobalt_model::Config::from_config(config)?;
53-
debug!(
53+
log::debug!(
5454
"Overriding config `destination` with `{}`",
5555
dest.path().display()
5656
);
@@ -67,11 +67,11 @@ impl ServeArgs {
6767

6868
dest.close()?;
6969
} else {
70-
info!("Watching {} for changes", &config.source.display());
70+
log::info!("Watching {} for changes", &config.source.display());
7171
thread::spawn(move || {
7272
let e = serve(&server);
7373
if let Some(e) = e.err() {
74-
error!("{}", e);
74+
log::error!("{}", e);
7575
}
7676
process::exit(1)
7777
});
@@ -84,19 +84,19 @@ impl ServeArgs {
8484
}
8585

8686
fn serve(server: &file_serve::Server) -> Result<()> {
87-
info!(
87+
log::info!(
8888
"Serving {} through static file server",
8989
server.source().display()
9090
);
91-
info!("Server Listening on http://{}", server.addr());
92-
info!("Ctrl-c to stop the server");
91+
log::info!("Server Listening on http://{}", server.addr());
92+
log::info!("Ctrl-c to stop the server");
9393

9494
Ok(server.serve()?)
9595
}
9696

9797
fn open_browser(url: String) -> Result<()> {
9898
match open::that(url) {
99-
Ok(()) => info!("Please check your browser!"),
99+
Ok(()) => log::info!("Please check your browser!"),
100100
Err(why) => eprintln!("Failure to execute command: {why}"),
101101
}
102102
Ok(())
@@ -125,7 +125,7 @@ fn watch(config: &cobalt_model::Config) -> Result<()> {
125125
watcher
126126
.watch(&source, notify::RecursiveMode::Recursive)
127127
.with_context(|| anyhow::format_err!("Notify error"))?;
128-
info!("Watching {} for changes", config.source.display());
128+
log::info!("Watching {} for changes", config.source.display());
129129

130130
for event in rx {
131131
let event = event.with_context(|| anyhow::format_err!("Notify error"))?;
@@ -145,17 +145,17 @@ fn watch(config: &cobalt_model::Config) -> Result<()> {
145145
// ensure we don't miss anything (normal file walks will miss
146146
// `_layouts`, etc).
147147
if event_path.starts_with(&destination) {
148-
trace!("Ignored file changed {:?}", event);
148+
log::trace!("Ignored file changed {:?}", event);
149149
false
150150
} else {
151-
debug!("Page changed {:?}", event);
151+
log::debug!("Page changed {:?}", event);
152152
true
153153
}
154154
});
155155
if rebuild {
156156
let result = build::build(config.clone());
157157
if let Err(fail) = result {
158-
error!("build failed\n{:?}", fail);
158+
log::error!("build failed\n{:?}", fail);
159159
}
160160
}
161161
}

src/cobalt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ use log::warn;
1111
use sitemap::writer::SiteMapWriter;
1212

1313
use crate::cobalt_model;
14+
use crate::cobalt_model::Collection;
1415
use crate::cobalt_model::files;
1516
use crate::cobalt_model::permalink;
16-
use crate::cobalt_model::Collection;
1717
use crate::cobalt_model::{Config, Minify, SortOrder};
1818
use crate::document::{Document, RenderContext};
1919
use crate::error::Result;

src/cobalt_model/assets.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use log::debug;
66
use serde::{Deserialize, Serialize};
77

88
use super::sass;
9-
use super::{files, Minify};
9+
use super::{Minify, files};
1010

1111
use crate::error::Result;
1212

src/cobalt_model/files.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use std::path;
55

66
use crate::error::Result;
77
use anyhow::Context as _;
8-
use ignore::gitignore::{Gitignore, GitignoreBuilder};
98
use ignore::Match;
9+
use ignore::gitignore::{Gitignore, GitignoreBuilder};
1010
use log::debug;
1111
use log::trace;
1212
use normalize_line_endings::normalized;
@@ -318,7 +318,7 @@ mod tests {
318318
use super::*;
319319

320320
macro_rules! assert_includes_dir {
321-
($root:expr, $ignores:expr, $test:expr, $included:expr) => {
321+
($root:expr_2021, $ignores:expr_2021, $test:expr_2021, $included:expr_2021) => {
322322
let mut files = FilesBuilder::new(path::Path::new($root)).unwrap();
323323
let ignores: &[&str] = $ignores;
324324
for ignore in ignores {
@@ -329,7 +329,7 @@ mod tests {
329329
};
330330
}
331331
macro_rules! assert_includes_file {
332-
($root:expr, $ignores:expr, $test:expr, $included:expr) => {
332+
($root:expr_2021, $ignores:expr_2021, $test:expr_2021, $included:expr_2021) => {
333333
let mut files = FilesBuilder::new(path::Path::new($root)).unwrap();
334334
let ignores: &[&str] = $ignores;
335335
for ignore in ignores {

0 commit comments

Comments
 (0)