Format schedule.rs seems to have forgotten for previous commit

This commit is contained in:
Tom 2022-08-18 20:51:37 +02:00
parent 022a1d8991
commit d54b600391

View file

@ -1,7 +1,7 @@
use crate::{commands::utils, Context, Error}; use crate::{commands::utils, Context, Error};
use cached::proc_macro::cached; use cached::proc_macro::cached;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use log::{warn}; use log::warn;
use reqwest::header::AUTHORIZATION; use reqwest::header::AUTHORIZATION;
use serde::Deserialize; use serde::Deserialize;
@ -30,11 +30,19 @@ impl MSEvent {
fn get_value(&self, high_tier: bool) -> String { fn get_value(&self, high_tier: bool) -> String {
let link = if high_tier { let link = if high_tier {
format!("[{id}](https://morningstreams.com/hightier/f1/session/{id})\n", id=self.id) format!(
"[{id}](https://morningstreams.com/hightier/f1/session/{id})\n",
id = self.id
)
} else { } else {
"".to_string() "".to_string()
}; };
format!("{link}Start: <t:{start}:R>\nEnd: <t:{end}:R>", link=link, start=self.metadata.attributes.start.timestamp(), end=self.metadata.attributes.end.timestamp()) format!(
"{link}Start: <t:{start}:R>\nEnd: <t:{end}:R>",
link = link,
start = self.metadata.attributes.start.timestamp(),
end = self.metadata.attributes.end.timestamp()
)
} }
} }
@ -42,48 +50,41 @@ impl MSEvent {
struct MSMetadata { struct MSMetadata {
// id: String, // id: String,
// system: System, // system: System,
#[serde(rename="emfAttributes")] #[serde(rename = "emfAttributes")]
attributes: EmfAttributes, attributes: EmfAttributes,
#[serde(rename="titleBrief")] #[serde(rename = "titleBrief")]
brief: String, brief: String,
// #[serde(rename="Series")] // #[serde(rename="Series")]
// series: String, // series: String,
} }
#[derive(Deserialize, Clone)] #[derive(Deserialize, Clone)]
struct EmfAttributes { struct EmfAttributes {
#[serde(with = "ms_date")] #[serde(with = "ms_date")]
#[serde(rename="sessionStartDate")] #[serde(rename = "sessionStartDate")]
start: DateTime<Utc>, start: DateTime<Utc>,
#[serde(with = "ms_date")] #[serde(with = "ms_date")]
#[serde(rename="sessionEndDate")] #[serde(rename = "sessionEndDate")]
end: DateTime<Utc>, end: DateTime<Utc>,
#[serde(rename="Series")] #[serde(rename = "Series")]
series: String, series: String,
} }
mod ms_date { mod ms_date {
use chrono::{DateTime, Utc, NaiveDateTime}; use chrono::{DateTime, NaiveDateTime, Utc};
use serde::{self, Deserialize, Deserializer}; use serde::{self, Deserialize, Deserializer};
pub fn deserialize<'de, D>(deserializer: D) -> Result<DateTime<Utc>, D::Error> pub fn deserialize<'de, D>(deserializer: D) -> Result<DateTime<Utc>, D::Error>
where where
D: Deserializer<'de>, D: Deserializer<'de>,
{ {
let n = i64::deserialize(deserializer)?/1000; let n = i64::deserialize(deserializer)? / 1000;
// let s = String::deserialize(deserializer)?; // let s = String::deserialize(deserializer)?;
Ok(DateTime::from_utc(NaiveDateTime::from_timestamp(n, 0), Utc)) Ok(DateTime::from_utc(NaiveDateTime::from_timestamp(n, 0), Utc))
} }
} }
#[cached(time = 3600)] #[cached(time = 3600)]
async fn get_schedule() -> Option<MSReq> { async fn get_schedule() -> Option<MSReq> {
let token = super::super::SETTINGS let token = super::super::SETTINGS
@ -136,24 +137,26 @@ async fn get_schedule() -> Option<MSReq> {
} }
#[poise::command(slash_command)] #[poise::command(slash_command)]
pub async fn schedule( pub async fn schedule(ctx: Context<'_>) -> Result<(), Error> {
ctx: Context<'_>,
) -> Result<(), Error> {
let events: Option<MSReq> = get_schedule().await; let events: Option<MSReq> = get_schedule().await;
let ht: bool = utils::high_tier(ctx).await; let ht: bool = utils::high_tier(ctx).await;
match events { match events {
None => {ctx.say("Error on fetching events :(").await?;}, None => {
ctx.say("Error on fetching events :(").await?;
}
Some(evs) => { Some(evs) => {
ctx.send(|b| {b.embed(|e| { ctx.send(|b| {
e.title(format!("F1 schedule: {}", evs.event_title)); b.embed(|e| {
for event in evs.containers { e.title(format!("F1 schedule: {}", evs.event_title));
e.field(event.get_title(), event.get_value(ht), true); for event in evs.containers {
}; e.field(event.get_title(), event.get_value(ht), true);
e }
})}).await?; e
})
})
.await?;
} }
}; };
Ok(()) Ok(())
}
}