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.
This commit is contained in:
Daniel Szabo 2022-05-09 22:36:13 +01:00
parent 9e4940eb44
commit cd2eea30ce
3 changed files with 30 additions and 28 deletions

View file

@ -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() {

View file

@ -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
}
});
}

View file

@ -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(),
)
}
}
}