Skip to content

Commit

Permalink
Set Clippy to Pedantic (#27)
Browse files Browse the repository at this point in the history
* added clippy.toml

* Fixed clippy pedantic lints

* fmt

* bumped to v0.4.5
  • Loading branch information
folkengine authored Dec 18, 2021
1 parent fa7275a commit 546c7b3
Show file tree
Hide file tree
Showing 13 changed files with 177 additions and 73 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "cardpack"
description = "Generic Deck of Cards"
version = "0.4.4"
version = "0.4.5"
authors = ["folkengine <gaoler@electronicpanopticon.com>"]
repository = "https://github.com/ContractBridge/cardpack.rs.git"
homepage = "https://github.com/ContractBridge/cardpack.rs"
Expand Down
3 changes: 3 additions & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
msrv = "1.56.0"

cognitive-complexity-threshold = 30
19 changes: 12 additions & 7 deletions src/cards/card.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use colored::*;
use colored::Colorize;
use std::fmt;
use unic_langid::LanguageIdentifier;

use crate::cards::rank::*;
use crate::cards::suit::*;
use crate::cards::rank::Rank;
use crate::cards::suit::Suit;
use crate::Named;

pub const BLANK: &str = "blank";
Expand All @@ -25,6 +25,7 @@ pub struct Card {
impl Card {
/// Instantiates a new Card with the default weight as defined in the fluent
/// templates.
#[must_use]
pub fn new(rank: &'static str, suit: &'static str) -> Card {
let suit = Suit::new(suit);
let rank = Rank::new(rank);
Expand All @@ -40,44 +41,47 @@ impl Card {

/// Instantiates a Card with the weight determined by the passed in Rank and
/// Suit.
#[must_use]
pub fn new_from_structs(rank: Rank, suit: Suit) -> Card {
let weight = Card::determine_weight(&suit, &rank);
let index = Card::determine_index(&suit, &rank);
Card {
weight,
index,
rank,
suit,
rank,
}
}

/// Returns a Symbol String for the Card.
#[must_use]
pub fn symbol(&self, lid: &LanguageIdentifier) -> String {
let rank = self.rank.index(lid);
let suit = self.suit.symbol();
format!("{}{}", rank, suit)
}

/// Returns a Symbol String for the Card in the traditional colors for the Suits.
#[must_use]
pub fn symbol_colorized(&self, lid: &LanguageIdentifier) -> String {
let rank = self.rank.index(lid);
let suit = self.suit.symbol();
match self.suit.name() {
"hearts" => format!("{}{}", rank, suit).red().to_string(),
"diamonds" => format!("{}{}", rank, suit).red().to_string(),
"hearts" | "herz" | "diamonds" => format!("{}{}", rank, suit).red().to_string(),
"laub" => format!("{}{}", rank, suit).green().to_string(),
"herz" => format!("{}{}", rank, suit).red().to_string(),
"schellen" => format!("{}{}", rank, suit).yellow().to_string(),
_ => format!("{}{}", rank, suit),
}
}

#[must_use]
pub fn blank_card() -> Card {
Card::new(BLANK, BLANK)
}

/// A valid Card is one where the Rank and Suit are not blank.
/// Cards are blank when an invalid Rank or Suit are passed in.
#[must_use]
pub fn is_valid(&self) -> bool {
!self.rank.is_blank() && !self.suit.is_blank()
}
Expand Down Expand Up @@ -134,6 +138,7 @@ impl Named for Card {
mod card_tests {
use super::*;
use crate::fluent::named::{GERMAN, US_ENGLISH};
use crate::{ACE, BLANK_RANK, BLANK_SUIT, CLUBS, HEARTS, JACK, QUEEN, SPADES};
use std::cell::Cell;

// region impl tests
Expand Down
43 changes: 26 additions & 17 deletions src/cards/decks/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ use std::fmt;
use crate::cards::card::Card;
use crate::cards::pack::Pack;
use crate::cards::pile::Pile;
use crate::cards::suit::*;
use crate::cards::suit::{Suit, CLUBS, DIAMONDS, HEARTS, SPADES};
use std::collections::HashMap;
use term_table::row::Row;
use term_table::table_cell::{Alignment, TableCell};
use term_table::{Table, TableStyle};

#[derive(Clone, Debug, PartialEq)]
#[allow(clippy::module_name_repetitions)]
pub enum BridgeDirection {
N,
E,
Expand All @@ -19,16 +20,13 @@ pub enum BridgeDirection {
}

impl BridgeDirection {
#[must_use]
pub fn to(c: char) -> BridgeDirection {
match c {
'S' => BridgeDirection::S,
's' => BridgeDirection::S,
'N' => BridgeDirection::N,
'n' => BridgeDirection::N,
'E' => BridgeDirection::E,
'e' => BridgeDirection::E,
'W' => BridgeDirection::W,
'w' => BridgeDirection::W,
'S' | 's' => BridgeDirection::S,
'N' | 'n' => BridgeDirection::N,
'E' | 'e' => BridgeDirection::E,
'W' | 'w' => BridgeDirection::W,
_ => BridgeDirection::Unknown,
}
}
Expand All @@ -44,9 +42,10 @@ impl BridgeDirection {
}
}

/// BridgeBoard is a French Deck Pack that sorts and validates the hands dealt as a part
/// `BridgeBoard` is a French Deck Pack that sorts and validates the hands dealt as a part
/// of a Bridge hand.
#[derive(Clone, Debug, Hash, PartialEq)]
#[allow(clippy::module_name_repetitions)]
pub struct BridgeBoard {
pack: Pack,
pub south: Pile,
Expand All @@ -57,7 +56,12 @@ pub struct BridgeBoard {

impl BridgeBoard {
/// Parses a Portable Bridge Notation deal string and converts it into a
/// BridgeBoard struct.
/// `BridgeBoard` struct.
///
/// # Panics
///
/// Will panic if an invalid PBN deal string is passed in.
#[must_use]
pub fn from_pbn_deal(deal: &str) -> BridgeBoard {
let (direction, pbn) = BridgeBoard::split_on_direction(deal);

Expand Down Expand Up @@ -88,6 +92,8 @@ impl BridgeBoard {
}
}

#[allow(clippy::missing_panics_doc)]
#[must_use]
pub fn deal() -> BridgeBoard {
let mut board = BridgeBoard::default();
let mut cards = board.pack.cards().shuffle();
Expand All @@ -114,16 +120,16 @@ impl BridgeBoard {
println!(
"{}",
BridgeBoard::compass(
BridgeBoard::compass_cell("NORTH", north),
BridgeBoard::compass_cell("WEST", west),
BridgeBoard::compass_cell("EAST", east),
BridgeBoard::compass_cell("SOUTH", south),
BridgeBoard::compass_cell("NORTH", north.as_str()),
BridgeBoard::compass_cell("WEST", west.as_str()),
BridgeBoard::compass_cell("EAST", east.as_str()),
BridgeBoard::compass_cell("SOUTH", south.as_str()),
)
);
}

fn compass_cell(direction: &str, index: String) -> String {
let contents = " ".to_owned() + direction + "\n" + index.as_str();
fn compass_cell(direction: &str, index: &str) -> String {
let contents = " ".to_owned() + direction + "\n" + index;

let mut table = Table::new();
table.has_top_boarder = false;
Expand Down Expand Up @@ -161,10 +167,12 @@ impl BridgeBoard {
table.render()
}

#[must_use]
pub fn get_pack(&self) -> &Pack {
&self.pack
}

#[must_use]
pub fn is_valid(&self) -> bool {
let piles = &[
self.south.clone(),
Expand All @@ -177,6 +185,7 @@ impl BridgeBoard {
}

/// Returns a Portable Bridge Notation deal string from a Bridge Board.
#[must_use]
pub fn to_pbn_deal(&self) -> String {
let south = BridgeBoard::hand_to_pbn_deal_segment(&self.south);
let west = BridgeBoard::hand_to_pbn_deal_segment(&self.west);
Expand Down
17 changes: 15 additions & 2 deletions src/cards/decks/standard52.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::fmt;

use crate::cards::decks::deck_error::DeckError;
use crate::cards::rank::*;
use crate::cards::suit::*;
use crate::cards::rank::{Rank, BLANK_RANK};
use crate::cards::suit::Suit;
use crate::{Card, Pack, Pile};

#[derive(Clone, Debug, Hash, PartialEq)]
Expand All @@ -12,6 +12,9 @@ pub struct Standard52 {
}

impl Standard52 {
/// # Errors
///
/// Will return `DeckError::PilePackMismatch` error if `Pile` passed in isn't a `FrenchDeck`.
pub fn new_from_pile(pile: Pile) -> Result<Standard52, DeckError> {
let standard52 = Standard52 {
pack: Pack::french_deck(),
Expand All @@ -25,13 +28,17 @@ impl Standard52 {
}
}

#[must_use]
pub fn new_shuffled() -> Standard52 {
Standard52 {
pack: Pack::french_deck(),
deck: Pile::french_deck().shuffle(),
}
}

/// # Errors
///
/// Will return `DeckError::InvalidIndex` if passed in index is incomplete.
pub fn from_index(card_str: &'static str) -> Result<Standard52, DeckError> {
let mut pile = Pile::default();
for index in card_str.split_whitespace() {
Expand All @@ -50,24 +57,29 @@ impl Standard52 {
}
}

#[must_use]
pub fn to_index(&self) -> String {
self.deck.to_index()
}

#[must_use]
pub fn to_index_str(&self) -> &'static str {
self.deck.to_index_str()
}

#[must_use]
pub fn to_symbol_index(&self) -> String {
self.deck.to_symbol_index()
}

/// A Standard52 deck is complete if a sorted Pile of the deck is equal to it's Pack.
#[must_use]
pub fn is_complete(&self) -> bool {
let pile = self.deck.sort();
&pile == self.pack.cards()
}

#[must_use]
pub fn card_from_index(card_str: &'static str) -> Card {
let rank = Rank::from_french_deck_index(Standard52::rank_str_from_index(card_str));
let suit = Suit::from_french_deck_index(Standard52::suit_char_from_index(card_str));
Expand Down Expand Up @@ -114,6 +126,7 @@ impl fmt::Display for Standard52 {
#[allow(non_snake_case)]
mod standard52_tests {
use super::*;
use crate::{DIAMONDS, FIVE, FOUR, SPADES, THREE, TWO};
use rstest::rstest;

#[test]
Expand Down
9 changes: 9 additions & 0 deletions src/cards/pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ impl Pack {
}

/// Returns true of the combined Cards from the passed in Vector match the Cards in the Pack.
#[must_use]
pub fn is_complete(&self, piles: &[Pile]) -> bool {
let mut pile = Pile::pile_on(piles.to_vec());
pile.sort_in_place();
pile == self.cards
}

/// Returns a reference to the cards in the Pack.
#[must_use]
pub fn cards(&self) -> &Pile {
&self.cards
}
Expand All @@ -51,6 +53,7 @@ impl Pack {
Pack::new(pile)
}

#[must_use]
pub fn euchre_deck() -> Pack {
Pack::new(Pile::euchre_deck())
}
Expand All @@ -61,26 +64,32 @@ impl Pack {
Pack::new(pile)
}

#[must_use]
pub fn french_deck() -> Pack {
Pack::new(Pile::french_deck())
}

#[must_use]
pub fn french_deck_with_jokers() -> Pack {
Pack::new(Pile::french_deck_with_jokers())
}

#[must_use]
pub fn pinochle_deck() -> Pack {
Pack::new(Pile::pinochle_deck())
}

#[must_use]
pub fn short_deck() -> Pack {
Pack::new(Pile::short_deck())
}

#[must_use]
pub fn skat_deck() -> Pack {
Pack::new(Pile::skat_deck())
}

#[must_use]
pub fn spades_deck() -> Pack {
Pack::new(Pile::spades_deck())
}
Expand Down
Loading

0 comments on commit 546c7b3

Please sign in to comment.