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.
This commit is contained in:
parent
9e4940eb44
commit
cd2eea30ce
3 changed files with 30 additions and 28 deletions
|
@ -7,29 +7,24 @@ const ANIMAL_NAMES: &[&str] = &[
|
||||||
"wolf", "duck", "lion", "shark", "worm", "eagle", "lizard", "sheep", "zebra",
|
"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();
|
let mut result: Vec<&str> = Vec::new();
|
||||||
|
|
||||||
if n == 0 {
|
if number == 0 {
|
||||||
return ANIMAL_NAMES[0].parse().unwrap();
|
return ANIMAL_NAMES[0].parse().unwrap();
|
||||||
} else if n == 1 {
|
|
||||||
return ANIMAL_NAMES[1].parse().unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// max 4 animals so 6 * 6 = 64 bits
|
// max 4 animals so 6 * 6 = 64 bits
|
||||||
let mut power = 6;
|
let mut power = 6;
|
||||||
loop {
|
loop {
|
||||||
let d = n / ANIMAL_NAMES.len().pow(power) as u64;
|
let digit = number / ANIMAL_NAMES.len().pow(power) as u64;
|
||||||
|
if !(result.is_empty() && digit == 0) {
|
||||||
if !(result.is_empty() && d == 0) {
|
result.push(ANIMAL_NAMES[digit as usize]);
|
||||||
result.push(ANIMAL_NAMES[d as usize]);
|
|
||||||
}
|
}
|
||||||
|
number -= digit * ANIMAL_NAMES.len().pow(power) as u64;
|
||||||
n -= d * ANIMAL_NAMES.len().pow(power) as u64;
|
|
||||||
|
|
||||||
if power > 0 {
|
if power > 0 {
|
||||||
power -= 1;
|
power -= 1;
|
||||||
} else {
|
} else if power <= 0 || number == 0 {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -37,10 +32,10 @@ pub fn to_animal_names(mut n: u64) -> String {
|
||||||
result.join("-")
|
result.join("-")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_u64(n: &str) -> u64 {
|
pub fn to_u64(animal_names: &str) -> u64 {
|
||||||
let mut result: u64 = 0;
|
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();
|
let mut pow = animals.len();
|
||||||
for i in 0..animals.len() {
|
for i in 0..animals.len() {
|
||||||
|
|
13
src/main.rs
13
src/main.rs
|
@ -390,15 +390,18 @@ fn remove_expired(pastas: &mut Vec<Pasta>) {
|
||||||
} as i64;
|
} as i64;
|
||||||
|
|
||||||
pastas.retain(|p| {
|
pastas.retain(|p| {
|
||||||
// delete the files too
|
// expiration is `never` or not reached
|
||||||
if p.expiration < timenow {
|
if p.expiration == 0 || p.expiration > timenow {
|
||||||
|
// keep
|
||||||
|
true
|
||||||
|
} else {
|
||||||
// remove the file itself
|
// remove the file itself
|
||||||
fs::remove_file(format!("./pasta_data/{}/{}", p.id_as_animals(), p.file));
|
fs::remove_file(format!("./pasta_data/{}/{}", p.id_as_animals(), p.file));
|
||||||
// and remove the containing directory
|
// and remove the containing directory
|
||||||
fs::remove_dir(format!("./pasta_data/{}/", p.id_as_animals()));
|
fs::remove_dir(format!("./pasta_data/{}/", p.id_as_animals()));
|
||||||
};
|
// remove
|
||||||
|
false
|
||||||
p.expiration == 0 || p.expiration > timenow
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -32,6 +32,9 @@ impl Pasta {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn expiration_as_string(&self) -> String {
|
pub fn expiration_as_string(&self) -> String {
|
||||||
|
if self.expiration == 0 {
|
||||||
|
String::from("Never")
|
||||||
|
} else {
|
||||||
let date =
|
let date =
|
||||||
DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(self.expiration, 0), Utc);
|
DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(self.expiration, 0), Utc);
|
||||||
format!(
|
format!(
|
||||||
|
@ -42,6 +45,7 @@ impl Pasta {
|
||||||
date.minute(),
|
date.minute(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for Pasta {
|
impl fmt::Display for Pasta {
|
||||||
|
|
Loading…
Reference in a new issue