diff --git a/src/list.rs b/src/list.rs index b63c199..04e4d02 100644 --- a/src/list.rs +++ b/src/list.rs @@ -8,7 +8,7 @@ use std::path::Path; pub fn list() -> Result<(), TourError> { require_tour()?; let total = get_tour_step()?; - let current = get_current_step(); + let current = get_current_step()?; if total == 0 { println!("No steps yet."); diff --git a/src/status.rs b/src/status.rs index a1c3e89..189fb65 100644 --- a/src/status.rs +++ b/src/status.rs @@ -9,7 +9,7 @@ pub fn status() -> Result<(), TourError> { let total = get_tour_step()?; let ended = Path::new(TOUR_DIR).join("ended").exists(); - let current = get_current_step(); + let current = get_current_step()?; println!("Tour: {} steps{}", total, if ended { " (ended)" } else { "" }); diff --git a/src/step.rs b/src/step.rs index 681988f..fc064f0 100644 --- a/src/step.rs +++ b/src/step.rs @@ -31,7 +31,7 @@ pub fn next(n: Option) -> Result<(), TourError> { } let delta = n.unwrap_or(1); let last = total - 1; - match get_current_step() { + match get_current_step()? { Some(current) if current >= last => { println!("Already at the last step ({total}/{total})."); Ok(()) @@ -48,7 +48,7 @@ pub fn prev(n: Option) -> Result<(), TourError> { return Err(TourError::NoStepsToNavigate); } let delta = n.unwrap_or(1); - let current = get_current_step().ok_or(TourError::NotStarted)?; + let current = get_current_step()?.ok_or(TourError::NotStarted)?; if current == 0 { println!("Already at the first step (1/{total})."); return Ok(()); @@ -72,7 +72,7 @@ fn go_to_step(target: u32, total: u32) -> Result<(), TourError> { // Files the tour owns in the working directory: the current step's set if a // session exists, otherwise every file any step tracks (conservative). - let current_files = match get_current_step() { + let current_files = match get_current_step()? { Some(current) if steps_dir.join(current.to_string()).is_dir() => { let dir = steps_dir.join(current.to_string()); let mut set = BTreeSet::new(); diff --git a/src/utils.rs b/src/utils.rs index e963fa0..da02b4f 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -13,14 +13,42 @@ pub fn require_tour() -> Result<(), TourError> { Ok(()) } -pub fn get_current_step() -> Option { - fs::read_to_string(SESSION_PATH) - .ok() - .and_then(|s| { - s.lines() - .find_map(|l| l.strip_prefix("STEP=")) - .and_then(|v| v.trim().parse::().ok()) - }) +/// Fails the command if the tour is missing or already ended. Use for any +/// command that mutates authoring state. +pub fn require_active() -> Result<(), TourError> { + require_tour()?; + if Path::new(TOUR_DIR).join("ended").exists() { + return Err(TourError::TourEnded); + } + Ok(()) +} + +/// Collapses `./a/./b` to `a/b` so the same file always has one spelling in +/// the staged/removed lists and in step snapshots. +pub fn normalize_path(p: &Path) -> PathBuf { + // components() drops interior "." but keeps a leading one, so filter it out. + p.components() + .filter(|c| !matches!(c, std::path::Component::CurDir)) + .collect() +} + +/// `Ok(None)` means no session yet; a session file with an unreadable STEP is +/// corruption, not "not started". +pub fn get_current_step() -> Result, TourError> { + let content = match fs::read_to_string(SESSION_PATH) { + Ok(c) => c, + Err(e) if e.kind() == io::ErrorKind::NotFound => return Ok(None), + Err(e) => return Err(e.into()), + }; + match content.lines().find_map(|l| l.strip_prefix("STEP=")) { + None if content.trim().is_empty() => Ok(None), + None => Err(TourError::CorruptedTour( + "session file has no STEP= entry".into(), + )), + Some(v) => v.trim().parse::().map(Some).map_err(|_| { + TourError::CorruptedTour(format!("session step {:?} is not a number", v.trim())) + }), + } } pub fn get_tour_step() -> Result {