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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
mod parser;
pub mod url;

use anyhow::{anyhow, Result};
use nom::combinator::all_consuming;

#[derive(Clone, Debug, PartialEq)]
pub struct PlayerId(usize);

#[derive(Clone, Debug, PartialEq)]
pub enum Sex {
  Female,
  Male,
}

#[derive(Clone, Debug, PartialEq)]
pub struct Coords(isize, isize);

#[derive(Clone, Debug)]
pub struct Birth {
  pub unix_time: usize,
  pub player_id: PlayerId,
  pub email_hash: String,
  pub sex: Sex,
  pub coords: Coords,
  pub parent: Option<PlayerId>,
  pub pop: usize,
  pub chain: usize,
}

#[derive(Clone, Debug)]
pub enum Cause {
  Disconnect,
  Hunger,
  Killer(PlayerId),
  OldAge,
  Other(String),
}

#[derive(Clone, Debug)]
pub struct Death {
  pub unix_time: usize,
  pub player_id: PlayerId,
  pub email_hash: String,
  pub age: f32,
  pub sex: Sex,
  pub coords: Coords,
  pub cause: Cause,
  pub pop: usize,
}

#[derive(Clone, Debug)]
pub enum LifeLogEntry {
  Birth(Birth),
  Death(Death),
  Error(String),
}

pub type LifeLog = Vec<LifeLogEntry>;

pub fn get(target: &str) -> Result<LifeLog> {
  let text = reqwest::get(target)?.text()?;
  let parser = all_consuming(parser::life_log);
  parser(&text)
    .or_else(|err| Err(anyhow!(format!("{:?}", err))))
    .map(|(_, life_log)| life_log)
}