aboutsummaryrefslogtreecommitdiffstats
path: root/src/agenda.rs
blob: 3cfcfb7a7a3fec007d23071ddc9a0895085b518f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use serde::{
    Deserialize,
    Serialize,
};
use std::{
    fmt,
    fs,
};

#[derive(Debug, Deserialize, Serialize)]
pub struct AgendaPoint {
    pub title: String,
    pub adder: String,
}

impl fmt::Display for AgendaPoint {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} ({})", self.title, self.adder)
    }
}

impl AgendaPoint {
    pub fn to_add_message(&self) -> String {
        format!("'{}' added by {}", self.title, self.adder)
    }
}

#[derive(Deserialize, Serialize)]
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")
}

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 add_point(point: AgendaPoint) {
    let mut agenda = read_agenda();
    agenda.points.push(point);
    write_agenda(agenda);
}