From cd2eea30ce3fa266a5cda9b76de957b1f584a897 Mon Sep 17 00:00:00 2001
From: Daniel Szabo <daniel.szabo99@outlook.com>
Date: Mon, 9 May 2022 22:36:13 +0100
Subject: [PATCH] Bugfix - #11

Also changed expiration date to show "Never" instead of unix epoch in the pasta list, and renamed a few variables in animalnumbers.rs to make the function more readable.
---
 src/animalnumbers.rs | 23 +++++++++--------------
 src/main.rs          | 13 ++++++++-----
 src/pasta.rs         | 22 +++++++++++++---------
 3 files changed, 30 insertions(+), 28 deletions(-)

diff --git a/src/animalnumbers.rs b/src/animalnumbers.rs
index c6a7e27..a94e702 100644
--- a/src/animalnumbers.rs
+++ b/src/animalnumbers.rs
@@ -7,29 +7,24 @@ const ANIMAL_NAMES: &[&str] = &[
     "wolf", "duck", "lion", "shark", "worm", "eagle", "lizard", "sheep", "zebra",
 ];
 
-pub fn to_animal_names(mut n: u64) -> String {
+pub fn to_animal_names(mut number: u64) -> String {
     let mut result: Vec<&str> = Vec::new();
 
-    if n == 0 {
+    if number == 0 {
         return ANIMAL_NAMES[0].parse().unwrap();
-    } else if n == 1 {
-        return ANIMAL_NAMES[1].parse().unwrap();
     }
 
     // max 4 animals so 6 * 6 = 64 bits
     let mut power = 6;
     loop {
-        let d = n / ANIMAL_NAMES.len().pow(power) as u64;
-
-        if !(result.is_empty() && d == 0) {
-            result.push(ANIMAL_NAMES[d as usize]);
+        let digit = number / ANIMAL_NAMES.len().pow(power) as u64;
+        if !(result.is_empty() && digit == 0) {
+            result.push(ANIMAL_NAMES[digit as usize]);
         }
-
-        n -= d * ANIMAL_NAMES.len().pow(power) as u64;
-
+        number -= digit * ANIMAL_NAMES.len().pow(power) as u64;
         if power > 0 {
             power -= 1;
-        } else {
+        } else if power <= 0 || number == 0 {
             break;
         }
     }
@@ -37,10 +32,10 @@ pub fn to_animal_names(mut n: u64) -> String {
     result.join("-")
 }
 
-pub fn to_u64(n: &str) -> u64 {
+pub fn to_u64(animal_names: &str) -> u64 {
     let mut result: u64 = 0;
 
-    let animals: Vec<&str> = n.split("-").collect();
+    let animals: Vec<&str> = animal_names.split("-").collect();
 
     let mut pow = animals.len();
     for i in 0..animals.len() {
diff --git a/src/main.rs b/src/main.rs
index 07139af..48a5b06 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -390,15 +390,18 @@ fn remove_expired(pastas: &mut Vec<Pasta>) {
     } as i64;
 
     pastas.retain(|p| {
-        // delete the files too
-        if p.expiration < timenow {
+        // expiration is `never` or not reached
+        if p.expiration == 0 || p.expiration > timenow {
+            // keep
+            true
+        } else {
             // remove the file itself
             fs::remove_file(format!("./pasta_data/{}/{}", p.id_as_animals(), p.file));
             // and remove the containing directory
             fs::remove_dir(format!("./pasta_data/{}/", p.id_as_animals()));
-        };
-
-        p.expiration == 0 || p.expiration > timenow
+            // remove
+            false
+        }
     });
 }
 
diff --git a/src/pasta.rs b/src/pasta.rs
index 5ac8844..4f67823 100644
--- a/src/pasta.rs
+++ b/src/pasta.rs
@@ -32,15 +32,19 @@ impl Pasta {
     }
 
     pub fn expiration_as_string(&self) -> String {
-        let date =
-            DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(self.expiration, 0), Utc);
-        format!(
-            "{:02}-{:02} {}:{}",
-            date.month(),
-            date.day(),
-            date.hour(),
-            date.minute(),
-        )
+        if self.expiration == 0 {
+            String::from("Never")
+        } else {
+            let date =
+                DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(self.expiration, 0), Utc);
+            format!(
+                "{:02}-{:02} {}:{}",
+                date.month(),
+                date.day(),
+                date.hour(),
+                date.minute(),
+            )
+        }
     }
 }