Skip to content

Commit

Permalink
Lint Rust code
Browse files Browse the repository at this point in the history
  • Loading branch information
sdispater committed Aug 4, 2023
1 parent ba8aab6 commit 30835dc
Show file tree
Hide file tree
Showing 11 changed files with 282 additions and 216 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ on:
- '**'

jobs:
Linting:
name: Linting
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: "3.11"
- name: "Install pre-commit"
run: pip install pre-commit
- name: "Install Rust toolchain"
run: rustup component add rustfmt clippy
- run: pre-commit run --all-files

Tests:
name: ${{ matrix.os }} / ${{ matrix.python-version }}
runs-on: ${{ matrix.os }}-latest
Expand Down
9 changes: 9 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,12 @@ repos:
rev: v0.0.270
hooks:
- id: ruff

- repo: local
hooks:
- id: lint-rust
name: Lint Rust
entry: make lint-rust
types: [rust]
language: rust
pass_filenames: false
68 changes: 68 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,71 @@ build_wheels_i686:
# run tests against all supported python versions
tox:
@tox


lint-rust:
cargo fmt --version
cargo fmt --all -- --check
cargo clippy --version
cargo clippy --tests -- \
-D warnings \
-W clippy::pedantic \
-W clippy::dbg_macro \
-W clippy::print_stdout \
-A clippy::cast-possible-truncation \
-A clippy::cast-possible-wrap \
-A clippy::cast-precision-loss \
-A clippy::cast-sign-loss \
-A clippy::doc-markdown \
-A clippy::float-cmp \
-A clippy::fn-params-excessive-bools \
-A clippy::if-not-else \
-A clippy::manual-let-else \
-A clippy::match-bool \
-A clippy::match-same-arms \
-A clippy::missing-errors-doc \
-A clippy::missing-panics-doc \
-A clippy::module-name-repetitions \
-A clippy::must-use-candidate \
-A clippy::needless-pass-by-value \
-A clippy::similar-names \
-A clippy::single-match-else \
-A clippy::struct-excessive-bools \
-A clippy::too-many-lines \
-A clippy::unnecessary-wraps \
-A clippy::unused-self \
-A clippy::used-underscore-binding


format-rust:
cargo fmt --version
cargo fmt --all
cargo clippy --version
cargo clippy --tests --fix --allow-dirty -- \
-D warnings \
-W clippy::pedantic \
-W clippy::dbg_macro \
-W clippy::print_stdout \
-A clippy::cast-possible-truncation \
-A clippy::cast-possible-wrap \
-A clippy::cast-precision-loss \
-A clippy::cast-sign-loss \
-A clippy::doc-markdown \
-A clippy::float-cmp \
-A clippy::fn-params-excessive-bools \
-A clippy::if-not-else \
-A clippy::manual-let-else \
-A clippy::match-bool \
-A clippy::match-same-arms \
-A clippy::missing-errors-doc \
-A clippy::missing-panics-doc \
-A clippy::module-name-repetitions \
-A clippy::must-use-candidate \
-A clippy::needless-pass-by-value \
-A clippy::similar-names \
-A clippy::single-match-else \
-A clippy::struct-excessive-bools \
-A clippy::too-many-lines \
-A clippy::unnecessary-wraps \
-A clippy::unused-self \
-A clippy::used-underscore-binding
27 changes: 3 additions & 24 deletions rust/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ pub const EPOCH_YEAR: u32 = 1970;
pub const DAYS_PER_N_YEAR: u32 = 365;
pub const DAYS_PER_L_YEAR: u32 = 366;

pub const USECS_PER_SEC: u32 = 1000000;

pub const SECS_PER_MIN: u32 = 60;
pub const SECS_PER_HOUR: u32 = SECS_PER_MIN * 60;
pub const SECS_PER_DAY: u32 = SECS_PER_HOUR * 24;

// 400-year chunks always have 146097 days (20871 weeks).
pub const DAYS_PER_400_YEARS: u32 = 146097;
pub const DAYS_PER_400_YEARS: u32 = 146_097;
pub const SECS_PER_400_YEARS: u64 = DAYS_PER_400_YEARS as u64 * SECS_PER_DAY as u64;

