From: Ruben Beltran del Rio Date: Sun, 16 Apr 2023 14:56:10 +0000 (+0200) Subject: Handle layout error better X-Git-Tag: 1.0.0~2 X-Git-Url: https://git.r.bdr.sh/rbdr/page/commitdiff_plain/48ea90800c3aa6055247fe2c56bc8e9d63024bd3?ds=sidebyside Handle layout error better --- diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b64b6e2 --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +profile = dev + +default: build + +build: + cargo build --profile $(profile) + +.PHONY: build diff --git a/README.gmi b/README.gmi index ade3518..d53a207 100644 --- a/README.gmi +++ b/README.gmi @@ -59,3 +59,11 @@ in the directory. ## What happens to files that aren't gemini? They're copied as-is. + +# Building + +This project is built using cargo. A makefile is provided to run common tasks. + +Build dev version with `make` or `make build`. + +Build release with `make -e profile=release` or `make -e profile=release build`. diff --git a/src/file_handler/mod.rs b/src/file_handler/mod.rs index a106c27..4932ef1 100644 --- a/src/file_handler/mod.rs +++ b/src/file_handler/mod.rs @@ -35,18 +35,18 @@ impl FileHandler { FileType::Unknown } - pub fn get_layout_or_panic(&mut self, files: &Vec) { + pub fn get_layout_or_panic(&mut self, files: &Vec) -> Result<(), &str> { for file in files { match file.file_type { FileType::Layout => { let layout_text = read_to_string(&file.path).unwrap(); self.layout = Some(layout_text); - return; + return Ok(()); }, _ => {} } } - panic!("No layout found. Please ensure there's a _layout.html file at the root"); + Err("No layout found. Please ensure there's a _layout.html file at the root") } pub fn handle_all(&self, source: &PathBuf, destination: &PathBuf, files: &Vec) { diff --git a/src/main.rs b/src/main.rs index fdfe863..c6de435 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ mod file_finder; mod file_handler; use std::io::Result; +use std::process::exit; use std::env::current_dir; use std::fs::{create_dir_all, remove_dir_all}; @@ -27,7 +28,13 @@ fn main() -> Result<()> { // Step 3. Load the layout let mut file_handler = FileHandler::default(); - file_handler.get_layout_or_panic(&files); + match file_handler.get_layout_or_panic(&files) { + Ok(_) => {}, + Err(error) => { + eprintln!("{}", error); + exit(1); + } + } // Step 4. Process all files file_handler.handle_all(&source, &destination, &files);