replace try operator with expect
This commit is contained in:
parent
6cfc9dd8be
commit
e0737aa8cf
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "nyf"
|
name = "nyf"
|
||||||
version = "0.3.0"
|
version = "0.3.1"
|
||||||
authors = ["glyph <glyph@mycelial.technology>"]
|
authors = ["glyph <glyph@mycelial.technology>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
description = "html splicer and rss generator"
|
description = "html splicer and rss generator"
|
||||||
|
|
29
src/main.rs
29
src/main.rs
|
@ -21,14 +21,14 @@
|
||||||
//! element are spliced into the `[[ title ]]` tag of the base HTML template for each
|
//! element are spliced into the `[[ title ]]` tag of the base HTML template for each
|
||||||
//! sub-directory template.
|
//! sub-directory template.
|
||||||
//!
|
//!
|
||||||
use std::{error, fs, io, path::Path};
|
use std::{fs, io, path::Path};
|
||||||
|
|
||||||
// define the path for the template directory
|
// define the path for the template directory
|
||||||
const TEMPLATE_DIR: &str = "./templates";
|
const TEMPLATE_DIR: &str = "./templates";
|
||||||
// define the path for the generated site output
|
// define the path for the generated site output
|
||||||
const SITE_DIR: &str = "./site";
|
const SITE_DIR: &str = "./site";
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn error::Error>> {
|
fn main() {
|
||||||
// read the base html template to a string
|
// read the base html template to a string
|
||||||
let base = format!("{}/base.html", TEMPLATE_DIR);
|
let base = format!("{}/base.html", TEMPLATE_DIR);
|
||||||
let base_path = Path::new(&base);
|
let base_path = Path::new(&base);
|
||||||
|
@ -57,25 +57,31 @@ fn main() -> Result<(), Box<dyn error::Error>> {
|
||||||
.expect("failed to write the root index.html file");
|
.expect("failed to write the root index.html file");
|
||||||
|
|
||||||
// walk the template directory and collect paths for all files and sub-directories
|
// walk the template directory and collect paths for all files and sub-directories
|
||||||
let template_files = fs::read_dir(TEMPLATE_DIR)?
|
let template_files: Vec<_> = fs::read_dir(TEMPLATE_DIR)
|
||||||
|
.expect("failed to read template directory")
|
||||||
.map(|res| res.map(|e| e.path()))
|
.map(|res| res.map(|e| e.path()))
|
||||||
.collect::<Result<Vec<_>, io::Error>>()?;
|
.collect::<Result<Vec<_>, io::Error>>()
|
||||||
|
.expect("failed to collect template file paths");
|
||||||
|
|
||||||
// loop through each file and sub-directory
|
// loop through each file and sub-directory
|
||||||
for entry in template_files {
|
for entry in template_files {
|
||||||
if entry.is_dir() {
|
if entry.is_dir() {
|
||||||
// replicate templates sub-directory structure in site output directory
|
// replicate templates sub-directory structure in site output directory
|
||||||
let template_sub_dir_suffix = entry.strip_prefix(TEMPLATE_DIR)?;
|
let template_sub_dir_suffix = entry
|
||||||
|
.strip_prefix(TEMPLATE_DIR)
|
||||||
|
.expect("failed to strip prefix from template directory path");
|
||||||
let site_sub_dir = Path::new(SITE_DIR).join(template_sub_dir_suffix);
|
let site_sub_dir = Path::new(SITE_DIR).join(template_sub_dir_suffix);
|
||||||
// create the sub-directory if it doesn't already exist
|
// create the sub-directory if it doesn't already exist
|
||||||
if !site_sub_dir.is_dir() {
|
if !site_sub_dir.is_dir() {
|
||||||
fs::create_dir(site_sub_dir)?;
|
fs::create_dir(site_sub_dir).expect("failed to create a site output sub-directory");
|
||||||
}
|
}
|
||||||
// read each file from the sub-diretory
|
// read each file from the sub-diretory
|
||||||
for file in fs::read_dir(entry)? {
|
for file in fs::read_dir(entry).expect("failed to read file in template sub-directory")
|
||||||
let file = file?;
|
{
|
||||||
|
let file = file.unwrap();
|
||||||
let file_path = file.path();
|
let file_path = file.path();
|
||||||
let file_html = fs::read_to_string(&file_path)?;
|
let file_html = fs::read_to_string(&file_path)
|
||||||
|
.expect("failed to read template html file to string");
|
||||||
// find the index of the h2 tag (represents the page title)
|
// find the index of the h2 tag (represents the page title)
|
||||||
let mut title_start = file_html.find("<h2>").expect("<h2> tag not found");
|
let mut title_start = file_html.find("<h2>").expect("<h2> tag not found");
|
||||||
// increment the index to represent the start of the title text
|
// increment the index to represent the start of the title text
|
||||||
|
@ -93,10 +99,9 @@ fn main() -> Result<(), Box<dyn error::Error>> {
|
||||||
// replace the template directory path with the site output path
|
// replace the template directory path with the site output path
|
||||||
let output_path = file_output_path.replace(TEMPLATE_DIR, SITE_DIR);
|
let output_path = file_output_path.replace(TEMPLATE_DIR, SITE_DIR);
|
||||||
// trim whitespace from the end of the generated html and write to file
|
// trim whitespace from the end of the generated html and write to file
|
||||||
fs::write(output_path, file_output.trim())?
|
fs::write(output_path, file_output.trim())
|
||||||
|
.expect("failed to write html file to site directory")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue