~rbdr/page

48ea90800c3aa6055247fe2c56bc8e9d63024bd3 — Ruben Beltran del Rio 1 year, 2 months ago 0e3bcda
Handle layout error better
4 files changed, 27 insertions(+), 4 deletions(-)

A Makefile
M README.gmi
M src/file_handler/mod.rs
M src/main.rs
A Makefile => Makefile +8 -0
@@ 0,0 1,8 @@
profile = dev

default: build

build:
	cargo build --profile $(profile)

.PHONY: build

M README.gmi => README.gmi +8 -0
@@ 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`.

M src/file_handler/mod.rs => src/file_handler/mod.rs +3 -3
@@ 35,18 35,18 @@ impl FileHandler {
        FileType::Unknown
    }

    pub fn get_layout_or_panic(&mut self, files: &Vec<File>) {
    pub fn get_layout_or_panic(&mut self, files: &Vec<File>) -> 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<File>) {

M src/main.rs => src/main.rs +8 -1
@@ 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);