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