Skip to main content

dvb/
route_changes.rs

1//! Route changes and disruption information from the VVO WebAPI.
2
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5
6use crate::{DvbResponse, common::Mot, error::Result, time::DvbTime};
7
8const ROUTE_CHANGES_URL: &str = "https://webapi.vvo-online.de/rc";
9const ROUTE_CHANGE_LINES_URL: &str = "https://webapi.vvo-online.de/rc/lines";
10
11#[derive(Serialize, Deserialize, JsonSchema, Clone, Debug)]
12#[serde(rename_all = "PascalCase")]
13pub struct ValidityPeriod {
14    pub begin: Option<DvbTime>,
15    pub end: Option<DvbTime>,
16}
17
18#[derive(Serialize, Deserialize, JsonSchema, Clone, Debug)]
19#[serde(rename_all = "PascalCase")]
20pub struct Change {
21    pub id: Option<String>,
22    pub title: Option<String>,
23    pub description: Option<String>,
24    pub r#type: Option<String>,
25    pub trip_request_include: Option<bool>,
26    pub publish_date: Option<DvbTime>,
27    #[serde(default)]
28    pub line_ids: Vec<String>,
29    #[serde(default)]
30    pub validity_periods: Vec<ValidityPeriod>,
31}
32
33#[derive(Serialize, Deserialize, JsonSchema, Clone, Debug)]
34#[serde(rename_all = "PascalCase")]
35pub struct Banner {
36    pub title: Option<String>,
37    pub description: Option<String>,
38    pub r#type: Option<String>,
39    pub modified_time: Option<DvbTime>,
40    pub trip_request_include: Option<bool>,
41}
42
43#[derive(Serialize, Deserialize, JsonSchema, Clone, Debug)]
44#[serde(rename_all = "PascalCase")]
45pub struct Diva {
46    pub number: Option<String>,
47    pub network: Option<String>,
48}
49
50#[derive(Serialize, Deserialize, JsonSchema, Clone, Debug)]
51#[serde(rename_all = "PascalCase")]
52pub struct Line {
53    pub id: Option<String>,
54    pub name: Option<String>,
55    pub mot: Option<Mot>,
56    pub transportation_company: Option<String>,
57    #[serde(default)]
58    pub divas: Vec<Diva>,
59}
60
61#[derive(Serialize, Deserialize, JsonSchema, Clone, Debug)]
62#[serde(rename_all = "PascalCase")]
63pub struct RouteChanges {
64    #[serde(default)]
65    pub changes: Vec<Change>,
66    #[serde(default)]
67    pub banners: Vec<Banner>,
68    #[serde(default)]
69    pub lines: Vec<Line>,
70}
71
72#[derive(Serialize, Deserialize, JsonSchema, Clone, Debug)]
73#[serde(rename_all = "PascalCase")]
74pub struct RouteChangeLines {
75    #[serde(default)]
76    pub lines: Vec<Line>,
77}
78
79#[derive(Serialize, Clone, Debug, Default)]
80pub struct Params<'a> {
81    /// Include short-term changes.
82    pub shortterm: Option<bool>,
83    /// Provider filter.
84    pub provider: Option<&'a str>,
85    /// Response format.
86    pub format: Option<&'a str>,
87}
88
89#[derive(Serialize, Clone, Debug, Default)]
90pub struct LinesParams<'a> {
91    /// Provider filter.
92    pub provider: Option<&'a str>,
93    /// Response format.
94    pub format: Option<&'a str>,
95}
96
97/// Fetches current route changes and disruptions from the VVO WebAPI.
98///
99/// # Arguments
100/// * `params` - Parameters including optional short-term filter and provider.
101///
102/// # Returns
103/// * `Result<DvbResponse<RouteChanges>>` - The parsed response containing changes, banners, and affected lines.
104///
105/// Endpoint: `https://webapi.vvo-online.de/rc`
106pub async fn route_changes(params: Params<'_>) -> Result<DvbResponse<RouteChanges>> {
107    let response = reqwest::Client::new()
108        .post(ROUTE_CHANGES_URL)
109        .json(&params)
110        .send()
111        .await?
112        .json()
113        .await?;
114
115    Ok(response)
116}
117
118/// Fetches lines affected by route changes from the VVO WebAPI.
119///
120/// # Arguments
121/// * `params` - Parameters including optional provider filter.
122///
123/// # Returns
124/// * `Result<DvbResponse<RouteChangeLines>>` - The parsed response containing affected lines.
125///
126/// Endpoint: `https://webapi.vvo-online.de/rc/lines`
127pub async fn route_change_lines(params: LinesParams<'_>) -> Result<DvbResponse<RouteChangeLines>> {
128    let response = reqwest::Client::new()
129        .post(ROUTE_CHANGE_LINES_URL)
130        .json(&params)
131        .send()
132        .await?
133        .json()
134        .await?;
135
136    Ok(response)
137}