//! Version 2 adds a note count and fixes the crash command. use sausage_actor::{json, messages, reply}; const DATA: &str = "/data/notes.json"; fn save(notes: &[String]) { std::fs::write(DATA, serde_json::to_string(notes).unwrap()).unwrap(); std::fs::write("/public/notes.txt", notes.join("\n") + "\n").unwrap(); } fn main() { let mut notes: Vec = std::fs::read_to_string(DATA) .ok() .and_then(|text| serde_json::from_str(&text).ok()) .unwrap_or_default(); for message in messages() { let body = &message.body; let response = match message.kind() { "add" => { notes.push(body["text"].as_str().unwrap_or_default().to_string()); save(¬es); json!({"type": "added", "count": notes.len()}) } "list" => json!({"type": "notes", "items": notes}), "count" => json!({"type": "count", "count": notes.len(), "version": 2}), "crash" => json!({"type": "not_crashing", "version": 2}), _ => json!({"type": "error", "reason": "unknown message"}), }; let _ = reply(&message, response); } }