Minimal JSON structure can be parsed

This commit is contained in:
Ollo 2023-09-13 22:11:08 +02:00
parent 3b0f7def77
commit 4fa63917ed

View File

@ -27,35 +27,43 @@ pub struct GraphQL {
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct GraphQLResponse { pub struct GraphQLResponse {
pub name: String, pub name: String,
pub journeys: Vec<JourneyElements>, pub journeys: JourneysElement,
} }
#[derive(Default, Debug, Clone, PartialEq, Deserialize)] #[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct JourneyElements { pub struct JourneysElement {
#[serde(untagged)] pub elements: Vec<Journey>,
pub elements: Vec<JourneyElements>,
} }
#[derive(Default, Debug, Clone, PartialEq, Deserialize)] #[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct Line { pub struct Line {
pub id: String, pub lineGroup: LineGroup,
}
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LineGroup {
pub label: String,
} }
#[derive(Default, Debug, Clone, PartialEq, Deserialize)] #[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct Journey { pub struct Journey {
pub line: Line, pub line: Line,
pub destination: String,
pub barrier_level: String,
pub loads_forecast_type: String,
pub realtime_departure: Option<i64>,
pub scheduled_departure: i64,
pub difference: Option<i64>,
pub canceled: bool, pub canceled: bool,
pub stops: Vec<StopsElement>,
} }
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct StopsElement {
pub destinationLabel: String,
//FIXME is in sub-structure: pub realtime_departure: Option<i64>,
//FIXME is in sub-structure: pub scheduled_departure: i64,
//TODO pub difference: Option<i64>,
}
// Return value // Return value
pub struct NextDeparture { pub struct NextDeparture {
pub rheinau: i64, pub rheinau: i64,
@ -75,9 +83,10 @@ pub fn fetch_data() -> Option<&'static str> {
println!("Could not convert response {:?}", text.err()); println!("Could not convert response {:?}", text.err());
return Option::None; return Option::None;
} }
let rawText = &text.unwrap();
println!("{}", &rawText);
let body: std::result::Result<Station, serde_json::Error> = serde_json::from_str(&rawText);
let body: std::result::Result<Station, serde_json::Error> = serde_json::from_str(&text.unwrap());
if body.is_err() { if body.is_err() {
println!("Could not parse json {:?}", body.err()); println!("Could not parse json {:?}", body.err());
@ -86,14 +95,15 @@ pub fn fetch_data() -> Option<&'static str> {
let cur_time = DateTime::<Utc>::default(); let cur_time = DateTime::<Utc>::default();
// parse JSON result.. serach of both directions // parse JSON result.. search of both directions
let json = body.unwrap(); let json = body.unwrap();
for journey in (json.graphQL.response.journeys) { for el in (json.graphQL.response.journeys.elements) {
let destination = (journey.destination).to_string(); //TODO:
let departure = (journey.realtime_departure); /*let destination = (el.destination).to_string();
let difference = (journey.difference); let departure = el.realtime_departure;
let difference = el.difference;
if (departure.is_some()) { if departure.is_some() {
// get current time // get current time
let time_s = departure.unwrap(); let time_s = departure.unwrap();
let local_time = NaiveDateTime::from_timestamp_millis(time_s*1000).unwrap(); let local_time = NaiveDateTime::from_timestamp_millis(time_s*1000).unwrap();
@ -102,11 +112,11 @@ pub fn fetch_data() -> Option<&'static str> {
let hour = europe_time.hour(); let hour = europe_time.hour();
let minute = europe_time.minute(); let minute = europe_time.minute();
if (zoned_time > cur_time) { if zoned_time > cur_time {
println!("------------- Future starts here ----------------"); println!("------------- Future starts here ----------------");
} }
println!("{0} {1}:{2}", destination, hour, minute); println!("{0} {1}:{2}", destination, hour, minute);
} }*/
} }
Some("") Some("")