use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::io;
use std::path::Path;
use std::time::{SystemTime, UNIX_EPOCH};
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct RecentNotes {
#[serde(default)]
opened: HashMap<String, i64>,
}
fn now_millis() -> i64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_millis() as i64)
.unwrap_or(0)
}
impl RecentNotes {
pub fn load(path: &Path) -> Self {
fs::read_to_string(path)
.ok()
.and_then(|s| toml::from_str(&s).ok())
.unwrap_or_default()
}
pub fn save(&self, path: &Path) -> io::Result<()> {
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)?;
}
let toml = toml::to_string_pretty(self)
.map_err(|e| io::Error::other(format!("toml serialization error: {e}")))?;
fs::write(path, toml)
}
pub fn mark_opened(&mut self, note: &str) {
self.opened.insert(note.to_string(), now_millis());
}
pub fn last_opened(&self, note: &str) -> Option<i64> {
self.opened.get(note).copied()
}
pub fn rename(&mut self, old: &str, new: &str) {
if let Some(time) = self.opened.remove(old) {
self.opened.insert(new.to_string(), time);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn mark_opened_then_last_opened_is_set() {
let mut r = RecentNotes::default();
assert_eq!(r.last_opened("x"), None);
r.mark_opened("x");
assert!(r.last_opened("x").is_some());
}
#[test]
fn rename_moves_entry_preserving_time() {
let mut r = RecentNotes::default();
r.mark_opened("old");
let t = r.last_opened("old").unwrap();
r.rename("old", "new");
assert_eq!(r.last_opened("old"), None);
assert_eq!(r.last_opened("new"), Some(t));
}
#[test]
fn rename_unknown_note_is_noop() {
let mut r = RecentNotes::default();
r.rename("missing", "new");
assert_eq!(r.last_opened("new"), None);
}
#[test]
fn roundtrips_names_with_slashes() {
let mut r = RecentNotes::default();
r.opened.insert("project-a/standup".into(), 42);
let toml = toml::to_string_pretty(&r).unwrap();
let back: RecentNotes = toml::from_str(&toml).unwrap();
assert_eq!(back.last_opened("project-a/standup"), Some(42));
}
}