refactor: Simplify Site::read
This commit is contained in:
parent
96df281a20
commit
200bd42687
95
src/site.rs
95
src/site.rs
|
@ -1,8 +1,9 @@
|
|||
//! Management of on-disk layout of the source of a site
|
||||
|
||||
use std::{
|
||||
borrow::ToOwned,
|
||||
collections::{BTreeMap, BTreeSet},
|
||||
fs::{create_dir_all, File},
|
||||
fs::{create_dir_all, read_dir, File},
|
||||
io::Write,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
@ -59,7 +60,6 @@ impl Site {
|
|||
///
|
||||
/// Will return an error if serialization fails, the user does not have write permissions for
|
||||
/// the directory, or any other IO errors occur while writing out the config.
|
||||
#[allow(clippy::too_many_lines)]
|
||||
pub fn write(&mut self, site_dir: impl AsRef<Path>) -> Result<(), SiteError> {
|
||||
let site_dir = site_dir.as_ref();
|
||||
// Setup logging
|
||||
|
@ -182,7 +182,6 @@ impl Site {
|
|||
///
|
||||
/// Will return an error if the state of the given site is invalid, or of any other IO errors
|
||||
/// occur while reading in the state
|
||||
#[allow(clippy::too_many_lines)]
|
||||
pub fn read(site_dir: impl AsRef<Path>) -> Result<Site, SiteError> {
|
||||
let site_dir = site_dir.as_ref();
|
||||
// Setup the logging
|
||||
|
@ -201,68 +200,57 @@ impl Site {
|
|||
// Read in the statics
|
||||
let mut statics = BTreeSet::new();
|
||||
let statics_dir = site_dir.join("statics");
|
||||
let statics_iter = WalkDir::new(&statics_dir)
|
||||
WalkDir::new(&statics_dir)
|
||||
.into_iter()
|
||||
.filter_map(std::result::Result::ok)
|
||||
.filter(|e| e.path().is_file());
|
||||
for static_path in statics_iter {
|
||||
statics.insert(
|
||||
static_path
|
||||
.path()
|
||||
.filter(|e| e.path().is_file())
|
||||
.filter_map(|e| {
|
||||
e.path()
|
||||
.strip_prefix(&statics_dir)
|
||||
.expect("Invalid static?")
|
||||
.to_owned(),
|
||||
);
|
||||
}
|
||||
.map(ToOwned::to_owned)
|
||||
.ok()
|
||||
})
|
||||
.for_each(|e| {
|
||||
statics.insert(e);
|
||||
});
|
||||
|
||||
// Read in the styles
|
||||
let mut styles = BTreeSet::new();
|
||||
let styles_dir = site_dir.join("styles");
|
||||
let styles_iter = WalkDir::new(&styles_dir)
|
||||
WalkDir::new(&styles_dir)
|
||||
.into_iter()
|
||||
.filter_map(std::result::Result::ok)
|
||||
.filter(|e| e.path().is_file());
|
||||
for style_path in styles_iter {
|
||||
styles.insert(
|
||||
style_path
|
||||
.path()
|
||||
.filter(|e| e.path().is_file())
|
||||
.filter_map(|e| {
|
||||
e.path()
|
||||
.strip_prefix(&styles_dir)
|
||||
.expect("Invalid style?")
|
||||
.to_owned(),
|
||||
);
|
||||
}
|
||||
.map(ToOwned::to_owned)
|
||||
.ok()
|
||||
})
|
||||
.for_each(|e| {
|
||||
styles.insert(e);
|
||||
});
|
||||
|
||||
// Read in the pages
|
||||
let mut pages = BTreeMap::new();
|
||||
let pages_dir = site_dir.join("pages");
|
||||
debug!(?pages_dir, "Reading in pages");
|
||||
for dir in
|
||||
std::fs::read_dir(&pages_dir).context(EnumerateDirectorySnafu { path: &pages_dir })?
|
||||
{
|
||||
let dir = dir.context(EnumerateDirectorySnafu { path: &pages_dir })?;
|
||||
let dir_path = dir.path();
|
||||
// Make sure it is a directory
|
||||
if dir
|
||||
.metadata()
|
||||
for dir in read_dir(&pages_dir).context(EnumerateDirectorySnafu { path: &pages_dir })? {
|
||||
let dir_path = dir
|
||||
.context(EnumerateDirectorySnafu { path: &pages_dir })?
|
||||
.is_dir()
|
||||
{
|
||||
.path();
|
||||
// Make sure it is a directory
|
||||
if dir_path.is_dir() {
|
||||
// Extract the final component
|
||||
if let Some(page_dir) = dir_path.file_name() {
|
||||
let page =
|
||||
Page::read(site_dir, page_dir).context(PageReadSnafu { page: page_dir })?;
|
||||
pages.insert(PathBuf::from(page_dir), page);
|
||||
} else {
|
||||
warn!(
|
||||
?dir_path,
|
||||
"Path in pages directory did not have a final component? ignorning"
|
||||
);
|
||||
warn!(?dir_path, "Invalid path in pages, ignoring");
|
||||
}
|
||||
} else {
|
||||
warn!(
|
||||
?dir_path,
|
||||
"Item in pages directory was not a directory, ignoring"
|
||||
);
|
||||
warn!(?dir_path, "Non-dir in pages, ignoring");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -270,33 +258,22 @@ impl Site {
|
|||
let mut posts = BTreeMap::new();
|
||||
let posts_dir = site_dir.join("posts");
|
||||
debug!(?posts_dir, "Reading in posts");
|
||||
for dir in
|
||||
std::fs::read_dir(&posts_dir).context(EnumerateDirectorySnafu { path: &posts_dir })?
|
||||
{
|
||||
let dir = dir.context(EnumerateDirectorySnafu { path: &posts_dir })?;
|
||||
let dir_path = dir.path();
|
||||
// Make sure it is a directory
|
||||
if dir
|
||||
.metadata()
|
||||
for dir in read_dir(&posts_dir).context(EnumerateDirectorySnafu { path: &posts_dir })? {
|
||||
let dir_path = dir
|
||||
.context(EnumerateDirectorySnafu { path: &posts_dir })?
|
||||
.is_dir()
|
||||
{
|
||||
.path();
|
||||
// Make sure it is a directory
|
||||
if dir_path.is_dir() {
|
||||
// Extract the final component
|
||||
if let Some(post_dir) = dir_path.file_name() {
|
||||
let post =
|
||||
Post::read(site_dir, post_dir).context(PostReadSnafu { post: post_dir })?;
|
||||
posts.insert(PathBuf::from(post_dir), post);
|
||||
} else {
|
||||
warn!(
|
||||
?dir_path,
|
||||
"Path in posts directory did not have a final component? ignorning"
|
||||
);
|
||||
warn!(?dir_path, "Invalid path in posts, ignoring");
|
||||
}
|
||||
} else {
|
||||
warn!(
|
||||
?dir_path,
|
||||
"Item in posts directory was not a directory, ignoring"
|
||||
);
|
||||
warn!(?dir_path, "Non-dir in posts, ignoring");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue