diff options
Diffstat (limited to 'src/agenda.rs')
| -rw-r--r-- | src/agenda.rs | 58 |
1 files changed, 43 insertions, 15 deletions
diff --git a/src/agenda.rs b/src/agenda.rs index 3cfcfb7..39b07a7 100644 --- a/src/agenda.rs +++ b/src/agenda.rs @@ -9,8 +9,8 @@ use std::{ #[derive(Debug, Deserialize, Serialize)] pub struct AgendaPoint { - pub title: String, - pub adder: String, + title: String, + adder: String, } impl fmt::Display for AgendaPoint { @@ -30,21 +30,49 @@ pub struct Agenda { points: Vec<AgendaPoint>, } -pub fn read_agenda() -> Agenda { - serde_json::from_str::<Agenda>( - &fs::read_to_string("agenda.json") - .expect("Can't read agenda.json")) - .expect("Error parsing agenda.json") +impl Agenda { + fn write(&self) { + fs::write(std::path::Path::new("agenda.json"), + serde_json::to_string_pretty(&self).expect("Can't serialize agenda")) + .expect("Can't write agenda.json"); + } +} + +pub enum ParseError { + NoSuchCommand, } -pub fn write_agenda(agenda: Agenda) { - fs::write(std::path::Path::new("agenda.json"), - serde_json::to_string_pretty(&agenda).expect("Can't serialize agenda")) - .expect("Can't write agenda.json"); +pub fn parse_message(message: &str, sender: &str) -> Result<Option<String>, ParseError> { + if message.starts_with("!add ") { + let mut agenda = read_agenda(); + agenda.points.push(AgendaPoint { + title: message[5..].to_string(), + adder: sender.to_string(), + }); + agenda.write(); + Ok(None) + } else if message.starts_with("!agenda") { + Ok(Some(read_agenda() + .points + .iter() + .map(|p| p.to_string()) + .collect::<Vec<_>>() + .join("\n"))) + } else if message.starts_with("!clear") { + Agenda { + points: Vec::new(), + }.write(); + Ok(None) + } else if message.starts_with("!help") { + Ok(Some("Available commands:\n !add\n !agenda\n !clear\n !help".to_string())) + } else { + Err(ParseError::NoSuchCommand) + } } -pub fn add_point(point: AgendaPoint) { - let mut agenda = read_agenda(); - agenda.points.push(point); - write_agenda(agenda); +fn read_agenda() -> Agenda { + serde_json::from_str::<Agenda>( + &fs::read_to_string("agenda.json") + .expect("Can't read agenda.json")) + .expect("Error parsing agenda.json") } |
