diff options
-rw-r--r-- | src/skaldpress/error.rs | 35 | ||||
-rw-r--r-- | src/skaldpress/main.rs | 81 | ||||
-rw-r--r-- | src/skaldpress/metadata_parser.rs | 2 | ||||
-rw-r--r-- | src/skaldpress/mod.rs | 1 |
4 files changed, 101 insertions, 18 deletions
diff --git a/src/skaldpress/error.rs b/src/skaldpress/error.rs new file mode 100644 index 0000000..f8639b6 --- /dev/null +++ b/src/skaldpress/error.rs @@ -0,0 +1,35 @@ +use std::error::Error; +use std::fmt; + +#[derive(Debug)] +pub enum SkaldpressError { + TemplateReadError(u8, std::io::Error, String), + FileReadError(u8, std::io::Error, String), + DirectoryReadError(u8, std::io::Error, String), + PathOperationError(u8, Option<Box<dyn Error>>), + DirectoryCreateError(u8, std::io::Error, String), +} + +impl fmt::Display for SkaldpressError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + SkaldpressError::TemplateReadError(code, _, file) => { + write!(f, "[SP{}] Error reading template \"{}\"", code, file) + } + SkaldpressError::FileReadError(code, _, file) => { + write!(f, "[SP{}] Error reading file \"{}\"", code, file) + } + SkaldpressError::DirectoryReadError(code, _, dir) => { + write!(f, "[SP{}] Error reading directory \"{}\"", code, dir) + } + SkaldpressError::PathOperationError(code, _) => { + write!(f, "[SP{}] Path operation error", code) + } + SkaldpressError::DirectoryCreateError(code, _, dir) => { + write!(f, "[SP{}] Directory create error \"{}\"", code, dir) + } + } + } +} + +impl std::error::Error for SkaldpressError {} diff --git a/src/skaldpress/main.rs b/src/skaldpress/main.rs index 5be6e13..265f7fc 100644 --- a/src/skaldpress/main.rs +++ b/src/skaldpress/main.rs @@ -2,18 +2,28 @@ use std::fs; use std::path::Path; use skaldpress::macro_processor::MacroProcessor; +use skaldpress::skaldpress::error::SkaldpressError; use skaldpress::skaldpress::metadata_parser::extract_parse_yaml_metadata; const TEMPLATES_DIR: &str = "templates/"; const CONTENT_DIR: &str = "content/"; const BUILD_DIR: &str = "build/"; -fn compile_file(file_path: &Path) -> Result<String, Box<dyn std::error::Error>> { +fn compile_file(file_path: &Path) -> Result<String, SkaldpressError> { let extension = file_path.extension().expect("SP14"); - let file_content = fs::read_to_string(file_path).expect("Failed to read file"); + let file_content = fs::read_to_string(file_path).map_err(|e| { + SkaldpressError::FileReadError( + 1, + e, + file_path.to_str().unwrap_or("unknown file").to_string(), + ) + })?; let (map, file_content) = extract_parse_yaml_metadata(&file_content); - let file_content = match extension.to_str().expect("SP15") { + let file_content = match extension + .to_str() + .ok_or(SkaldpressError::PathOperationError(3, None))? + { "md" => markdown::to_html(file_content), _ => file_content.to_string(), }; @@ -23,11 +33,8 @@ fn compile_file(file_path: &Path) -> Result<String, Box<dyn std::error::Error>> }; let template_file = format!("{}{}.html", TEMPLATES_DIR, template); - //println!( - // "Processing template {} for content file {:?}", - // template_file, file_path - //); - let template = fs::read_to_string(template_file).expect("Failed to read template"); + let template = fs::read_to_string(&template_file) + .map_err(|e| SkaldpressError::TemplateReadError(2, e, template_file))?; let mut macro_processor = MacroProcessor::new(); for (key, value) in map { @@ -41,36 +48,74 @@ fn compile_file(file_path: &Path) -> Result<String, Box<dyn std::error::Error>> fn compile_file_and_write(source_file_path: &Path) -> Result<(), Box<dyn std::error::Error>> { let dest_file_path = Path::new(BUILD_DIR) - .join(source_file_path.strip_prefix(CONTENT_DIR).expect("SP14")) + .join( + source_file_path + .strip_prefix(CONTENT_DIR) + .map_err(|e| SkaldpressError::PathOperationError(4, Some(Box::new(e))))?, + ) .with_extension("html"); - std::fs::create_dir_all(&dest_file_path.parent().expect("SP19")).expect("SP10"); + let dest_dir = &dest_file_path + .parent() + .ok_or(SkaldpressError::PathOperationError(5, None))?; + std::fs::create_dir_all(&dest_dir).map_err(|e| { + SkaldpressError::DirectoryCreateError( + 6, + e, + dest_dir.to_str().unwrap_or("unknown path").to_string(), + ) + })?; let file_content = compile_file(&source_file_path)?; fs::write(&dest_file_path, file_content)?; Ok(()) } -fn compile_files_in_directory(directory: &Path) { - for entry in fs::read_dir(directory).expect("SP4") { - let entry = entry.expect("SP5"); +fn compile_files_in_directory(directory: &Path) -> Result<(), SkaldpressError> { + for entry in fs::read_dir(directory).map_err(|e| { + SkaldpressError::DirectoryReadError( + 6, + e, + directory.to_str().unwrap_or("unknown dir").to_string(), + ) + })? { + let entry = match entry { + Ok(entry) => entry, + Err(e) => { + println!("\x1b[31mError getting file info {:#?}\x1b[0m", e); + continue; + } + }; let path = entry.path(); - let metadata = fs::metadata(&path).expect("SP6"); + if metadata.is_file() { println!("Compiling {:#?}", path.as_path()); - compile_file_and_write(path.as_path()).expect("SP12"); + if let Err(e) = compile_file_and_write(path.as_path()) { + println!( + "\x1b[31mError compiling {:#?}: {}\x1b[0m", + path.as_path(), + e + ); + }; } else if metadata.is_dir() { - compile_files_in_directory(path.as_path()); + if let Err(e) = compile_files_in_directory(path.as_path()) { + println!( + "\x1b[31mError processing directory {:#?}: {}\x1b[0m", + path.as_path(), + e + ); + }; } } + Ok(()) } fn main() { - std::fs::remove_dir_all(Path::new(BUILD_DIR)).expect("SP21"); + let _ = std::fs::remove_dir_all(Path::new(BUILD_DIR)); // Should run twice, or at least the files which uses macros which has to be generated during // runtime - compile_files_in_directory(Path::new(CONTENT_DIR)); + let _ = compile_files_in_directory(Path::new(CONTENT_DIR)); // Just for testing //compile_file_and_write(Path::new("content/test.html")).expect("AYYYYO"); diff --git a/src/skaldpress/metadata_parser.rs b/src/skaldpress/metadata_parser.rs index 7a91b74..77ee7b8 100644 --- a/src/skaldpress/metadata_parser.rs +++ b/src/skaldpress/metadata_parser.rs @@ -16,6 +16,8 @@ impl fmt::Display for YamlValue { } } +/// Extract a beginning YAML block of a input-string into a HashMap +/// /// Currently, all files MUST have a metadata block, or this will fail completely pub fn extract_parse_yaml_metadata<'a>(markdown: &'a str) -> (HashMap<String, YamlValue>, &'a str) { let mut yaml_map = HashMap::new(); diff --git a/src/skaldpress/mod.rs b/src/skaldpress/mod.rs index 1a14898..770035f 100644 --- a/src/skaldpress/mod.rs +++ b/src/skaldpress/mod.rs @@ -1 +1,2 @@ +pub mod error; pub mod metadata_parser; |