diff --git a/src/lib.rs b/src/lib.rs index f2be644..5d5cd7d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -52,11 +52,14 @@ pub fn try_template( bail!(e); } - if should_keep(action)? { + // wrap project dir in Option to save to session file + let project_dir = if should_keep(action)? { println!("Saved as {project_dir:?}."); + Some(project_dir) } else { remove_attempt(&project_dir)?; - } + None + }; // save session data to file let file = File::create(get_session_path(tmp_dir))?; diff --git a/src/main.rs b/src/main.rs index 2f3ef3f..7f387a2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,15 +24,19 @@ fn main() -> anyhow::Result<()> { } else if args.required.list_templates { println!("{}", Templates::try_from(data_dir.as_ref())?); } else if args.required.previous { - let session = Session::from_file(&get_session_path(&tmp_dir))?; - if !session.previous_attempt.exists() { - bail!( - "Last modified attempt, {:?} does not exist! Did it move?\nMake a new attempt!", - session.previous_attempt - ); + match Session::from_file(&get_session_path(&tmp_dir))?.previous_attempt { + Some(path) => { + if !path.exists() { + bail!( + "Last saved attempt, {:?} does not exist! Did it move?", + path + ); + } + + atmpt::summon_and_wait(&args.editor, &path)?; + } + None => bail!("No previous attempt found in session file!"), } - - atmpt::summon_and_wait(&args.editor, &session.previous_attempt)?; } else { let template = match args.required.template { Some(t) => t, diff --git a/src/session.rs b/src/session.rs index d4a0c85..818de1e 100644 --- a/src/session.rs +++ b/src/session.rs @@ -10,7 +10,7 @@ use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize)] pub struct Session { pub last_template: String, - pub previous_attempt: PathBuf, + pub previous_attempt: Option, } impl Session {