From a7ae9cbc56d2a7d36443e6f7f8c51d90ae6fc214 Mon Sep 17 00:00:00 2001 From: Qrius Date: Thu, 26 Sep 2024 00:11:05 +0200 Subject: Auto pick sensible output extension --- src/skaldpress/error.rs | 1 + src/skaldpress/main.rs | 25 +++++++++++++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/skaldpress/error.rs b/src/skaldpress/error.rs index ae769a5..4f16ffa 100644 --- a/src/skaldpress/error.rs +++ b/src/skaldpress/error.rs @@ -8,6 +8,7 @@ pub const SP_GEN_DEST_STRIP_PREFIX_ERROR: u8 = 4; pub const SP_COMPILE_FILE_EXTENSION_ERROR: u8 = 7; pub const SP_COMPILE_TEMPLATE_MACRO_PROCESS_ERROR: u8 = 9; pub const SP_COMPILE_FILE_MACRO_PROCESS_ERROR: u8 = 10; +pub const SP_COMPILE_FILE_TEMPLATE_EXTENSION_ERROR: u8 = 11; #[derive(Debug)] pub enum SkaldpressError { diff --git a/src/skaldpress/main.rs b/src/skaldpress/main.rs index c1b2006..2aa1790 100644 --- a/src/skaldpress/main.rs +++ b/src/skaldpress/main.rs @@ -6,8 +6,9 @@ use skaldpress::macro_processor::MacroProcessor; use skaldpress::skaldpress::error::SkaldpressError; use skaldpress::skaldpress::error::{ SP_COMPILE_FILE_EXTENSION_ERROR, SP_COMPILE_FILE_EXTENSION_ERROR_2, - SP_COMPILE_FILE_MACRO_PROCESS_ERROR, SP_COMPILE_FILE_TEMPLATE_READ_ERROR, - SP_COMPILE_TEMPLATE_MACRO_PROCESS_ERROR, SP_GEN_DEST_STRIP_PREFIX_ERROR, + SP_COMPILE_FILE_MACRO_PROCESS_ERROR, SP_COMPILE_FILE_TEMPLATE_EXTENSION_ERROR, + SP_COMPILE_FILE_TEMPLATE_READ_ERROR, SP_COMPILE_TEMPLATE_MACRO_PROCESS_ERROR, + SP_GEN_DEST_STRIP_PREFIX_ERROR, }; use skaldpress::skaldpress::metadata_parser::extract_parse_yaml_metadata; use skaldpress::skaldpress::metadata_parser::YamlValue; @@ -19,6 +20,7 @@ const BUILD_DIR: &str = "build/"; struct CompiledFile { content: String, metadata: HashMap, + extension: String, } /// Will attempt to compile a specific file, potentially storing some state about the file @@ -60,13 +62,25 @@ fn compile_file(file_path: &Path) -> Result { return Ok(CompiledFile { content: file_content, metadata: map, + extension: String::from(extension.to_str().unwrap_or("")), }); }; let template_file = format!("{}{}", TEMPLATES_DIR, template); let template = fs::read_to_string(&template_file).map_err(|e| { - SkaldpressError::TemplateReadError(SP_COMPILE_FILE_TEMPLATE_READ_ERROR, e, template_file) + SkaldpressError::TemplateReadError( + SP_COMPILE_FILE_TEMPLATE_READ_ERROR, + e, + template_file.clone(), + ) })?; + let template_extension = + Path::new(&template_file) + .extension() + .ok_or(SkaldpressError::PathOperationError( + SP_COMPILE_FILE_TEMPLATE_EXTENSION_ERROR, + None, + ))?; macro_processor.define_macro_string(String::from("CONTENT"), file_content); let content = macro_processor @@ -75,6 +89,7 @@ fn compile_file(file_path: &Path) -> Result { Ok(CompiledFile { content, metadata: map, + extension: String::from(template_extension.to_str().unwrap_or("")), }) } @@ -93,9 +108,7 @@ fn compile_file_and_write(source_file_path: &Path) -> Result<(), Box