stcp_scraper/lines/
mod.rs

1mod fetch;
2mod parse;
3
4use crate::{Error, response::StcpResponse};
5use serde::{Deserialize, Serialize};
6use std::fmt::{Display, Formatter};
7use core::fmt;
8
9/// Internal code for STCP, such as 600 or 107. In this case, 107 matches to the ZC pubcode.
10#[derive(Deserialize, Serialize, Debug, PartialEq)]
11pub struct Code(pub String);
12
13impl From<&str> for Code {
14    fn from(string: &str) -> Self {
15        Code(string.to_string())
16    }
17}
18
19impl Display for Code {
20    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
21        write!(f, "{}", &self.0)
22    }
23}
24
25/// Code shown to the end users, such as 600 or ZC.
26#[derive(Deserialize, Serialize, Debug, PartialEq)]
27pub struct PubCode(pub String);
28
29/// Represents a STCP line, such as 600, ZC or 3M.
30#[derive(Deserialize, Serialize, Debug, PartialEq)]
31pub struct Line {
32    /// Accessibility level. Unsure what it means or what it is used for.
33    pub accessibility: u8,
34
35    /// Code is the internal code for STCP, such as 600 or 107. In this case, 107 matches to the ZC pubcode.
36    pub code: Code,
37
38    /// Pubcode is the code for the users, such as 600 or ZC.
39    #[serde(rename = "pubcode")]
40    pub pub_code: PubCode,
41
42    /// Description of line, including pubcode and initial and final stops.
43    pub description: String,
44}
45
46type LinesResponse = StcpResponse<Line>;
47
48pub fn fetch_lines() -> Result<Vec<Line>, Error> {
49    let json = fetch::fetch_lines()?;
50
51    let lines = parse::parse_lines(&json)?;
52
53    Ok(lines.records)
54}