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
mod fetch;
mod parse;

use crate::{Error, response::StcpResponse};
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter};
use core::fmt;

/// Internal code for STCP, such as 600 or 107. In this case, 107 matches to the ZC pubcode.
#[derive(Deserialize, Serialize, Debug, PartialEq)]
pub struct Code(pub String);

impl From<&str> for Code {
    fn from(string: &str) -> Self {
        Code(string.to_string())
    }
}

impl Display for Code {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "{}", &self.0)
    }
}

/// Code shown to the end users, such as 600 or ZC.
#[derive(Deserialize, Serialize, Debug, PartialEq)]
pub struct PubCode(pub String);

/// Represents a STCP line, such as 600, ZC or 3M.
#[derive(Deserialize, Serialize, Debug, PartialEq)]
pub struct Line {
    /// Accessibility level. Unsure what it means or what it is used for.
    pub accessibility: u8,

    /// Code is the internal code for STCP, such as 600 or 107. In this case, 107 matches to the ZC pubcode.
    pub code: Code,

    /// Pubcode is the code for the users, such as 600 or ZC.
    #[serde(rename = "pubcode")]
    pub pub_code: PubCode,

    /// Description of line, including pubcode and initial and final stops.
    pub description: String,
}

type LinesResponse = StcpResponse<Line>;

pub fn fetch_lines() -> Result<Vec<Line>, Error> {
    let json = fetch::fetch_lines()?;

    let lines = parse::parse_lines(&json)?;

    Ok(lines.records)
}