Skip to content

Commit

Permalink
refactor: Updating to apply clippy lints
Browse files Browse the repository at this point in the history
  • Loading branch information
dandxy89 committed Oct 29, 2024
1 parent 5269fb7 commit 09c0231
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ pub trait RuleExt {
}

impl RuleExt for Rule {
#[inline]
fn is_numeric(&self) -> bool {
matches!(self, Self::FLOAT | Self::PLUS | Self::MINUS | Self::POS_INFINITY | Self::NEG_INFINITY)
}

#[inline]
fn is_cmp(&self) -> bool {
matches!(self, Self::GT | Self::LT | Self::EQ | Self::GTE | Self::LTE | Self::CMP)
}
Expand All @@ -23,6 +26,7 @@ pub trait AsFloat {
}

impl AsFloat for Pair<'_, Rule> {
#[inline]
#[allow(clippy::unreachable, clippy::wildcard_enum_match_arm)]
fn as_float(&self) -> anyhow::Result<f64> {
match self.as_rule() {
Expand Down
4 changes: 3 additions & 1 deletion src/model/coefficient.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use pest::iterators::Pairs;

use crate::{
common::{AsFloat, RuleExt},
common::{AsFloat as _, RuleExt as _},
Rule,
};

Expand All @@ -15,6 +15,7 @@ pub struct Coefficient {
}

impl PartialEq for Coefficient {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.var_name == other.var_name && self.coefficient == other.coefficient
}
Expand All @@ -25,6 +26,7 @@ impl Eq for Coefficient {}
impl TryFrom<Pairs<'_, Rule>> for Coefficient {
type Error = anyhow::Error;

#[inline]
#[allow(clippy::unreachable, clippy::wildcard_enum_match_arm)]
fn try_from(values: Pairs<'_, Rule>) -> anyhow::Result<Self> {
let (mut value, mut var_name) = (1.0, String::new());
Expand Down
8 changes: 6 additions & 2 deletions src/model/constraint.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::str::FromStr;
use std::str::FromStr as _;

use pest::iterators::Pair;
use unique_id::sequence::SequenceGenerator;

use crate::{
common::RuleExt,
common::RuleExt as _,
model::{coefficient::Coefficient, get_name, lp_problem::LPPart, sense::Cmp, sos::SOSClass},
Rule,
};
Expand All @@ -20,6 +20,7 @@ pub enum Constraint {
}

impl PartialEq for Constraint {
#[inline]
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(
Expand All @@ -38,13 +39,15 @@ impl PartialEq for Constraint {
impl Eq for Constraint {}

impl Constraint {
#[inline]
#[must_use]
pub fn name(&self) -> String {
match self {
Self::Standard { name, .. } | Self::SOS { name, .. } => name.to_string(),
}
}

#[inline]
#[must_use]
pub fn coefficients(&self) -> &[Coefficient] {
match self {
Expand All @@ -57,6 +60,7 @@ impl Constraint {
impl LPPart for Constraint {
type Output = Self;

#[inline]
fn try_into(pair: Pair<'_, Rule>, gen: &mut SequenceGenerator) -> anyhow::Result<Self> {
let mut parts = pair.into_inner().peekable();
// Constraint name can be omitted in LP files, so we need to handle that case
Expand Down
5 changes: 5 additions & 0 deletions src/model/lp_problem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,26 @@ pub struct LPProblem {
}

impl LPProblem {
#[inline]
#[must_use]
pub fn with_problem_name(self, problem_name: &str) -> Self {
Self { problem_name: problem_name.to_owned(), ..self }
}

#[inline]
#[must_use]
pub fn with_sense(self, problem_sense: Sense) -> Self {
Self { problem_sense, ..self }
}

#[inline]
pub fn add_variable(&mut self, name: &str) {
if !name.is_empty() {
self.variables.entry(name.to_owned()).or_default();
}
}

#[inline]
pub fn set_variable_bounds(&mut self, name: &str, kind: Variable) {
if !name.is_empty() {
match self.variables.entry(name.to_owned()) {
Expand All @@ -59,6 +63,7 @@ impl LPProblem {
}
}

#[inline]
pub fn add_objective(&mut self, objectives: Vec<Objective>) {
for ob in &objectives {
ob.coefficients.iter().for_each(|c| {
Expand Down
6 changes: 3 additions & 3 deletions src/model/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::iter::Peekable;

use pest::iterators::Pairs;
use unique_id::{sequence::SequenceGenerator, Generator};
use unique_id::{sequence::SequenceGenerator, Generator as _};

use crate::{model::prefix::Prefix, Rule};
use crate::{model::prefix::Prefix as _, Rule};

pub mod coefficient;
pub mod constraint;
Expand All @@ -17,7 +17,7 @@ pub mod variable;

fn get_name(parts: &mut Peekable<Pairs<'_, Rule>>, gen: &SequenceGenerator, rule: Rule) -> String {
if parts.peek().unwrap().as_rule() == rule {
parts.next().unwrap().as_str().to_string()
parts.next().unwrap().as_str().to_owned()
} else {
format!("{}{}", rule.prefix(), gen.next_id())
}
Expand Down
1 change: 1 addition & 0 deletions src/model/objective.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct Objective {
impl LPPart for Objective {
type Output = Self;

#[inline]
fn try_into(pair: Pair<'_, Rule>, gen: &mut SequenceGenerator) -> anyhow::Result<Self> {
let mut parts = pair.into_inner().peekable();
// Objective name can be omitted in LP files, so we need to handle that case
Expand Down
1 change: 1 addition & 0 deletions src/model/parse_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::{

type ResultVec<T> = anyhow::Result<Vec<T>>;

#[inline]
#[allow(clippy::wildcard_enum_match_arm)]
/// # Errors
/// Returns an error if the `compose` fails
Expand Down
1 change: 1 addition & 0 deletions src/model/prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub trait Prefix {
}

impl Prefix for Rule {
#[inline]
#[allow(clippy::wildcard_enum_match_arm)]
fn prefix(&self) -> &'static str {
match self {
Expand Down
1 change: 1 addition & 0 deletions src/model/sense.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub enum Cmp {
impl FromStr for Cmp {
type Err = anyhow::Error;

#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
">=" => Ok(Self::GreaterOrEqual),
Expand Down
2 changes: 2 additions & 0 deletions src/model/sos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub enum SOSClass {
impl FromStr for SOSClass {
type Err = anyhow::Error;

#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"s1" | "s1::" => Ok(Self::S1),
Expand All @@ -33,6 +34,7 @@ impl FromStr for SOSClass {
impl LPPart for SOSClass {
type Output = Constraint;

#[inline]
fn try_into(pair: Pair<'_, Rule>, _: &mut SequenceGenerator) -> anyhow::Result<Self::Output> {
let mut parts = pair.into_inner();
let name = parts.next().unwrap().as_str().to_owned();
Expand Down
3 changes: 3 additions & 0 deletions src/model/variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub enum Variable {
}

impl From<Rule> for Variable {
#[inline]
#[allow(clippy::wildcard_enum_match_arm, clippy::unreachable)]
fn from(value: Rule) -> Self {
match value {
Expand All @@ -40,6 +41,7 @@ impl From<Rule> for Variable {
}

impl PartialEq for Variable {
#[inline]
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::LB(l0), Self::LB(r0)) | (Self::UB(l0), Self::UB(r0)) => l0 == r0,
Expand All @@ -52,6 +54,7 @@ impl PartialEq for Variable {
impl Eq for Variable {}

impl Variable {
#[inline]
#[allow(clippy::wildcard_enum_match_arm)]
pub fn set_semi_continuous(&mut self) {
if let Self::Bounded(lb, ub, _) = self {
Expand Down
8 changes: 5 additions & 3 deletions src/parse.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use std::{
fs::File,
io::{BufReader, Read},
io::{BufReader, Read as _},
path::Path,
};

use pest::Parser;
use unique_id::{sequence::SequenceGenerator, GeneratorFromSeed};
use pest::Parser as _;
use unique_id::{sequence::SequenceGenerator, GeneratorFromSeed as _};

use crate::{
model::{lp_problem::LPProblem, parse_model::compose},
LParser, Rule,
};

#[inline]
/// # Errors
/// Returns an error if the `read_to_string` or `open` fails
pub fn parse_file(path: &Path) -> anyhow::Result<String> {
Expand All @@ -25,6 +26,7 @@ pub fn parse_file(path: &Path) -> anyhow::Result<String> {
Ok(contents)
}

#[inline]
/// # Errors
/// Returns an error if the parse fails
pub fn parse_lp_file(contents: &str) -> anyhow::Result<LPProblem> {
Expand Down

0 comments on commit 09c0231

Please sign in to comment.