genius_invokation/cards/action/
mod.rs

1macro_rules! impl_from {
2    ($variant:ident : $child:ident => $parent:path) => {
3        impl From<$child> for $parent {
4            fn from(value: $child) -> $parent {
5                <$parent>::$variant(value)
6            }
7        }
8    };
9    ($variant:ident : $child:ident => $parent:path => $higher:path) => {
10        impl_from!($variant : $child => $parent);
11
12        impl From<$child> for $higher {
13            fn from(value: $child) -> $higher {
14                <$higher>::from(<$parent>::from(value))
15            }
16        }
17    };
18    ($variant:ident : $child:ident => $parent:path => $higher:path => $highest:path) => {
19        impl_from!($variant : $child => $parent => $higher);
20
21        impl From<$child> for $highest {
22            fn from(value: $child) -> $highest {
23                <$highest>::from(<$higher>::from(<$parent>::from(value)))
24            }
25        }
26    };
27}
28
29macro_rules! impl_match_method {
30    ($($method:ident -> $return:ty { $var:ident : $match:pat },)+) => {
31        $(
32            fn $method(&self) -> Option<$return> {
33                if let $match = self { Some(*$var) } else { None }
34            }
35        )+
36    };
37}
38
39pub use equip::*;
40mod equip;
41
42pub use event::*;
43mod event;
44
45pub use support::*;
46mod support;
47
48pub use cost::{CardCost, DiceCost};
49mod cost;
50
51pub use card_trait::PlayingCard;
52mod card_trait;
53
54type Price = u16;
55
56#[derive(Debug, Hash, Clone, Copy, PartialEq, Eq)]
57pub enum ActionCard {
58    Equipment(EquipmentCard),
59    Support(SupportCard),
60    Event(EventCard),
61}
62
63impl PlayingCard for ActionCard {
64    fn name(&self) -> &'static str {
65        match self {
66            Self::Equipment(card) => card.name(),
67            Self::Support(card)   => card.name(),
68            Self::Event(card)     => card.name(),
69        }
70    }
71
72    fn shop_price(&self) -> Option<Price> {
73        match self {
74            Self::Equipment(card) => card.shop_price(),
75            Self::Support(card)   => card.shop_price(),
76            Self::Event(card)     => card.shop_price(),
77        }
78    }
79
80    fn cost(&self) -> CardCost {
81        match self {
82            Self::Equipment(card) => card.cost(),
83            Self::Support(card)   => card.cost(),
84            Self::Event(card)     => card.cost(),
85        }
86    }
87
88    impl_match_method!(
89        equipment    -> EquipmentCard { card: Self::Equipment(card) },
90        support      -> SupportCard { card: Self::Support(card) },
91        event        -> EventCard { card: Self::Event(card) },
92        artifact     -> ArtifactCard { card: Self::Equipment(EquipmentCard::Artifact(card)) },
93        talent       -> TalentCard { card: Self::Equipment(EquipmentCard::Talent(card)) },
94        weapon       -> WeaponCard { card: Self::Equipment(EquipmentCard::Weapon(card)) },
95        companion    -> CompanionCard { card: Self::Support(SupportCard::Companion(card)) },
96        location     -> LocationCard { card: Self::Support(SupportCard::Location(card)) },
97        item         -> ItemCard { card: Self::Support(SupportCard::Item(card)) },
98        food         -> FoodCard { card: Self::Event(EventCard::Food(card)) },
99        normal_event -> NormalEventCard { card: Self::Event(EventCard::Normal(card)) },
100        resonance    -> ElementalResonanceCard { card: Self::Event(EventCard::Resonance(card)) },
101    );
102}
103
104use std::cmp::Ordering;
105use super::CardOrd;
106impl CardOrd for ActionCard {
107    fn cmp(&self, other: &Self) -> Ordering {
108        // Equipment < Support < Event
109        match (self, other) {
110            (Self::Equipment(x), Self::Equipment(y)) => x.cmp(y),
111            (Self::Support(x), Self::Support(y))     => x.cmp(y),
112            (Self::Event(x), Self::Event(y))         => x.cmp(y),
113            (Self::Equipment(_), _) => Ordering::Less,
114            (_, Self::Equipment(_)) => Ordering::Greater,
115            (Self::Support(_), _)   => Ordering::Less,
116            (_, Self::Support(_))   => Ordering::Greater,
117        }
118    }
119}
120
121impl_from!(Action: ActionCard => crate::Card);