|
|
|
@ -1,10 +1,10 @@
|
|
|
|
|
use std::error::Error;
|
|
|
|
|
use std::fs::DirEntry;
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
|
|
use directories::ProjectDirs;
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use crate::config::Config;
|
|
|
|
|
use crate::plan::{convert_old, Plan, PlanMetadata};
|
|
|
|
|
|
|
|
|
@ -84,7 +84,6 @@ impl Storage {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn load_plan_at_path(path: PathBuf, save_local: bool) -> Option<Plan> {
|
|
|
|
|
log::trace!("Loading plan: {path:?}");
|
|
|
|
|
let plan: Plan = match serde_json::from_str(&std::fs::read_to_string(&path).ok()?).ok() {
|
|
|
|
|
Some(plan) => plan,
|
|
|
|
|
None => convert_old(path.clone())?,
|
|
|
|
@ -99,10 +98,22 @@ impl Storage {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//QoL touch file to put recent plans at top! :D
|
|
|
|
|
match std::fs::File::open(&path) {
|
|
|
|
|
Ok(file) => {
|
|
|
|
|
file.set_modified(std::time::SystemTime::now()).ok();
|
|
|
|
|
}
|
|
|
|
|
Err(_) => (),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Some(plan)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn save_plan_at_store_path(file_name: &str, mut plan: Plan, allow_overwrite: bool) -> Result<PathBuf, Box<dyn Error>> {
|
|
|
|
|
pub fn save_plan_at_store_path(
|
|
|
|
|
file_name: &str,
|
|
|
|
|
mut plan: Plan,
|
|
|
|
|
allow_overwrite: bool,
|
|
|
|
|
) -> Result<PathBuf, Box<dyn Error>> {
|
|
|
|
|
let plan_dir = match Self::plan_dir() {
|
|
|
|
|
Some(dir) => dir,
|
|
|
|
|
None => return Err("No plan dir".into()),
|
|
|
|
@ -133,14 +144,18 @@ impl Storage {
|
|
|
|
|
None => return vec![],
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let read_dir = match plan_dir.read_dir() {
|
|
|
|
|
Ok(read_dir) => read_dir,
|
|
|
|
|
let mut read_dir: Vec<DirEntry> = match plan_dir.read_dir() {
|
|
|
|
|
Ok(read_dir) => read_dir.filter_map(|v| v.ok()).collect(),
|
|
|
|
|
Err(_) => return vec![],
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
read_dir.sort_by_key(|v| v.metadata().ok()?.modified().ok());
|
|
|
|
|
read_dir.reverse();
|
|
|
|
|
|
|
|
|
|
read_dir
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|entry| {
|
|
|
|
|
let path = entry.ok()?.path();
|
|
|
|
|
let path = entry.path();
|
|
|
|
|
|
|
|
|
|
if path.extension()? != "json" {
|
|
|
|
|
return None;
|
|
|
|
|