use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Memory {
pub content: String,
pub updated_at: DateTime<Utc>,
pub version: u64,
}
impl Memory {
pub fn new() -> Self {
Self {
content: String::new(),
updated_at: Utc::now(),
version: 1,
}
}
pub fn with_content(content: impl Into<String>) -> Self {
Self {
content: content.into(),
updated_at: Utc::now(),
version: 1,
}
}
pub fn update(&mut self, content: impl Into<String>) {
self.content = content.into();
self.updated_at = Utc::now();
self.version += 1;
}
pub fn append(&mut self, content: impl AsRef<str>) {
if !self.content.is_empty() {
self.content.push('\n');
}
self.content.push_str(content.as_ref());
self.updated_at = Utc::now();
self.version += 1;
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DailyNote {
pub date: String,
pub content: String,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
impl DailyNote {
pub fn today() -> Self {
let now = Utc::now();
Self {
date: now.format("%Y-%m-%d").to_string(),
content: String::new(),
created_at: now,
updated_at: now,
}
}
pub fn for_date(date: impl Into<String>) -> Self {
let now = Utc::now();
Self {
date: date.into(),
content: String::new(),
created_at: now,
updated_at: now,
}
}
pub fn update(&mut self, content: impl Into<String>) {
self.content = content.into();
self.updated_at = Utc::now();
}
pub fn append(&mut self, content: impl AsRef<str>) {
if !self.content.is_empty() {
self.content.push('\n');
}
self.content.push_str(content.as_ref());
self.updated_at = Utc::now();
}
pub fn filename(&self) -> String {
format!("{}.md", self.date)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_memory_creation() {
let memory = Memory::new();
assert!(memory.content.is_empty());
assert_eq!(memory.version, 1);
}
#[test]
fn test_memory_with_content() {
let memory = Memory::with_content("Test content");
assert_eq!(memory.content, "Test content");
}
#[test]
fn test_memory_update() {
let mut memory = Memory::new();
memory.update("New content");
assert_eq!(memory.content, "New content");
assert_eq!(memory.version, 2);
}
#[test]
fn test_memory_append() {
let mut memory = Memory::with_content("Line 1");
memory.append("Line 2");
assert_eq!(memory.content, "Line 1\nLine 2");
assert_eq!(memory.version, 2);
}
#[test]
fn test_daily_note_today() {
let note = DailyNote::today();
assert!(!note.date.is_empty());
assert!(note.content.is_empty());
}
#[test]
fn test_daily_note_filename() {
let note = DailyNote::for_date("2024-01-15");
assert_eq!(note.filename(), "2024-01-15.md");
}
}