diff options
author | Kjetil Orbekk <kj@orbekk.com> | 2022-09-04 16:36:35 -0400 |
---|---|---|
committer | Kjetil Orbekk <kj@orbekk.com> | 2022-09-04 16:38:42 -0400 |
commit | 2f310f54829cb368f407373eb95cd4512992ba9e (patch) | |
tree | 8f17d97cf84b30fce1d5703de4b7f6d7ba54acfa /webapp/src/main.rs | |
parent | ca37b1757a92e7b85fc434474642d1506dcd86a3 (diff) |
Extract card logic into a separate file
Diffstat (limited to 'webapp/src/main.rs')
-rw-r--r-- | webapp/src/main.rs | 95 |
1 files changed, 13 insertions, 82 deletions
diff --git a/webapp/src/main.rs b/webapp/src/main.rs index 2d01713..ed84935 100644 --- a/webapp/src/main.rs +++ b/webapp/src/main.rs @@ -1,7 +1,8 @@ +use crate::card::{Rank, Suit}; #[allow(unused_imports)] use log::{debug, error, info, warn}; -use std::fmt; use yew::prelude::*; +pub mod card; fn main() { std::panic::set_hook(Box::new(console_error_panic_hook::hook)); @@ -63,6 +64,15 @@ pub fn hand(props: &HandProps) -> Html { } } +pub fn suit_css_class(suit: Suit) -> &'static str { + match suit { + Suit::Club => "suit-club", + Suit::Diamond => "suit-diamond", + Suit::Heart => "suit-heart", + Suit::Spade => "suit-spade", + } +} + #[derive(Clone, Default, PartialEq, Properties)] pub struct HandProps { #[prop_or_default] @@ -70,10 +80,10 @@ pub struct HandProps { } #[function_component(Card)] -pub fn card(props: &CardProps) -> Html { +pub fn wcard(props: &CardProps) -> Html { html! { <div class="card"> - <div class={ props.suit.css_class() }> + <div class={ suit_css_class(props.suit) }> { props.rank } </div> </div> @@ -85,82 +95,3 @@ pub struct CardProps { suit: Suit, rank: Rank, } - -#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Copy)] -pub enum Suit { - Club, - Diamond, - Heart, - Spade, -} - -#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Copy)] -pub enum Rank { - Two = 2, - Three, - Four, - Five, - Six, - Seven, - Eight, - Nine, - Ten, - Jack, - Queen, - King, - Ace, -} - -impl Suit { - pub fn css_class(&self) -> &'static str { - match self { - Suit::Club => "suit-club", - Suit::Diamond => "suit-diamond", - Suit::Heart => "suit-heart", - Suit::Spade => "suit-spade", - } - } -} - -impl fmt::Display for Suit { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> { - f.write_str(match self { - Suit::Club => "♣", - Suit::Diamond => "♢", - Suit::Heart => "♡", - Suit::Spade => "♠", - }) - } -} - -impl fmt::Debug for Suit { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> { - write!(f, "{}", self) - } -} - -impl fmt::Display for Rank { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> { - f.write_str(match self { - Rank::Ace => "A", - Rank::King => "K", - Rank::Queen => "Q", - Rank::Jack => "J", - Rank::Ten => "10", - Rank::Nine => "9", - Rank::Eight => "8", - Rank::Seven => "7", - Rank::Six => "6", - Rank::Five => "5", - Rank::Four => "4", - Rank::Three => "3", - Rank::Two => "2", - }) - } -} - -impl fmt::Debug for Rank { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> { - write!(f, "{}", self) - } -} |