From 178ab684fab1aa6ec888af5e51d28c65b7565606 Mon Sep 17 00:00:00 2001 From: Empire Date: Sat, 19 Aug 2023 00:05:19 +0200 Subject: [PATCH] fix parser and code style --- client/bin/src/main.rs | 22 ++++++------ client/bin/src/straba.rs | 50 ++++++++++++++++----------- client/openweathermap/src/forecast.rs | 2 +- 3 files changed, 41 insertions(+), 33 deletions(-) diff --git a/client/bin/src/main.rs b/client/bin/src/main.rs index e24c9c1..8fcf1e4 100644 --- a/client/bin/src/main.rs +++ b/client/bin/src/main.rs @@ -2,20 +2,20 @@ use bit::BitIndex; use chrono_tz::Europe::Berlin; use chrono::{DateTime, NaiveDateTime, Utc, Timelike}; -use openweathermap::{forecast::{Weather, List}}; +use openweathermap::forecast::Weather; use substring::Substring; use tinybmp::Bmp; use core::time; use embedded_graphics::{ - image::{Image}, + image::Image, mono_font::{iso_8859_1::FONT_6X10, MonoTextStyle}, - pixelcolor::{BinaryColor}, + pixelcolor::BinaryColor, prelude::*, - primitives::{PrimitiveStyle}, + primitives::PrimitiveStyle, text::Text, }; -use std::{net::UdpSocket, time::{Instant, Duration}}; +use std::net::UdpSocket; use std::{env, thread}; use openweathermap::forecast::Forecast; @@ -115,7 +115,7 @@ impl DrawTarget for UdpDisplay { } -fn renderWeather(display: &mut UdpDisplay ,data: &Option>){ +fn render_weather(display: &mut UdpDisplay ,data: &Option>){ let text_style = MonoTextStyle::new(&FONT_6X10, BinaryColor::On); match data { @@ -140,7 +140,7 @@ fn renderWeather(display: &mut UdpDisplay ,data: &Option::default(); - if(zoned_time > cur_time){ + if zoned_time > cur_time { println!("Skipping old result {hour}:{minute} @{time_s}"); } @@ -164,7 +164,7 @@ fn renderWeather(display: &mut UdpDisplay ,data: &Option, tinybmp::ParseError> = match short_icon_code { @@ -235,7 +235,7 @@ fn send_package(ipaddress: String, data: &Option>) { // .into_styled(PRIMITIVE_STYLE) // .draw(&mut display) // .unwrap(); - renderWeather(&mut display, data); + render_weather(&mut display, data); @@ -278,7 +278,7 @@ fn main() { let mut last_data = Option::None; // Test Webcrawler for public transportataion - straba::fetchData(); + straba::fetch_data(); loop { let delay = time::Duration::from_millis(10000); diff --git a/client/bin/src/straba.rs b/client/bin/src/straba.rs index 9175226..7759b76 100644 --- a/client/bin/src/straba.rs +++ b/client/bin/src/straba.rs @@ -1,10 +1,10 @@ /* @file straba.rs * @brief fetch next depature of light rail vehicle */ -use std::error::Error; use serde::Deserialize; +use serde_json::Value; -const stationURL:&str = "https://www.rnv-online.de/rest/departure/2494"; +const STATION_URL:&str = "https://www.rnv-online.de/rest/departure/2494"; #[derive(Default, Debug, Clone, PartialEq, Deserialize)] #[serde(rename_all = "camelCase")] @@ -20,9 +20,9 @@ pub struct Station { pub struct Line { pub id: i64, pub text: String, - pub iconOutlined: i64, - pub iconColor: String, - pub iconTextColor: i64, + pub icon_outlined: i64, + pub icon_color: String, + pub icon_text_color: i64, } #[derive(Default, Debug, Clone, PartialEq, Deserialize)] @@ -30,10 +30,10 @@ pub struct Line { pub struct Journey { pub line: Line, pub destination: String, - pub barrierLevel: String, - pub loadsForecastType: String, - pub realtimeDeparture: i64, - pub scheduledDeparture: i64, + pub barrier_level: String, + pub loads_forecast_type: String, + pub realtime_departure: i64, + pub scheduled_departure: i64, pub difference: i64, pub canceled: bool, } @@ -44,20 +44,28 @@ pub struct NextDeparture { } -pub fn fetchData() -> Result { - let result = reqwest::blocking::get(stationURL); +pub fn fetch_data() -> Option { + let result = reqwest::blocking::get(STATION_URL); - let response = match result { - Ok(res) => res, - Err(err) => return reqwest::Error(err), - }; + if result.is_err() { + println!("Could not read station response {:?}", result.err()); + return Option::None; + } + let text = result.unwrap().text(); + if text.is_err() { + println!("Could not convert response {:?}", text.err()); + return Option::None; + } - let body = serde_json::from_str(&text); - let json = match body { - Ok(json) => json, - Err(err) => return reqwest::Error(err), - }; + let body: std::result::Result = serde_json::from_str(&text.unwrap()); + + if body.is_err() { + println!("Could not parse json {:?}", body.err()); + return Option::None; + } + + let json = body.unwrap(); let date = json["id"].to_string(); - Ok(date) + Some(date) } diff --git a/client/openweathermap/src/forecast.rs b/client/openweathermap/src/forecast.rs index 2096157..1b614fd 100644 --- a/client/openweathermap/src/forecast.rs +++ b/client/openweathermap/src/forecast.rs @@ -1,4 +1,4 @@ -use serde::{Deserialize, __private::de}; +use serde::Deserialize; #[derive(Default, Debug, Clone, PartialEq, Deserialize)] #[serde(rename_all = "camelCase")]