Skip to content

Commit

Permalink
Simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
Gekkio committed Mar 23, 2024
1 parent 55569a6 commit 34ba5b2
Show file tree
Hide file tree
Showing 14 changed files with 208 additions and 165 deletions.
13 changes: 13 additions & 0 deletions backend/src/config/cartridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,19 @@ impl IndexMut<PartDesignator> for ChipRoleConfig {
}
}

impl IntoIterator for ChipRoleConfig {
type Item = (PartDesignator, ChipRole);
type IntoIter = Box<dyn Iterator<Item = Self::Item>>;

fn into_iter(self) -> Self::IntoIter {
Box::new(
PartDesignator::ALL
.into_iter()
.filter_map(move |d| self[d].map(|role| (d, role))),
)
}
}

impl<'a> IntoIterator for &'a ChipRoleConfig {
type Item = (PartDesignator, ChipRole);
type IntoIter = Box<dyn Iterator<Item = Self::Item> + 'a>;
Expand Down
2 changes: 1 addition & 1 deletion site/src/gbhwdb.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
*/

@import 'common';
@import 'template/console_page_chip';
@import 'template/listing_chip';
@import 'template/listing_entry_cell';
@import 'template/site_footer';
@import 'template/site_header';
@import 'template/submission_chip';

* {
box-sizing: border-box;
Expand Down
17 changes: 16 additions & 1 deletion site/src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: MIT

use maud::{html, Markup, DOCTYPE};
use maud::{html, Markup, Render, DOCTYPE};

use crate::site::SiteSection;
use crate::template::{site_footer::SiteFooter, site_header::SiteHeader};
Expand All @@ -23,6 +23,7 @@ pub mod markdown;
pub mod markdown_page;
pub mod site_footer;
pub mod site_header;
pub mod submission_chip_table;

pub fn page(title: &str, section: SiteSection, content: Markup) -> String {
html! {
Expand Down Expand Up @@ -55,3 +56,17 @@ pub fn page(title: &str, section: SiteSection, content: Markup) -> String {
}
.into_string()
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Optional<T>(Option<T>);

impl<T> Render for Optional<T>
where
T: Render,
{
fn render_to(&self, buffer: &mut String) {
if let Some(value) = &self.0 {
value.render_to(buffer);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* SPDX-License-Identifier: MIT
*/

.console-page-chip {
.submission-chip {
&--outlier {
background-color: rgba(255, 0, 0, 0.4);
}
Expand Down
48 changes: 12 additions & 36 deletions site/src/template/cartridge_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use gbhwdb_backend::config::cartridge::ChipRoleConfig;
use maud::{html, Markup, Render};
use time::{format_description::FormatItem, macros::format_description};

use crate::legacy::{HasDateCode, LegacyCartridgeSubmission, LegacyChip, LegacyPhoto};
use crate::{
legacy::{HasDateCode, LegacyCartridgeSubmission, LegacyPhoto},
template::submission_chip_table::{submission_chip_table, SubmissionChip},
};

pub struct CartridgePage<'a> {
pub submission: &'a LegacyCartridgeSubmission,
Expand Down Expand Up @@ -38,6 +41,13 @@ impl<'a> Render for CartridgePage<'a> {
let metadata = &self.submission.metadata;
let photos = &self.submission.photos;
let board = &metadata.board;
let chips = ChipRoleConfig::from(board.layout)
.into_iter()
.map(|(designator, role)| SubmissionChip {
designator: designator.as_str(),
label: role.display(),
chip: metadata.board[designator].as_ref(),
});
html! {
article.page-cartridge {
h2 { (metadata.cfg.name) ": " (self.submission.title) " [" (self.submission.contributor) "]" }
Expand Down Expand Up @@ -85,21 +95,7 @@ impl<'a> Render for CartridgePage<'a> {
}
}
h3 { "Chips" }
table {
thead {
tr {
th;
th { "Chip" }
th { "Type" }
th { "Manufacturer" }
th { "Date" }
th { "Label" }
}
}
@for (designator, role) in &ChipRoleConfig::from(board.layout) {
(render_chip(designator.as_str(), role.display(), metadata.board[designator].as_ref()))
}
}
(submission_chip_table(chips))
@if let Some(dump) = &metadata.dump {
div {
h3 { "ROM dump" }
Expand All @@ -117,23 +113,3 @@ impl<'a> Render for CartridgePage<'a> {
}
}
}

fn render_chip(designator: &str, label: &str, chip: Option<&LegacyChip>) -> Markup {
html! {
tr.console-page-chip {
td { (designator) }
td { (label) }
@if let Some(chip) = chip {
td { (chip.kind.as_deref().unwrap_or_default()) }
td { (chip.manufacturer.as_deref().unwrap_or_default()) }
td { (chip.date_code().calendar().unwrap_or_default()) }
td { (chip.label.as_deref().unwrap_or_default()) }
} @else {
td;
td;
td;
td;
}
}
}
}
94 changes: 39 additions & 55 deletions site/src/template/console_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@

use maud::{html, Markup, Render};

use crate::legacy::{
console::{ChipInfo, LegacyConsoleMetadata},
HasDateCode, LegacyChip, LegacyPhoto, LegacyPhotos, LegacySubmission, PhotoInfo, PhotoKind,
use crate::{
legacy::{
console::LegacyConsoleMetadata, HasDateCode, LegacyPhoto, LegacyPhotos, LegacySubmission,
PhotoInfo, PhotoKind,
},
template::{
submission_chip_table::{submission_chip_table, SubmissionChip},
Optional,
},
};

pub struct ConsolePage<'a, M, P> {
pub submission: &'a LegacySubmission<M, P>,
pub extra_sections: Vec<Box<dyn Fn(&Self, &M) -> Markup>>,
pub extra_chips: Vec<Box<dyn Fn(&M) -> (&str, &str, Option<&LegacyChip>)>>,
pub extra_chips: Vec<Box<dyn Fn(&M) -> SubmissionChip>>,
}

impl<'a, M: LegacyConsoleMetadata, P: LegacyPhotos> ConsolePage<'a, M, P> {
Expand All @@ -23,10 +29,6 @@ impl<'a, M: LegacyConsoleMetadata, P: LegacyPhotos> ConsolePage<'a, M, P> {
extra_chips: Vec::new(),
}
}
fn render_chip_info(&self, info: &ChipInfo<M>) -> Markup {
let chip = (info.getter)(&self.submission.metadata);
render_chip(info.designator, info.label, chip)
}
fn render_photo_info(&self, photo: &PhotoInfo<P>) -> Option<Markup> {
(photo.getter)(&self.submission.photos).map(|photo| self.render_photo(photo))
}
Expand All @@ -49,13 +51,39 @@ impl<'a, M: LegacyConsoleMetadata, P: LegacyPhotos> Render for ConsolePage<'a, M
fn render(&self) -> Markup {
let metadata = &self.submission.metadata;
let mainboard = metadata.mainboard();
let chips = M::chips()
.into_iter()
.map(|info| {
let chip = (info.getter)(&self.submission.metadata);
SubmissionChip {
designator: info.designator,
label: info.label,
chip,
}
})
.chain(metadata.lcd_panel().into_iter().flat_map(|panel| {
[
SubmissionChip {
designator: "-",
label: "LCD column driver",
chip: panel.column_driver.as_ref(),
},
SubmissionChip {
designator: "-",
label: "LCD row driver",
chip: panel.row_driver.as_ref(),
},
]
.into_iter()
}))
.chain(self.extra_chips.iter().map(|f| f(metadata)));
html! {
article class=(format!("page-console page-console--{console}", console = M::CONSOLE.id())) {
h2 { (M::CONSOLE.code()) ": " (self.submission.title) " [" (self.submission.contributor) "]" }
div.page-console__photo {
@for info in P::infos() {
@if info.kind == PhotoKind::MainUnit {
(self.render_photo_info(&info).unwrap_or_default())
(Optional(self.render_photo_info(&info)))
}
}
}
Expand Down Expand Up @@ -91,7 +119,7 @@ impl<'a, M: LegacyConsoleMetadata, P: LegacyPhotos> Render for ConsolePage<'a, M
div.page-console__photo {
@for info in P::infos() {
@if info.kind == PhotoKind::MainBoard {
(self.render_photo_info(&info).unwrap_or_default())
(Optional(self.render_photo_info(&info)))
}
}
}
Expand Down Expand Up @@ -135,51 +163,7 @@ impl<'a, M: LegacyConsoleMetadata, P: LegacyPhotos> Render for ConsolePage<'a, M
((section)(self, metadata))
}
h3 { "Chips" }
table {
thead {
tr {
th;
th { "Chip" }
th { "Type" }
th { "Manufacturer" }
th { "Date" }
th { "Label" }
}
}
tbody {
@for info in M::chips() {
(self.render_chip_info(&info))
}
@if let Some(panel) = metadata.lcd_panel() {
(render_chip("-", "LCD column driver", panel.column_driver.as_ref()))
(render_chip("-", "LCD row driver", panel.row_driver.as_ref()))
}
@for f in &self.extra_chips {
@let (designator, label, chip) = f(metadata);
(render_chip(designator, label, chip))
}
}
}
}
}
}
}

fn render_chip(designator: &str, label: &str, chip: Option<&LegacyChip>) -> Markup {
html! {
tr.console-page-chip {
td { (designator) }
td { (label) }
@if let Some(chip) = chip {
td { (chip.kind.as_deref().unwrap_or_default()) }
td { (chip.manufacturer.as_deref().unwrap_or_default()) }
td { (chip.date_code().calendar().unwrap_or_default()) }
td { (chip.label.as_deref().unwrap_or_default()) }
} @else {
td;
td;
td;
td;
(submission_chip_table(chips))
}
}
}
Expand Down
17 changes: 8 additions & 9 deletions site/src/template/dmg_console_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use maud::{html, Markup, Render};

use super::console_page::ConsolePage;
use super::{console_page::ConsolePage, submission_chip_table::SubmissionChip};
use crate::legacy::{console::LegacyDmgMetadata, HasDateCode, LegacyDmgSubmission};

pub struct DmgConsolePage<'a> {
Expand Down Expand Up @@ -94,14 +94,13 @@ impl<'a> Render for DmgConsolePage<'a> {
}
}
})],
extra_chips: vec![Box::new(|m: &LegacyDmgMetadata| {
(
"-",
"LCD bias generator",
m.lcd_board
.as_ref()
.and_then(|board| board.regulator.as_ref()),
)
extra_chips: vec![Box::new(|m: &LegacyDmgMetadata| SubmissionChip {
designator: "-",
label: "LCD bias generator",
chip: m
.lcd_board
.as_ref()
.and_then(|board| board.regulator.as_ref()),
})],
}
.render()
Expand Down
9 changes: 6 additions & 3 deletions site/src/template/dmg_submission_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
use maud::{html, Markup, Render};

use super::console_submission_list::ConsoleSubmissionList;
use crate::legacy::{console::LegacyDmgMetadata, HasDateCode, LegacyDmgSubmission};
use crate::{
legacy::{console::LegacyDmgMetadata, HasDateCode, LegacyDmgSubmission},
template::Optional,
};

pub struct DmgSubmissionList<'a> {
pub submissions: &'a [LegacyDmgSubmission],
Expand All @@ -30,7 +33,7 @@ impl<'a> Render for DmgSubmissionList<'a> {
@if let Some(board) = &m.lcd_board {
div {
div { (board.kind) }
div { (board.date_code().calendar_short().unwrap_or_default()) }
div { (Optional(board.date_code().calendar_short())) }
}
}
}
Expand All @@ -40,7 +43,7 @@ impl<'a> Render for DmgSubmissionList<'a> {
@if let Some(board) = &m.power_board {
div {
div { "Type " (board.kind) }
div { (board.date_code().calendar_short().unwrap_or_default()) }
div { (Optional(board.date_code().calendar_short())) }
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions site/src/template/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ use super::{
listing_chip::ListingChip, listing_entry_cell::ListingEntryCell,
listing_photos_cell::ListingPhotosCell,
};
use crate::legacy::{HasDateCode, LegacyCartridgeSubmission};
use crate::{
legacy::{HasDateCode, LegacyCartridgeSubmission},
template::Optional,
};

#[derive(Clone, Debug)]
pub struct Game<'a> {
Expand Down Expand Up @@ -57,10 +60,10 @@ fn render_submission(submission: &LegacyCartridgeSubmission, chips: &ChipRoleCon
secondary_texts: &[],
submission,
})
td { (metadata.code.as_deref().unwrap_or_default()) }
td { (Optional(metadata.code.as_ref())) }
td {
div { (metadata.board.kind) }
div { (metadata.board.date_code().calendar().unwrap_or_default()) }
div { (Optional(metadata.board.date_code().calendar())) }
}
@for (designator, _) in chips {
(ListingChip {
Expand Down
Loading

0 comments on commit 34ba5b2

Please sign in to comment.