Skip to main content

card_stack/
lib.rs

1pub mod actions;
2pub mod priority;
3pub mod requirements;
4
5pub struct Stack<State, IncitingAction: crate::actions::IncitingActionInfo<State>> {
6    inciting_action: IncitingAction,
7    stack: Vec<IncitingAction::Stackable>,
8}
9
10impl<State, IncitingAction: crate::actions::IncitingActionInfo<State> + std::fmt::Debug>
11    std::fmt::Debug for Stack<State, IncitingAction>
12where
13    IncitingAction::Stackable: std::fmt::Debug,
14{
15    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16        f.debug_struct("Stack")
17            .field("inciting_action", &self.inciting_action)
18            .field("stack", &self.stack)
19            .finish()
20    }
21}
22
23impl<State, IncitingAction: crate::actions::IncitingActionInfo<State> + Clone> Clone
24    for Stack<State, IncitingAction>
25where
26    IncitingAction::Stackable: Clone,
27{
28    fn clone(&self) -> Self {
29        Stack {
30            inciting_action: self.inciting_action.clone(),
31            stack: self.stack.clone(),
32        }
33    }
34}
35
36impl<State, IncitingAction: crate::actions::IncitingActionInfo<State>>
37    Stack<State, IncitingAction>
38{
39    pub(crate) fn new(inciting_action: IncitingAction) -> Self {
40        Stack {
41            inciting_action,
42            stack: Vec::new(),
43        }
44    }
45    #[cfg(feature = "internals")]
46    pub fn from_stack(
47        inciting_action: IncitingAction,
48        stack: Vec<IncitingAction::Stackable>,
49    ) -> Self {
50        Stack {
51            inciting_action,
52            stack,
53        }
54    }
55    #[cfg(feature = "internals")]
56    pub fn take_contents(self) -> (IncitingAction, Vec<IncitingAction::Stackable>) {
57        (self.inciting_action, self.stack)
58    }
59    pub fn inciting_action(&self) -> &IncitingAction {
60        &self.inciting_action
61    }
62    pub fn full_stack(&self) -> &[IncitingAction::Stackable] {
63        &self.stack
64    }
65    pub fn take_inciting_action(self) -> IncitingAction {
66        self.inciting_action
67    }
68    pub fn stack(&mut self, stack: IncitingAction::Stackable) {
69        self.stack.push(stack)
70    }
71    pub fn pop(&mut self) -> Option<IncitingAction::Stackable> {
72        self.stack.pop()
73    }
74}
75
76impl<OldState, OldIncitingAction: crate::actions::IncitingActionInfo<OldState>>
77    Stack<OldState, OldIncitingAction>
78{
79    pub fn into_state<NewState, NewIncitingAction: crate::actions::IncitingActionInfo<NewState>>(
80        self,
81    ) -> Stack<NewState, NewIncitingAction>
82    where
83        OldIncitingAction: Into<NewIncitingAction>,
84        <OldIncitingAction as crate::actions::IncitingActionInfo<OldState>>::Stackable:
85            Into<<NewIncitingAction as crate::actions::IncitingActionInfo<NewState>>::Stackable>,
86    {
87        let inciting_action = self.inciting_action.into();
88        let stack = self
89            .stack
90            .into_iter()
91            .map(|stackable| stackable.into())
92            .collect();
93        Stack {
94            stack,
95            inciting_action,
96        }
97    }
98}
99
100/// Marker trait which signifies a empty input.
101/// Used to differentiate empty and non-empty inputs
102/// for generic implementations using generic constraints.
103pub trait EmptyInput {
104    fn empty() -> Self;
105}
106impl EmptyInput for () {
107    fn empty() -> Self {}
108}
109/// Marker trait which signifies a non-empty input.
110/// Used to differentiate empty and non-empty inputs
111/// for generic implementations using generic constraints.
112pub trait NonEmptyInput {}