// The number of seconds in an aligned 100-year chunk, for those that
Expand All @@ -22,9 +20,10 @@ pub const SECS_PER_100_YEARS: [u64; 2] = [

// The number of seconds in an aligned 4-year chunk, for those that
// do not begin with a leap year and those that do respectively.
#[allow(clippy::erasing_op)]
pub const SECS_PER_4_YEARS: [u32; 2] = [
(4 * DAYS_PER_N_YEAR + 0 * DAYS_PER_L_YEAR) * SECS_PER_DAY,
(3 * DAYS_PER_N_YEAR + 1 * DAYS_PER_L_YEAR) * SECS_PER_DAY,
(3 * DAYS_PER_N_YEAR + DAYS_PER_L_YEAR) * SECS_PER_DAY,
];

// The number of seconds in non-leap and leap years respectively.
Expand All @@ -33,8 +32,6 @@ pub const SECS_PER_YEAR: [u32; 2] = [
DAYS_PER_L_YEAR * SECS_PER_DAY,
];

pub const MONTHS_PER_YEAR: u32 = 12;

// The month lengths in non-leap and leap years respectively.
pub const DAYS_PER_MONTHS: [[i32; 13]; 2] = [
[-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
Expand All @@ -55,23 +52,5 @@ pub const MONTHS_OFFSETS: [[i32; 14]; 2] = [

pub const DAY_OF_WEEK_TABLE: [u32; 12] = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4];

pub const TM_SUNDAY: usize = 0;
pub const TM_MONDAY: usize = 1;
pub const TM_TUESDAY: usize = 2;
pub const TM_WEDNESDAY: usize = 3;
pub const TM_THURSDAY: usize = 4;
pub const TM_FRIDAY: usize = 5;
pub const TM_SATURDAY: usize = 6;

pub const TM_JANUARY: usize = 0;
pub const TM_FEBRUARY: usize = 1;
pub const TM_MARCH: usize = 2;
pub const TM_APRIL: usize = 3;
pub const TM_MAY: usize = 4;
pub const TM_JUNE: usize = 5;
pub const TM_JULY: usize = 6;
pub const TM_AUGUST: usize = 7;
pub const TM_SEPTEMBER: usize = 8;
pub const TM_OCTOBER: usize = 9;
pub const TM_NOVEMBER: usize = 10;
pub const TM_DECEMBER: usize = 11;
16 changes: 8 additions & 8 deletions rust/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::constants::{
};

fn p(year: i32) -> i32 {
return year + year / 4 - year / 100 + year / 400;
year + year / 4 - year / 100 + year / 400
}

pub fn is_leap(year: i32) -> bool {
Expand All @@ -25,22 +25,22 @@ pub fn days_in_year(year: i32) -> u32 {
}

pub fn week_day(year: i32, month: u32, day: u32) -> u32 {
let y: i32 = year - (month < 3) as i32;
let y: i32 = year - i32::from(month < 3);

let w: i32 = (p(y) + DAY_OF_WEEK_TABLE[(month - 1) as usize] as i32 + day as i32) % 7;

if w == 0 {
return 7;
}

w.abs() as u32
w.unsigned_abs()
}

pub fn day_number(year: i32, month: u8, day: u8) -> i32 {
let m = ((month + 9) % 12) as i32;
let m = i32::from((month + 9) % 12);
let y = year - m / 10;

return 365 * y + y / 4 - y / 100 + y / 400 + (m * 306 + 5) / 10 + (day as i32 - 1);
365 * y + y / 4 - y / 100 + y / 400 + (m * 306 + 5) / 10 + (i32::from(day) - 1)
}

pub fn local_time(
Expand All @@ -56,7 +56,7 @@ pub fn local_time(
seconds -= (10957 * SECS_PER_DAY as usize) as isize;
year += 30; // == 2000
} else {
seconds += ((146097 - 10957) * SECS_PER_DAY as usize) as isize;
seconds += ((146_097 - 10957) * SECS_PER_DAY as usize) as isize;
year -= 370; // == 1600
}

Expand Down Expand Up @@ -97,12 +97,12 @@ pub fn local_time(
}

// Handle months and days
let mut month = (TM_DECEMBER + 1) as usize;
let mut month = TM_DECEMBER + 1;
let mut day: usize = (seconds / (SECS_PER_DAY as isize) + 1) as usize;
seconds %= SECS_PER_DAY as isize;

let mut month_offset: usize;
while month != (TM_JANUARY + 1) as usize {
while month != (TM_JANUARY + 1) {
month_offset = MONTHS_OFFSETS[leap_year][month] as usize;
if day > month_offset {
day -= month_offset;
Expand Down
Loading

0 comments on commit 30835dc

Please sign in to comment.