Skip to content

Commit

Permalink
Implemented about 60 clippy lints
Browse files Browse the repository at this point in the history
  • Loading branch information
maxfierrog committed May 7, 2024
1 parent da46b6c commit faf0ada
Show file tree
Hide file tree
Showing 20 changed files with 202 additions and 222 deletions.
1 change: 1 addition & 0 deletions src/database/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(drop_bounds)]
//! # Database Module
//!
//! This module contains memory and I/O mechanisms used to store and fetch
Expand Down
25 changes: 14 additions & 11 deletions src/database/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
//! #### Authorship
//! - Max Fierro, 2/24/2024 ([email protected])
use std::fmt::Display;

use anyhow::Result;

use crate::database::error::DatabaseError;
Expand Down Expand Up @@ -123,17 +125,18 @@ impl SchemaBuilder {

/* UTILITY IMPLEMENTATIONS */

impl ToString for Datatype {
fn to_string(&self) -> String {
match self {
Datatype::DPFP => "Double-Precision Floating Point".to_string(),
Datatype::SPFP => "Single-Precision Floating Point".to_string(),
Datatype::CSTR => "C-Style ASCII String".to_string(),
Datatype::UINT => "Unsigned Integer".to_string(),
Datatype::SINT => "Signed Integer".to_string(),
Datatype::ENUM => "Enumeration".to_string(),
Datatype::BOOL => "Boolean".to_string(),
}
impl Display for Datatype {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let content = match self {
Datatype::DPFP => "Double-Precision Floating Point",
Datatype::SPFP => "Single-Precision Floating Point",
Datatype::CSTR => "C-Style ASCII String",
Datatype::UINT => "Unsigned Integer",
Datatype::SINT => "Signed Integer",
Datatype::ENUM => "Enumeration",
Datatype::BOOL => "Boolean",
};
write!(f, "{}", content)
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/game/crossteaser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,10 @@ use crate::game::Transition;
use crate::game::Variable;
use crate::interface::IOMode;
use crate::interface::Solution;
use crate::model::database::Identifier;
use crate::model::game::State;
use crate::model::game::Variant;
use crate::model::solver::SUtility;
use crate::solver::ClassicPuzzle;
use crate::util::Identify;

/* SUBMODULES */

Expand Down
12 changes: 6 additions & 6 deletions src/game/crossteaser/variants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ pub fn parse_variant(variant: String) -> Result<Session, GameError> {

/* VARIANT STRING VERIFICATION */

fn check_variant_pattern(variant: &String) -> Result<(), GameError> {
fn check_variant_pattern(variant: &str) -> Result<(), GameError> {
let re = Regex::new(VARIANT_PATTERN).unwrap();
if !re.is_match(&variant) {
if !re.is_match(variant) {
Err(GameError::VariantMalformed {
game_name: NAME,
hint: format!(
Expand All @@ -68,13 +68,13 @@ fn parse_parameters(variant: &str) -> Result<Vec<u64>, GameError> {
.parse::<u64>()
.map_err(|e| GameError::VariantMalformed {
game_name: NAME,
hint: format!("{}", e.to_string()),
hint: e.to_string(),
})
})
.collect()
}

fn check_param_count(params: &Vec<u64>) -> Result<(), GameError> {
fn check_param_count(params: &[u64]) -> Result<(), GameError> {
if params.len() != 3 {
Err(GameError::VariantMalformed {
game_name: NAME,
Expand All @@ -86,8 +86,8 @@ fn check_param_count(params: &Vec<u64>) -> Result<(), GameError> {
}
}

fn check_params_are_positive(params: &Vec<u64>) -> Result<(), GameError> {
if params.iter().any(|&x| x <= 0) {
fn check_params_are_positive(params: &[u64]) -> Result<(), GameError> {
if params.iter().any(|&x| x == 0) {
Err(GameError::VariantMalformed {
game_name: NAME,
hint: "All integers in the string must be positive.".to_owned(),
Expand Down
40 changes: 15 additions & 25 deletions src/game/mock/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,21 +198,19 @@ impl<'a> SessionBuilder<'a> {
),
})?
}
} else {
if new.terminal() && new_count < old_count {
Err(anyhow! {
format!(
"While constructing the game '{}', a medial node was \
added with a 0-indexed turn of {}, but then a new \
terminal node was added with {} entries. All turn \
indicators must be able to index terminal nodes'\
utility entries.",
self.name,
old_count - 1,
new_count,
),
})?
}
} else if new.terminal() && new_count < old_count {
Err(anyhow! {
format!(
"While constructing the game '{}', a medial node was \
added with a 0-indexed turn of {}, but then a new \
terminal node was added with {} entries. All turn \
indicators must be able to index terminal nodes'\
utility entries.",
self.name,
old_count - 1,
new_count,
),
})?
}

if new.terminal() {
Expand Down Expand Up @@ -301,21 +299,13 @@ impl Node {
/// Returns true if and only if `self` is a terminal node.
#[inline]
pub const fn terminal(&self) -> bool {
if let Node::Terminal(_) = self {
true
} else {
false
}
matches!(self, Node::Terminal(_))
}

/// Returns true if and only if `self` is a medial node.
#[inline]
pub const fn medial(&self) -> bool {
if let Node::Medial(_) = self {
true
} else {
false
}
matches!(self, Node::Medial(_))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/game/mock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ mod tests {
.build()?;

g.visualize(MODULE_NAME)?;
let states = vec![
let states = [
g.state(&s1),
g.state(&s2),
g.state(&s3),
Expand Down
4 changes: 2 additions & 2 deletions src/game/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl mock::Session<'_> {

let subdir = PathBuf::from(module);
let mut dir = get_directory(DevelopmentData::Visuals, subdir)?;
let name = format!("{}.svg", self.name()).replace(" ", "-");
let name = format!("{}.svg", self.name()).replace(' ', "-");

dir.push(name);
let file = File::create(dir)?;
Expand Down Expand Up @@ -71,7 +71,7 @@ impl Display for mock::Session<'_> {
mock::Node::Medial(turn) => {
attrs += &format!("label=P{} ", turn);
attrs += "style=filled ";
if self.start() == self.state(&node).unwrap() {
if self.start() == self.state(node).unwrap() {
attrs += "shape=doublecircle ";
attrs += "fillcolor=navajowhite3 ";
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/game/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ where
.decode(s.clone())
.context(format!("Failed to parse line #{}.", l))?;
if prev == game.start() {
for i in 1..history.len() {
let (l, s) = history[i].clone();
for h in history.iter().skip(1) {
let (l, s) = h.clone();
let next = game
.decode(s)
.context(format!("Failed to parse line #{}.", l))?;
Expand Down
14 changes: 7 additions & 7 deletions src/game/zero_by/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ type Elements = u64;

/* GAME DATA */

const NAME: &'static str = "zero-by";
const AUTHORS: &'static str = "Max Fierro <[email protected]>";
const ABOUT: &'static str =
const NAME: &str = "zero-by";
const AUTHORS: &str = "Max Fierro <[email protected]>";
const ABOUT: &str =
"Many players take turns removing a number of elements from a set of arbitrary \
size. The game variant determines how many players are in the game, how many \
elements are in the set to begin with, and the options players have in the \
Expand Down Expand Up @@ -193,13 +193,13 @@ impl Bounded for Session {

fn end(&self, state: State) -> bool {
let (_, elements) = self.decode_state(state);
elements <= 0
elements == 0
}
}

impl Codec for Session {
fn decode(&self, string: String) -> Result<State> {
Ok(parse_state(&self, string)?)
Ok(parse_state(self, string)?)
}

fn encode(&self, state: State) -> Result<String> {
Expand All @@ -226,8 +226,8 @@ impl<const N: PlayerCount> Extensive<N> for Session {
impl<const N: PlayerCount> SimpleUtility<N> for Session {
fn utility(&self, state: State) -> [SUtility; N] {
let (turn, _) = self.decode_state(state);
let mut payoffs = [SUtility::LOSE; N];
payoffs[turn] = SUtility::WIN;
let mut payoffs = [SUtility::Lose; N];
payoffs[turn] = SUtility::Win;
payoffs
}
}
18 changes: 8 additions & 10 deletions src/game/zero_by/states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ use crate::model::game::State;

/* ZERO-BY STATE ENCODING */

pub const STATE_DEFAULT: &'static str = "10-0";
pub const STATE_PATTERN: &'static str = r"^\d+-\d+$";
pub const STATE_PROTOCOL: &'static str =
pub const STATE_DEFAULT: &str = "10-0";
pub const STATE_PATTERN: &str = r"^\d+-\d+$";
pub const STATE_PROTOCOL: &str =
"The state string should be two dash-separated positive integers without any \
decimal points. The first integer will indicate the amount of elements left to \
remove from the set, and the second indicates whose turn it is to remove an \
Expand All @@ -41,15 +41,15 @@ pub fn parse_state(
check_state_pattern(&from)?;
let params = parse_parameters(&from)?;
let (elements, turn) = check_param_count(&params)?;
check_variant_coherence(elements, turn, &session)?;
check_variant_coherence(elements, turn, session)?;
Ok(session.encode_state(turn, elements))
}

/* STATE STRING VERIFICATION */

fn check_state_pattern(from: &String) -> Result<(), GameError> {
let re = Regex::new(STATE_PATTERN).unwrap();
if !re.is_match(&from) {
if !re.is_match(from) {
Err(GameError::StateMalformed {
game_name: NAME,
hint: format!(
Expand All @@ -62,22 +62,20 @@ fn check_state_pattern(from: &String) -> Result<(), GameError> {
}
}

fn parse_parameters(from: &String) -> Result<Vec<u64>, GameError> {
fn parse_parameters(from: &str) -> Result<Vec<u64>, GameError> {
from.split('-')
.map(|int_string| {
int_string
.parse::<u64>()
.map_err(|e| GameError::StateMalformed {
game_name: NAME,
hint: format!("{}", e.to_string()),
hint: e.to_string(),
})
})
.collect()
}

fn check_param_count(
params: &Vec<u64>,
) -> Result<(Elements, Player), GameError> {
fn check_param_count(params: &[u64]) -> Result<(Elements, Player), GameError> {
if params.len() != 2 {
Err(GameError::StateMalformed {
game_name: NAME,
Expand Down
27 changes: 13 additions & 14 deletions src/game/zero_by/variants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ use crate::util::min_ubits;

/* ZERO-BY VARIANT ENCODING */

pub const VARIANT_DEFAULT: &'static str = "2-10-1-2";
pub const VARIANT_PATTERN: &'static str = r"^[1-9]\d*(?:-[1-9]\d*)+$";
pub const VARIANT_PROTOCOL: &'static str =
pub const VARIANT_DEFAULT: &str = "2-10-1-2";
pub const VARIANT_PATTERN: &str = r"^[1-9]\d*(?:-[1-9]\d*)+$";
pub const VARIANT_PROTOCOL: &str =
"The variant string should be a dash-separated group of three or more positive \
integers. For example, '4-232-23-6-3-6' is valid but '598', '-23-1-5', and \
'fifteen-2-5' are not. The first integer represents the number of players in \
Expand Down Expand Up @@ -67,16 +67,16 @@ fn parse_parameters(variant: &str) -> Result<Vec<u64>, GameError> {
.parse::<u64>()
.map_err(|e| GameError::VariantMalformed {
game_name: NAME,
hint: format!("{}", e.to_string()),
hint: e.to_string(),
})
})
.collect();
params
}

fn check_variant_pattern(variant: &String) -> Result<(), GameError> {
fn check_variant_pattern(variant: &str) -> Result<(), GameError> {
let re = Regex::new(VARIANT_PATTERN).unwrap();
if !re.is_match(&variant) {
if !re.is_match(variant) {
Err(GameError::VariantMalformed {
game_name: NAME,
hint: format!(
Expand All @@ -89,31 +89,30 @@ fn check_variant_pattern(variant: &String) -> Result<(), GameError> {
}
}

fn check_param_count(params: &Vec<u64>) -> Result<(), GameError> {
fn check_param_count(params: &[u64]) -> Result<(), GameError> {
if params.len() < 3 {
Err(GameError::VariantMalformed {
game_name: NAME,
hint: format!(
"String needs to have at least 3 dash-separated integers."
),
hint: "String needs to have at least 3 dash-separated integers."
.to_string(),
})
} else {
Ok(())
}
}

fn check_params_are_positive(params: &Vec<u64>) -> Result<(), GameError> {
if params.iter().any(|&x| x <= 0) {
fn check_params_are_positive(params: &[u64]) -> Result<(), GameError> {
if params.iter().any(|&x| x == 0) {
Err(GameError::VariantMalformed {
game_name: NAME,
hint: format!("All integers in the string must be positive."),
hint: "All integers in the string must be positive.".to_string(),
})
} else {
Ok(())
}
}

fn parse_player_count(params: &Vec<u64>) -> Result<Player, GameError> {
fn parse_player_count(params: &[u64]) -> Result<Player, GameError> {
if params[0] > (Player::MAX as u64) {
Err(GameError::VariantMalformed {
game_name: NAME,
Expand Down
6 changes: 3 additions & 3 deletions src/interface/standard/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub enum Commands {
/// Run a query on an existing table in the system.
Query(QueryArgs),

/// Provide information about offerings.
/// Provides information about the system's offerings.
Info(InfoArgs),
}

Expand Down Expand Up @@ -125,8 +125,8 @@ pub struct InfoArgs {

/* DEFAULTS PROVIDED */
/// Specify which of the game's attributes to provide information about.
#[arg(short, long)]
pub attributes: Option<Vec<GameAttribute>>,
#[arg(short, long, value_delimiter = ',')]
pub attributes: Vec<GameAttribute>,

/// Format in which to send output to STDOUT.
#[arg(short, long, default_value_t = InfoFormat::Legible)]
Expand Down
Loading

0 comments on commit faf0ada

Please sign in to comment.