From 2f310f54829cb368f407373eb95cd4512992ba9e Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Sun, 4 Sep 2022 16:36:35 -0400 Subject: Extract card logic into a separate file --- webapp/src/main.rs | 95 ++++++++---------------------------------------------- 1 file changed, 13 insertions(+), 82 deletions(-) (limited to 'webapp/src/main.rs') 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! {
-
+
{ props.rank }
@@ -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) - } -} -- cgit v1.2.3