Remove unused code; disable checks on dead code in views

master
Tait Hoyem 1 year ago
parent fbcfe8fa6c
commit c510b7a309

@ -1,4 +1,4 @@
use std::collections::HashMap; use askama::i18n::FluentValue;
use crate::LOCALES; use crate::LOCALES;
use askama::i18n::{langid, LanguageIdentifier, Locale}; use askama::i18n::{langid, LanguageIdentifier, Locale};
use askama::i18n::fluent_templates::Loader; use askama::i18n::fluent_templates::Loader;
@ -27,6 +27,11 @@ pub enum SupportedLanguage {
#[display(fmt="fr-ca")] #[display(fmt="fr-ca")]
French, French,
} }
impl From<SupportedLanguage> for FluentValue<'_> {
fn from(n: SupportedLanguage) -> Self {
n.to_string().into()
}
}
impl Into<LanguageIdentifier> for SupportedLanguage { impl Into<LanguageIdentifier> for SupportedLanguage {
fn into(self) -> LanguageIdentifier { fn into(self) -> LanguageIdentifier {
match self { match self {
@ -61,49 +66,3 @@ pub struct LangLink {
pub href: String, pub href: String,
pub name: String, pub name: String,
} }
macro_rules! lang_url_match {
($lang:expr, $link_name:expr) => {
Into::<Locale>::into($lang)
.translate(
$link_name,
hashmap_macro::hashmap! {
"lang" => $lang.to_string().into()
},
)
.expect("Unable to find key {key} in locale {self}.")
};
($lang:expr, $link_name:expr, $id:expr) => {
Into::<Locale>::into($lang)
.translate(
$link_name,
hashmap_macro::hashmap! {
"lang" => $lang.to_string().into(),
"id" => $id.into()
},
)
.expect("Unable to find key {key} in locale {self}.")
};
}
// TODO: Genericize this so it can accept any arugments through a impl Iterator<(K, V>) or something similar.
impl LangLink {
pub fn from_lang(lang: SupportedLanguage, link_name: &str) -> Self {
Self {
name: lang.native_name(),
href: lang_url_match!(lang, link_name),
}
}
pub fn from_lang_and_id(lang: SupportedLanguage, id: i32, link_name: &str) -> Self {
Self {
name: lang.native_name(),
href: lang_url_match!(lang, link_name, id),
}
}
pub fn from_lang_and_name(lang: SupportedLanguage, name: &str, link_name: &str) -> Self {
Self {
name: lang.native_name(),
href: lang_url_match!(lang, link_name, name),
}
}
}

@ -3,11 +3,50 @@ mod filters;
mod model; mod model;
mod views; mod views;
mod languages; mod languages;
mod traits;
macro_rules! other_lang_urls {
($lang:expr, $template:ident) => {
$lang.other_langs().map(move |olang| {
LangLink {
name: olang.native_name(),
href: Into::<Locale>::into($lang)
.translate(
$template::URL_KEY,
vec![
("lang", olang.to_string().into())
],
)
.expect("Unable to find key {key} in locale {self}.")
}
}).collect()
};
($lang:expr, $template:ident, $($k:literal => $v:expr),+) => {
$lang.other_langs().map(move |olang| {
LangLink {
name: olang.native_name(),
href: Into::<Locale>::into($lang)
.translate(
$template::URL_KEY,
hashmap_macro::hashmap![
"lang" => olang.to_string().into(),
$($k => $v.into()),+
],
)
.expect("Unable to find key {key} in locale {self}.")
}
}).collect()
};
}
use traits::TemplateUrl;
use static_assertions::assert_impl_all;
#[macro_use]
extern crate ibihf_macros;
use askama::i18n::{langid, Locale}; use askama::i18n::{langid, Locale};
askama::i18n::load!(LOCALES); askama::i18n::load!(LOCALES);
use crate::model::{Division, Game, GamePlayer, League, Player, Shot, Team, Language}; use crate::model::{Division, Game, League, Player, Language};
use views::{GoalDetails, PlayerStats, ShotDetails, TeamStats, IihfStatsI64}; use views::{GoalDetails, PlayerStats, ShotDetails, TeamStats, IihfStatsI64};
use languages::{ use languages::{
SupportedLanguage, SupportedLanguage,
@ -18,31 +57,22 @@ use askama::Template;
use axum::{ use axum::{
extract::{Path, State}, extract::{Path, State},
http::StatusCode, http::StatusCode,
response::{IntoResponse, Json}, response::IntoResponse,
routing::get, routing::get,
Router, Router,
}; };
use axum_macros::debug_handler;
use ormx::Table; use ormx::Table;
use sqlx::{Pool, Postgres}; use sqlx::{Pool, Postgres};
use std::net::SocketAddr; use std::net::SocketAddr;
use std::sync::Arc; use std::sync::Arc;
const VERSION: &str = "0.3.4"; const VERSION: &str = "0.3.5";
#[derive(Template)] #[derive(Template, TemplateUrl)]
#[template(path = "hello.html")]
struct HelloTemplate<'a> {
name: &'a str,
years: i32,
}
#[derive(Template)]
#[template(path = "language_list.html")] #[template(path = "language_list.html")]
struct LanguageListTemplate<'a> { struct LanguageListTemplate<'a> {
#[locale] #[locale]
pub loc: Locale<'a>, pub loc: Locale<'a>,
pub url_name: &'a str,
pub lang_links: Vec<LangLink>, pub lang_links: Vec<LangLink>,
pub lang: SupportedLanguage, pub lang: SupportedLanguage,
pub languages: Vec<Language>, pub languages: Vec<Language>,
@ -53,7 +83,6 @@ struct LanguageListTemplate<'a> {
struct BoxScoreTemplate<'a> { struct BoxScoreTemplate<'a> {
#[locale] #[locale]
locale: Locale<'a>, locale: Locale<'a>,
lang: SupportedLanguage,
goals: Vec<GoalDetails>, goals: Vec<GoalDetails>,
} }
@ -62,7 +91,6 @@ struct BoxScoreTemplate<'a> {
struct IndividualGamePointsTableTemplate<'a> { struct IndividualGamePointsTableTemplate<'a> {
#[locale] #[locale]
locale: Locale<'a>, locale: Locale<'a>,
lang: SupportedLanguage,
players: Vec<PlayerStats>, players: Vec<PlayerStats>,
} }
@ -72,54 +100,54 @@ struct TeamGameStatsTemplate<'a> {
#[locale] #[locale]
locale: Locale<'a>, locale: Locale<'a>,
teams: Vec<TeamStats>, teams: Vec<TeamStats>,
lang: SupportedLanguage,
} }
#[derive(Template)] #[derive(Template, TemplateUrl)]
#[urls(url_key = "league_url", url_key_template = "league_url_tmpl")]
#[template(path = "division_list.html")] #[template(path = "division_list.html")]
struct DivisionListTemplate<'a> { struct DivisionListTemplate<'a> {
#[locale] #[locale]
locale: Locale<'a>, locale: Locale<'a>,
url_name: &'a str,
lang_links: Vec<LangLink>, lang_links: Vec<LangLink>,
league: League, league: League,
divisions: Vec<Division>, divisions: Vec<Division>,
lang: SupportedLanguage, lang: SupportedLanguage,
} }
assert_impl_all!(DivisionListTemplate: TemplateUrl);
#[derive(Template)] #[derive(Template, TemplateUrl)]
#[urls(url_key = "root_url", url_key_template = "root_url_tmpl")]
#[template(path = "league_list.html")] #[template(path = "league_list.html")]
struct LeagueListTemplate<'a> { struct LeagueListTemplate<'a> {
#[locale] #[locale]
locale: Locale<'a>, locale: Locale<'a>,
url_name: &'a str,
lang_links: Vec<LangLink>, lang_links: Vec<LangLink>,
lang: SupportedLanguage, lang: SupportedLanguage,
leagues: Vec<League>, leagues: Vec<League>,
heading: String,
} }
assert_impl_all!(LeagueListTemplate: TemplateUrl);
#[derive(Template)] #[derive(Template)]
#[template(path="partials/iihf_team_stats_table.html")] #[template(path="partials/iihf_team_stats_table.html")]
struct IihfTeamStatsTableTemplate<'a> { struct IihfTeamStatsTableTemplate<'a> {
#[locale] #[locale]
locale: Locale<'a>, locale: Locale<'a>,
lang: SupportedLanguage,
iihf_stats: Vec<IihfStatsI64>, iihf_stats: Vec<IihfStatsI64>,
} }
#[derive(Template)] #[derive(Template, TemplateUrl)]
#[urls(url_key = "division_url", url_key_template = "division_url_tmpl")]
#[template(path = "game_list.html")] #[template(path = "game_list.html")]
struct GameListTemplate<'a> { struct GameListTemplate<'a> {
#[locale] #[locale]
locale: Locale<'a>, locale: Locale<'a>,
url_name: &'a str,
lang_links: Vec<LangLink>, lang_links: Vec<LangLink>,
division: Division, division: Division,
iihf_team_stats_table: IihfTeamStatsTableTemplate<'a>, iihf_team_stats_table: IihfTeamStatsTableTemplate<'a>,
games: Vec<Game>, games: Vec<Game>,
lang: SupportedLanguage, lang: SupportedLanguage,
} }
assert_impl_all!(GameListTemplate: TemplateUrl);
#[derive(Template)] #[derive(Template)]
#[template(path = "partials/play_by_play_table.html")] #[template(path = "partials/play_by_play_table.html")]
@ -127,15 +155,14 @@ struct ShotsTableTemplate<'a> {
#[locale] #[locale]
locale: Locale<'a>, locale: Locale<'a>,
shots: Vec<ShotDetails>, shots: Vec<ShotDetails>,
lang: SupportedLanguage,
} }
#[derive(Template)] #[derive(Template, TemplateUrl)]
#[urls(url_key = "game_url", url_key_template = "game_url_tmpl")]
#[template(path = "game_score_page.html")] #[template(path = "game_score_page.html")]
struct GameScorePageTemplate<'a> { struct GameScorePageTemplate<'a> {
#[locale] #[locale]
locale: Locale<'a>, locale: Locale<'a>,
url_name: &'a str,
lang_links: Vec<LangLink>, lang_links: Vec<LangLink>,
game: Game, game: Game,
division: Division, division: Division,
@ -145,20 +172,22 @@ struct GameScorePageTemplate<'a> {
play_by_play: ShotsTableTemplate<'a>, play_by_play: ShotsTableTemplate<'a>,
lang: SupportedLanguage, lang: SupportedLanguage,
} }
assert_impl_all!(GameScorePageTemplate: TemplateUrl);
#[derive(Template)] #[derive(Template, TemplateUrl)]
#[urls(url_key = "player_url", url_key_template = "player_url_tmpl")]
#[template(path = "player_page.html")] #[template(path = "player_page.html")]
pub struct PlayerPageTemplate<'a> { pub struct PlayerPageTemplate<'a> {
#[locale] #[locale]
locale: Locale<'a>, locale: Locale<'a>,
lang_links: Vec<LangLink>, lang_links: Vec<LangLink>,
url_name: &'a str,
player: Player, player: Player,
league: League, league: League,
league_stats: PlayerStats, league_stats: PlayerStats,
lifetime_stats: PlayerStats, lifetime_stats: PlayerStats,
lang: SupportedLanguage, lang: SupportedLanguage,
} }
assert_impl_all!(PlayerPageTemplate: TemplateUrl);
#[derive(Clone)] #[derive(Clone)]
pub struct ServerState { pub struct ServerState {
@ -174,12 +203,10 @@ async fn main() {
let router = Router::new() let router = Router::new()
.route("/", get(language_list)) .route("/", get(language_list))
.route("/:lang/", get(league_html)) .route("/:lang/", get(league_html))
.route("/:lang/shots/", get(shots_all)) .route(&SupportedLanguage::English.lookup(DivisionListTemplate::URL_KEY), get(divisions_for_league_html))
.route("/:lang/test/", get(test_template)) .route(&SupportedLanguage::English.lookup(GameListTemplate::URL_KEY), get(games_for_division_html))
.route(&SupportedLanguage::English.lookup("league_url"), get(divisions_for_league_html)) .route(&SupportedLanguage::English.lookup(GameScorePageTemplate::URL_KEY), get(score_for_game_html))
.route(&SupportedLanguage::English.lookup("division_url"), get(games_for_division_html)) .route(&SupportedLanguage::French.lookup(GameScorePageTemplate::URL_KEY), get(score_for_game_html))
.route(&SupportedLanguage::English.lookup("game_url"), get(score_for_game_html))
.route(&SupportedLanguage::French.lookup("game_url"), get(score_for_game_html))
.route("/:lang/player/:name/", get(player_from_name)) .route("/:lang/player/:name/", get(player_from_name))
.with_state(state); .with_state(state);
let addr = SocketAddr::from(([127, 0, 0, 1], 8000)); let addr = SocketAddr::from(([127, 0, 0, 1], 8000));
@ -198,10 +225,9 @@ async fn language_list(
.unwrap(); .unwrap();
let lang_list_tmpl = LanguageListTemplate { let lang_list_tmpl = LanguageListTemplate {
loc: Locale::new(langid!("en-ca"), &LOCALES), loc: Locale::new(langid!("en-ca"), &LOCALES),
url_name: "root_url_tmpl",
lang_links: Vec::new(), lang_links: Vec::new(),
languages,
lang: SupportedLanguage::English, lang: SupportedLanguage::English,
languages
}; };
(StatusCode::OK, lang_list_tmpl) (StatusCode::OK, lang_list_tmpl)
} }
@ -226,24 +252,17 @@ async fn player_from_name(
.unwrap(); .unwrap();
let html = PlayerPageTemplate { let html = PlayerPageTemplate {
player, player,
lang_links: lang.other_langs().map(move |olang| LangLink::from_lang_and_name(olang, &name, "player_url_tmpl")).collect(), lang_links: other_lang_urls!(lang, PlayerPageTemplate),
lang,
locale: lang.into(), locale: lang.into(),
url_name: "player_url_tmpl",
league: latest_league, league: latest_league,
league_stats: latest_league_stats, league_stats: latest_league_stats,
lifetime_stats, lifetime_stats,
lang,
}; };
(StatusCode::OK, html) (StatusCode::OK, html)
} }
async fn test_template<'a>() -> HelloTemplate<'a> { /*
HelloTemplate {
name: "Tait",
years: 24,
}
}
macro_rules! get_all { macro_rules! get_all {
($crud_struct:ident, $func_name:ident) => { ($crud_struct:ident, $func_name:ident) => {
#[debug_handler] #[debug_handler]
@ -267,23 +286,17 @@ macro_rules! get_by_id {
} }
}; };
} }
*/
async fn league_html( async fn league_html(
State(server_config): State<ServerState>, State(server_config): State<ServerState>,
Path(lang): Path<SupportedLanguage>, Path(lang): Path<SupportedLanguage>,
) -> impl IntoResponse { ) -> impl IntoResponse {
let leagues = League::all(&*server_config.db_pool).await.unwrap(); let leagues = League::all(&*server_config.db_pool).await.unwrap();
let heading = match lang {
SupportedLanguage::English => "IBIHF Leagues",
SupportedLanguage::French => "League de FIDHS",
}
.to_string();
let leagues_template = LeagueListTemplate { let leagues_template = LeagueListTemplate {
lang_links: lang.other_langs().map(move |olang| LangLink::from_lang(olang, "root_url_tmpl")).collect(), lang_links: other_lang_urls!(lang, PlayerPageTemplate),
url_name: "root_url_tmpl",
locale: lang.into(), locale: lang.into(),
leagues, leagues,
heading,
lang, lang,
}; };
(StatusCode::OK, leagues_template) (StatusCode::OK, leagues_template)
@ -301,8 +314,8 @@ async fn divisions_for_league_html(
.unwrap(); .unwrap();
let html = DivisionListTemplate { let html = DivisionListTemplate {
locale: lang.into(), locale: lang.into(),
lang_links: lang.other_langs().map(move |olang| LangLink::from_lang_and_id(olang, league_id, "league_url_tmpl")).collect(), // TODO: add league_id here
url_name: "league_url_tmpl", lang_links: other_lang_urls!(lang, PlayerPageTemplate, "id" => league.id),
league, league,
divisions, divisions,
lang, lang,
@ -325,13 +338,11 @@ async fn games_for_division_html(
.unwrap(); .unwrap();
let games_template = GameListTemplate { let games_template = GameListTemplate {
locale: lang.into(), locale: lang.into(),
lang_links: lang.other_langs().map(move |olang| LangLink::from_lang_and_id(olang, division_id, "division_url_tmpl")).collect(), lang_links: other_lang_urls!(lang, GameListTemplate, "id" => division_id),
url_name: "division_url_tmpl",
division, division,
iihf_team_stats_table: IihfTeamStatsTableTemplate { iihf_team_stats_table: IihfTeamStatsTableTemplate {
locale: lang.into(), locale: lang.into(),
iihf_stats, iihf_stats,
lang,
}, },
games, games,
lang, lang,
@ -354,33 +365,30 @@ async fn score_for_game_html(
.await .await
.unwrap(); .unwrap();
let score = game.score(&server_config.db_pool).await.unwrap(); let score = game.score(&server_config.db_pool).await.unwrap();
let score_html = TeamGameStatsTemplate { locale: lang.into(), teams: score, lang }; let score_html = TeamGameStatsTemplate { locale: lang.into(), teams: score };
let goal_details = game.box_score(&server_config.db_pool) let goal_details = game.box_score(&server_config.db_pool)
.await .await
.unwrap(); .unwrap();
let goal_details_html = IndividualGamePointsTableTemplate { let goal_details_html = IndividualGamePointsTableTemplate {
locale: lang.into(), locale: lang.into(),
players: goal_details, players: goal_details,
lang,
}; };
let box_score = game.goals(&server_config.db_pool).await.unwrap(); let box_score = game.goals(&server_config.db_pool).await.unwrap();
let box_score_html = BoxScoreTemplate { let box_score_html = BoxScoreTemplate {
locale: lang.into(), locale: lang.into(),
goals: box_score, goals: box_score,
lang,
}; };
let pbp_html = ShotsTableTemplate { locale: lang.into(), shots: pbp, lang }; let pbp_html = ShotsTableTemplate { locale: lang.into(), shots: pbp };
let game_template = GameScorePageTemplate { let game_template = GameScorePageTemplate {
locale: lang.into(), locale: lang.into(),
lang_links: lang.other_langs().map(move |olang| LangLink::from_lang_and_id(olang, game_id, "game_url_tmpl")).collect(), lang_links: other_lang_urls!(lang, GameScorePageTemplate, "id" => game_id),
url_name: "game_url_tmpl",
division, division,
game, game,
lang,
box_score: box_score_html, box_score: box_score_html,
team_stats: score_html, team_stats: score_html,
individual_stats: goal_details_html, individual_stats: goal_details_html,
play_by_play: pbp_html, play_by_play: pbp_html,
lang,
}; };
(StatusCode::OK, game_template) (StatusCode::OK, game_template)
} }
@ -397,7 +405,6 @@ macro_rules! insert {
} }
} }
} }
*/
macro_rules! impl_all_query_types { macro_rules! impl_all_query_types {
($ty:ident, $func_all:ident, $func_by_id:ident) => { ($ty:ident, $func_all:ident, $func_by_id:ident) => {
@ -412,3 +419,4 @@ impl_all_query_types!(Team, team_all, team_id);
impl_all_query_types!(Shot, shots_all, shots_id); impl_all_query_types!(Shot, shots_all, shots_id);
impl_all_query_types!(Division, division_all, division_id); impl_all_query_types!(Division, division_all, division_id);
impl_all_query_types!(League, league_all, league_id); impl_all_query_types!(League, league_all, league_id);
*/

@ -0,0 +1,4 @@
pub trait TemplateUrl {
const URL_KEY: &'static str;
const URL_KEY_TEMPLATE: &'static str;
}

@ -1,3 +1,5 @@
#![allow(dead_code)]
use crate::model::{Division, Game, League, Player}; use crate::model::{Division, Game, League, Player};
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
use sqlx::FromRow; use sqlx::FromRow;
@ -32,6 +34,21 @@ pub struct IihfStatsI64 {
pub ties: i64, pub ties: i64,
pub points: i64, pub points: i64,
} }
impl Into<IihfStatsI64> for IihfStats {
fn into(self) -> IihfStatsI64 {
IihfStatsI64 {
team_name: self.team_name.clone(),
team_id: self.team_id,
reg_wins: self.reg_wins.into(),
reg_losses: self.reg_losses.into(),
ot_wins: self.ot_wins.into(),
ot_losses: self.ot_losses.into(),
ties: self.ties.into(),
points: self.points.into(),
}
}
}
#[derive(FromRow, Deserialize, Serialize, Debug)] #[derive(FromRow, Deserialize, Serialize, Debug)]
pub struct IihfPoints { pub struct IihfPoints {
pub team_name: String, pub team_name: String,

Loading…
Cancel
Save