stapix

Yet another static page generator for photo galleries

Commit
f8e5c2a354cda24263384337fa60a2fee1806c92
Parent
8d0830bbb288854fd0a56fdb79fe47169797a62f
Author
Pablo <pablo-escobar@riseup.net>
Date

Rewrote some of the macros

Rewrote some of the macros for logging erros and warnings

Diffstat

1 file changed, 28 insertions, 18 deletions

Status File Name N° Changes Insertions Deletions
Modified src/main.rs 46 28 18
diff --git a/src/main.rs b/src/main.rs
@@ -51,11 +51,22 @@ macro_rules! log {
     };
 }
 
+macro_rules! logln {
+    ($fmt:literal) => {
+        println!(concat!("{info_header_msg} ", $fmt),
+               info_header_msg = "[INFO]".green().bold());
+    };
+    ($fmt:literal, $($x:ident = $y:expr),+ $(,)?) => {
+        println!(concat!("{info_header_msg} ", $fmt), $($x = $y),+,
+               info_header_msg = "[INFO]".green().bold());
+    };
+}
+
 macro_rules! log_done {
     () => { println!(" Done!"); };
 }
 
-macro_rules! error {
+macro_rules! errorln {
     ($fmt:literal) => {
         eprintln!(concat!("\n{error_header_msg} ", $fmt),
                   error_header_msg = "[ERROR]".red().bold());
@@ -74,7 +85,7 @@ macro_rules! error {
     };
 }
 
-macro_rules! warning {
+macro_rules! warningln {
     ($fmt:literal) => {
         println!(concat!("{warning_header_msg} ", $fmt),
                  warning_header_msg = "[WARNING]".yellow().bold());
@@ -111,13 +122,13 @@ fn main() -> ExitCode {
     let (program, config) = match &args[..] {
         [program, config] => (program, config),
         [program] => {
-            error!("Expected 1 command line argument, found none";
+            errorln!("Expected 1 command line argument, found none";
                    newline = false);
             usage!(program);
             return ExitCode::FAILURE;
         }
         [program, ..] => {
-            error!("Expected 1 command line argument, found many";
+            errorln!("Expected 1 command line argument, found many";
                    newline = false);
             usage!(program);
             return ExitCode::FAILURE;
@@ -129,14 +140,14 @@ fn main() -> ExitCode {
     match f.map(serde_yaml::from_reader::<_, Vec<Picture>>) {
         // Error opening the config file
         Err(err) => {
-            error!("Couldn't open {config:?}: {err}",
+            errorln!("Couldn't open {config:?}: {err}",
                    config = config, err = err; newline = false);
             usage!(program);
             ExitCode::FAILURE
         }
         // Error parsing the config file
         Ok(Err(err)) => {
-            error!("Couldn't parse {config:?}: {err}",
+            errorln!("Couldn't parse {config:?}: {err}",
                    config = config, err = err; newline = false);
             usage_config!();
             ExitCode::FAILURE
@@ -148,7 +159,6 @@ fn main() -> ExitCode {
 /// Coordinates the rendering of all the pages and file conversions
 fn render_gallery(pics: Vec<Picture>) -> ExitCode {
     log!("Copying image files to the target directory...");
-    print!("");
 
     for pic in &pics {
         let mut target_path = PathBuf::from(TARGET_PATH);
@@ -156,7 +166,7 @@ fn render_gallery(pics: Vec<Picture>) -> ExitCode {
         target_path.push(&pic.file_name);
 
         if let Err(err) = fs::copy(&pic.path, &target_path) {
-            error!("Couldn't copy file {src:?} to {target:?}: {err}",
+            errorln!("Couldn't copy file {src:?} to {target:?}: {err}",
                    src = pic.path, target = target_path, err = err);
             return ExitCode::FAILURE;
         }
@@ -167,7 +177,7 @@ fn render_gallery(pics: Vec<Picture>) -> ExitCode {
     // ========================================================================
     for pic in &pics {
         if pic.alt.is_empty() {
-            warning!(
+            warningln!(
                 "Empty text alternative was specified for the file {name:?}",
                 name = pic.file_name
             );
@@ -182,7 +192,7 @@ fn render_gallery(pics: Vec<Picture>) -> ExitCode {
     );
     let (sender, reciever) = mpsc::channel();
 
-    log!("Started rendering WebP thumbnails (using {n} threads)\n",
+    logln!("Started rendering WebP thumbnails (using {n} threads)",
          n = num_threads);
 
     for pic in &pics {
@@ -200,7 +210,7 @@ fn render_gallery(pics: Vec<Picture>) -> ExitCode {
         }
     }
 
-    log!("Done rendering WebP thumbnails!\n");
+    logln!("Done rendering WebP thumbnails!");
 
     // ========================================================================
     log!("Rendering index.html...");
@@ -282,7 +292,7 @@ fn render_pic_page(pic: &Picture) -> io::Result<()> {
     let mut f = match File::create(&path) {
         Ok(file) => file,
         Err(err) => {
-            error!("Could not open file {path:?}: {err}",
+            errorln!("Could not open file {path:?}: {err}",
                    path = path, err = err);
             return Err(err);
         }
@@ -415,7 +425,7 @@ fn render_thumbnail(pic: Picture) -> bool {
             .expect("os should support file modification date");
 
         if thumb_mod_date > img_mod_date {
-            warning!("Skipped rendering the thumbnail for {name:?} (update {path:?} to overwrite)",
+            warningln!("Skipped rendering the thumbnail for {name:?} (update {path:?} to overwrite)",
                      name = pic.file_name, path = pic.path);
             return true;
         }
@@ -424,7 +434,7 @@ fn render_thumbnail(pic: Picture) -> bool {
     let mut thumb_file = match File::create(&thumb_path) {
         Ok(f)    => f,
         Err(err) => {
-            error!("Couldn't open WebP thumbnail file {thumb_path:?}: {err}",
+            errorln!("Couldn't open WebP thumbnail file {thumb_path:?}: {err}",
                    thumb_path = thumb_path, err = err);
             return false;
         }
@@ -433,7 +443,7 @@ fn render_thumbnail(pic: Picture) -> bool {
     let img_reader = match ImageReader::open(&pic.path) {
         Ok(r)    => r,
         Err(err) => {
-            error!(
+            errorln!(
                 "Couldn't open file {path:?} to render WebP thumbnail: {err}",
                 path = pic.file_name,
                 err = err
@@ -445,7 +455,7 @@ fn render_thumbnail(pic: Picture) -> bool {
     let img = match img_reader.decode() {
         Ok(img)  => img,
         Err(err) => {
-            error!(
+            errorln!(
                 "Faileded to decode image file {name:?}: {err}",
                 name = pic.file_name,
                 err = err
@@ -469,12 +479,12 @@ fn render_thumbnail(pic: Picture) -> bool {
         .encode(IMAGE_QUALITY);
 
     if let Err(err) = thumb_file.write_all(&mem) {
-        error!("Couldn't write WebP thumnail to file {path:?}: {err}",
+        errorln!("Couldn't write WebP thumnail to file {path:?}: {err}",
                path = thumb_path, err = err);
         return false;
     }
 
-    log!("Rendered WebP thumbnail for {name:?}\n", name = pic.file_name);
+    logln!("Rendered WebP thumbnail for {name:?}", name = pic.file_name);
     true
 }