aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorRuben Beltran del Rio <git@r.bdr.sh>2025-04-05 14:43:19 +0200
committerRuben Beltran del Rio <git@r.bdr.sh>2025-04-05 14:43:19 +0200
commit93c0e8ecee430883c62120f9f0b6999acfac4435 (patch)
tree785b9275d9dd525889bfb8cb687a41260ae39b16 /src/main.rs
parentbb3ec6b31f3c709fbf9f2a12c1708d63a61892ff (diff)
Update, panic less and propagate errors
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs36
1 files changed, 22 insertions, 14 deletions
diff --git a/src/main.rs b/src/main.rs
index 0414288..9dd296a 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -3,16 +3,28 @@ mod file_handler;
use std::env::current_dir;
use std::fs::{create_dir_all, remove_dir_all};
-use std::io::Result;
-use std::process::exit;
+use std::io::{Error, ErrorKind, Result};
use crate::file_finder::find_files;
use crate::file_handler::FileHandler;
fn main() -> Result<()> {
let source = current_dir()?;
- let source_name = source.file_name().unwrap().to_string_lossy();
- let parent = source.parent().unwrap();
+ let source_name = source
+ .file_name()
+ .ok_or_else(|| {
+ Error::new(
+ ErrorKind::InvalidData,
+ "Could not read current directory name.",
+ )
+ })?
+ .to_string_lossy();
+ let parent = source.parent().ok_or_else(|| {
+ Error::new(
+ ErrorKind::InvalidData,
+ "Current directory parent was not readable.",
+ )
+ })?;
let gemini_destination_name = format!("{source_name}_gemini");
let gemini_destination = parent.join(gemini_destination_name);
let html_destination_name = format!("{source_name}_html");
@@ -23,22 +35,18 @@ fn main() -> Result<()> {
// Step 2. Load the layout
let mut file_handler = FileHandler::default();
- match file_handler.get_layout_or_panic(&files) {
- Ok(()) => {}
- Err(error) => {
- eprintln!("{error}");
- exit(1);
- }
- }
+ file_handler.get_layout(&files)?;
// Step 3. Prepare the target priority
let _ = remove_dir_all(&html_destination);
let _ = remove_dir_all(&gemini_destination);
- create_dir_all(&html_destination).expect("Could not create HTML directory.");
- create_dir_all(&gemini_destination).expect("Could not create Gemini directory.");
+ create_dir_all(&html_destination)
+ .map_err(|e| Error::new(e.kind(), "Could not create HTML directory."))?;
+ create_dir_all(&gemini_destination)
+ .map_err(|e| Error::new(e.kind(), "Could not create Gemini directory."))?;
// Step 4. Process all files
- file_handler.handle_all(&source, &html_destination, &gemini_destination, &files);
+ file_handler.handle_all(&source, &html_destination, &gemini_destination, &files)?;
Ok(())
}