Errors are passed through "properly"
This commit is contained in:
@@ -114,22 +114,22 @@ pub enum StackingError {
|
|||||||
#[error("Trying to stack the same coloured suit")]
|
#[error("Trying to stack the same coloured suit")]
|
||||||
SameColour,
|
SameColour,
|
||||||
#[error("{0} is not \"next\" to {1}")]
|
#[error("{0} is not \"next\" to {1}")]
|
||||||
NotAdjacent(Card, Card),
|
NotAdjacent(String, String),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Card {
|
impl Card {
|
||||||
pub fn can_be_placed_on_top(&self, top: Card) -> bool {
|
pub fn can_be_placed_on_top(&self, top: Card) -> Result<(), StackingError> {
|
||||||
// Can't be the same Colour
|
// Can't be the same Colour
|
||||||
if self.suit.colour() != top.suit.colour() {
|
if self.suit.colour() != top.suit.colour() {
|
||||||
return false
|
return Err(StackingError::SameColour);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Needs to be adjascent
|
// Needs to be adjascent
|
||||||
if self.value == Value::King || self.value.indexed_values() != top.value.indexed_values() + 1 {
|
if self.value == Value::King || self.value.indexed_values() != top.value.indexed_values() + 1 {
|
||||||
return false
|
return Err(StackingError::NotAdjacent(self.to_string(), top.to_string()));
|
||||||
}
|
}
|
||||||
|
|
||||||
true
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user