-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change presenters to formatters and output to std instead of writing …
…to file
- Loading branch information
Showing
9 changed files
with
197 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use crate::{config::Config, dbapiclient::DbApiClient}; | ||
|
||
use reqwest::header::{HeaderMap, HeaderValue, ACCEPT}; | ||
|
||
pub struct DbApiClientReqwest { | ||
config: Config, | ||
} | ||
|
||
impl DbApiClientReqwest { | ||
pub fn new(config: Config) -> Self { | ||
Self { config } | ||
} | ||
|
||
fn construct_headers(&self) -> HeaderMap { | ||
let mut headers = HeaderMap::new(); | ||
headers.insert( | ||
"DB-Api-Key", | ||
HeaderValue::from_str(self.config.api_key.as_str()).unwrap(), | ||
); | ||
headers.insert( | ||
"DB-Client-Id", | ||
HeaderValue::from_str(self.config.client_id.as_str()).unwrap(), | ||
); | ||
headers.insert(ACCEPT, HeaderValue::from_static("application/xml")); | ||
headers | ||
} | ||
|
||
fn construct_complete_url(&self, endpoint: String) -> String { | ||
format!("{}{}", self.config.url, endpoint) | ||
} | ||
} | ||
|
||
impl DbApiClient for DbApiClientReqwest { | ||
fn get(&self, endpoint: String) -> Result<String, String> { | ||
let client = reqwest::Client::new(); | ||
let res = client | ||
.get(self.construct_complete_url(endpoint)) | ||
.headers(self.construct_headers()) | ||
.send(); | ||
|
||
let body = match res.text() { | ||
Ok(it) => it, | ||
Err(_err) => return Err(_err), | ||
}; | ||
|
||
Ok(body) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
use crate::timetable::Timetable; | ||
|
||
pub trait TimetableFormatter { | ||
fn format(&self, timetable: &Timetable) -> Result<String, String>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
use crate::timetable::{ArrivalDeparture, Timetable}; | ||
use crate::timetableformatter::TimetableFormatter; | ||
use chrono::NaiveDateTime; | ||
|
||
pub struct TimetableFormatterSimpleString(); | ||
|
||
impl TimetableFormatterSimpleString { | ||
#[allow(dead_code)] | ||
pub fn new() -> Self { | ||
Self {} | ||
} | ||
} | ||
|
||
impl TimetableFormatter for TimetableFormatterSimpleString { | ||
fn format(&self, timetable: &Timetable) -> Result<String, String> { | ||
let mut ret: String = "".to_string(); | ||
ret.push_str(&station_name(timetable)); | ||
ret.push_str(&seperator_lines(2)); | ||
ret.push_str(&timetablestop(timetable)); | ||
ret.push_str(&seperator_lines(1)); | ||
|
||
Ok(ret) | ||
} | ||
} | ||
|
||
fn timetablestop(timetable: &Timetable) -> String { | ||
if let Some(s) = &timetable.s { | ||
let mut ret: String = "".to_string(); | ||
for item in s.iter() { | ||
ret.push_str(&print_departure(item)) | ||
} | ||
ret | ||
} else { | ||
"Timetable is empty.\n".to_string() | ||
} | ||
} | ||
|
||
fn print_departure(s: &crate::timetable::TimetableStop) -> String { | ||
let mut ret: String = "".to_string(); | ||
if let Some(dp) = &s.dp { | ||
if let Some(tl) = &s.tl { | ||
ret.push_str(&train_info(tl, dp)); | ||
ret.push_str(&time_info(dp)); | ||
ret.push_str(&seperator_lines(1)); | ||
} | ||
} | ||
ret | ||
} | ||
|
||
fn time_info(dp: &ArrivalDeparture) -> String { | ||
let mut ret: String = "".to_string(); | ||
ret.push_str(&planned_time(dp)); | ||
ret.push_str(&changed_time(dp)); | ||
ret | ||
} | ||
|
||
fn planned_time(dp: &ArrivalDeparture) -> String { | ||
let pt = dp.pt.as_deref().unwrap_or("-"); | ||
if let Ok(dt) = NaiveDateTime::parse_from_str(pt, "%y%m%d%H%M") { | ||
return format!("Planned time: {}\n", dt); | ||
} | ||
"".to_string() | ||
} | ||
|
||
fn changed_time(dp: &crate::timetable::ArrivalDeparture) -> String { | ||
let ct: &str = dp.ct.as_deref().unwrap_or("-"); | ||
match NaiveDateTime::parse_from_str(ct, "%y%m%d%H%M") { | ||
Ok(dt) => format!("Actual time: {}\n", dt), | ||
Err(_) => "Actual time: No delay\n".to_string(), | ||
} | ||
} | ||
|
||
fn train_info(tl: &crate::timetable::Triplabel, dp: &crate::timetable::ArrivalDeparture) -> String { | ||
format!( | ||
"{} {}: {}\n", | ||
tl.c.as_ref().unwrap_or(&"".to_string()), | ||
dp.l.as_ref().unwrap_or(&"".to_string()), | ||
dp.ppth | ||
.as_ref() | ||
.unwrap_or(&"".to_string()) | ||
.split('|') | ||
.last() | ||
.unwrap_or("") | ||
) | ||
} | ||
|
||
fn station_name(timetable: &Timetable) -> String { | ||
format!( | ||
"{}\n", | ||
timetable | ||
.station | ||
.as_ref() | ||
.unwrap_or(&"Station name missing\n".to_string()) | ||
) | ||
} | ||
|
||
fn seperator_lines(count: i16) -> String { | ||
let mut ret: String = "".to_string(); | ||
for _n in 0..count { | ||
ret.push_str("----------------------\n"); | ||
} | ||
ret | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.