reintroduce parsing of epoch

This commit is contained in:
Cole Helbling 2020-03-12 15:50:59 -07:00
parent d6fd90693e
commit e7cb285183
No known key found for this signature in database
GPG Key ID: B37E0F2371016A4C
1 changed files with 16 additions and 10 deletions

View File

@ -1,7 +1,7 @@
use crate::dir::Dir; use crate::dir::Dir;
use crate::types::{Rank, Timestamp}; use crate::types::{Rank, Timestamp};
use crate::util; use crate::util;
use anyhow::{anyhow, Context, Result}; use anyhow::{anyhow, bail, Context, Result};
use fs2::FileExt; use fs2::FileExt;
use std::fs::{self, File, OpenOptions}; use std::fs::{self, File, OpenOptions};
use std::io::{self, BufRead, BufReader, BufWriter}; use std::io::{self, BufRead, BufReader, BufWriter};
@ -73,10 +73,10 @@ impl DB {
pub fn migrate<P: AsRef<Path>>(&mut self, path: P) -> Result<()> { pub fn migrate<P: AsRef<Path>>(&mut self, path: P) -> Result<()> {
if !self.dirs.is_empty() { if !self.dirs.is_empty() {
return Err(anyhow!( bail!(
"To prevent conflicts, you can only migrate from z with an empty \ "To prevent conflicts, you can only migrate from z with an empty \
zoxide database!" zoxide database!"
)); );
} }
let z_db_file = File::open(path).with_context(|| anyhow!("could not open z db file"))?; let z_db_file = File::open(path).with_context(|| anyhow!("could not open z db file"))?;
@ -94,26 +94,32 @@ impl DB {
let split_line = line.rsplitn(3, '|').collect::<Vec<&str>>(); let split_line = line.rsplitn(3, '|').collect::<Vec<&str>>();
match split_line.as_slice() { match split_line.as_slice() {
[_, rank_str, path_str] => { [epoch_str, rank_str, path_str] => {
// Set last_accessed to current time; otherwise, the rank will get scaled down, let epoch = epoch_str
// depending on how old the imported entry is (import in-place and let zoxide .parse::<i64>()
// scale down organically) .with_context(|| anyhow!("could not parse epoch: '{}'", epoch_str))?;
let epoch = util::get_current_time()?;
let rank = rank_str let rank = rank_str
.parse::<f64>() .parse::<f64>()
.with_context(|| anyhow!("could not parse rank: '{}'", rank_str))?; .with_context(|| anyhow!("could not parse rank: '{}'", rank_str))?;
let path = match PathBuf::from(path_str).canonicalize() {
Ok(path) => path,
Err(e) => {
eprintln!("invalid path '{}' at line {}: {}", path_str, line_number, e);
continue;
}
};
// FIXME: When we switch to PathBuf for storing directories inside Dir, just // FIXME: When we switch to PathBuf for storing directories inside Dir, just
// pass `PathBuf::from(path_str)` // pass `PathBuf::from(path_str)`
self.dirs.push(Dir { self.dirs.push(Dir {
path: path_str.to_string(), path: path.display().to_string(),
last_accessed: epoch, last_accessed: epoch,
rank, rank,
}); });
} }
[] | [""] => {} // ignore blank lines [] | [""] => {} // ignore blank lines
line => { line => {
eprintln!("invalid line {}: {:?}", line_number, line); eprintln!("invalid entry at line {}: {:?}", line_number, line);
continue; continue;
} }
}; };