Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions .github/workflows/create_pull_requests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Quality Check & PR to Master

on:
push:
branches:
- dev

jobs:
# test:
# runs-on: ubuntu-latest

# steps:
# - name: Checkout Code
# uses: actions/checkout@v4

# - name: Set up Rust
# uses: actions/setup-python@v5
# with:
# python-version: '3.11' # You can change this to your preferred version
# cache: 'pip'

# - name: Install Dependencies
# run: |
# python -m pip install --upgrade pip
# if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
# pip install pytest # Ensures pytest is available even if not in requirements.txt

# - name: Create .env file
# run: |

# - name: Run Tests
# run: |
# mkdir logs
# pytest -s

create_pull_request:
runs-on: ubuntu-latest
# needs: test # This creates the dependency link
if: github.actor == 'brianobot'
permissions:
contents: write
pull-requests: write
env:
GITHUB_TOKEN: ${{ secrets.MY_PERSONAL_TOKEN }}
steps:
- name: Checkout Code
uses: actions/checkout@v4

- name: Create Pull Request
run: |
# Check if a PR already exists from dev to master
PR_EXISTS=$(gh pr list --head dev --base master --state open --json number -q '.[0].number')

if [ -z "$PR_EXISTS" ]; then
echo "No open PR found. Creating a new one..."
gh pr create \
--head dev \
--base master \
--title "Auto-PR: Dev to Master" \
--body "Automated PR triggered by push from ${{ github.actor }}" \
--assignee "${{ github.actor }}"
else
echo "PR already exists: #$PR_EXISTS. Skipping creation."
fi
23 changes: 23 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
repos:
- repo: local
hooks:
- id: rustfmt
name: rustfmt
entry: cargo fmt --all -- --check
language: system
types: [rust]
pass_filenames: false

- id: cargo-check
name: cargo-check
entry: cargo check
language: system
types: [rust]
pass_filenames: false

- id: clippy
name: clippy
entry: cargo clippy -- -D warnings
language: system
types: [rust]
pass_filenames: false
9 changes: 5 additions & 4 deletions src/bin/01_variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn main() {
// causes the compiler to produce an error message, so only use mut if you indeed need to mutate
// the variable in question
println!("The value of y is {y}");
y = y + 10;
y += 10;
println!("The new value of y is {y}");

// CONSTANTS are not allowed to be made mutable, they are always immutable
Expand Down Expand Up @@ -73,7 +73,7 @@ fn main() {

timeout = 12.10;

timeout -= 1 as f32;
timeout -= 1f32;

println!("Timeout = {timeout}");

Expand Down Expand Up @@ -134,7 +134,8 @@ fn main() {
x += 10;
println!("X = {x}");

let ass_val = x = 10;
#[allow(clippy::let_unit_value)]
let _ass_val = x = 10;
println!("X = {}", x);
println!("Assigment value = {:?}", ass_val);
println!("Assigment value = {:?}", ());
}
4 changes: 2 additions & 2 deletions src/bin/02_data_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ fn main() {
// you can mix underscore and type suffix to better indicate the type of an integer
//
let _rating = 24_u8; // the underscore is ignore and only needed to make it easier to read
let _score = 25__u16;
let _score = 25_u16;

let some_value = 123u16;
println!("Some Value = {:?}", some_value);

// floating point type
let _rating = 5__f32; // again the underscore is ignored
let _rating = 5_f32; // again the underscore is ignored
let _average = 32f64;

// the floating point type has associated constant to meet IEEE specs
Expand Down
2 changes: 2 additions & 0 deletions src/bin/02b_literals.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::nonminimal_bool)]

fn main() {
// integers 1
// floats 1.23
Expand Down
9 changes: 8 additions & 1 deletion src/bin/02e_vectors.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::vec_init_then_push)]

use std::vec;

fn main() {
Expand All @@ -14,6 +16,7 @@ fn main() {

// you can pop elements from the vector
let last_value = data.pop();
#[allow(clippy::unnecessary_unwrap)]
if last_value.is_some() {
println!("Popped value: {}", last_value.unwrap());
}
Expand Down Expand Up @@ -98,6 +101,7 @@ fn main() {
// we can emulate the behaviour of storing different types in a vector
// by storying an enum that can hold different types

#[allow(clippy::enum_variant_names)]
#[derive(Debug)]
enum FieldType {
SmallIntegerField(i8),
Expand All @@ -119,13 +123,14 @@ fn main() {
}
}

#[allow(clippy::vec_init_then_push)]
let mut columns: Vec<FieldType> = Vec::new();

columns.push(FieldType::IntergerField(10));
columns.push(FieldType::SmallIntegerField(10));
columns.push(FieldType::BigIntergerField(10000000));
columns.push(FieldType::StringField("Hello".to_string()));
columns.push(FieldType::FloatField(3.14));
columns.push(FieldType::FloatField(3.146565));

println!("Columns = {:?}", columns);

Expand All @@ -145,6 +150,7 @@ fn main() {
println!("Reversed data = {:?}", data);

let mut names = vec!["John", "Doe", "Jane", "Doe", "Alice", "Bob"];
#[allow(clippy::unnecessary_sort_by)]
names.sort_by(|a, b| a.len().cmp(&b.len()));
println!("Sorted names = {:?}", names);

Expand Down Expand Up @@ -174,6 +180,7 @@ fn main() {
);

// an example to convert an vector of &str types to a vector String types
#[allow(clippy::useless_vec)]
let names = vec!["james", "paul", "okon", "obot", "abasifreke"]
.iter_mut()
.map(|x| x.to_string())
Expand Down
9 changes: 5 additions & 4 deletions src/bin/02g_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ fn main() {
#[allow(dead_code)]
fn show_files() -> std::io::Result<()> {
#[allow(dead_code)]
#[allow(clippy::useless_vec)]
let mut _v = vec![1, 2, 3];

// perfectly valid rust code
Expand Down Expand Up @@ -89,7 +90,7 @@ fn main() {
println!("Current Value = {start}");
start += 1;

if start % 24 == 0 {
if start.is_multiple_of(24) {
break start; // you can use break to return values from a loop in rust
}
};
Expand Down Expand Up @@ -117,10 +118,10 @@ fn main() {
break 'search;
}
}
println!("");
println!(" ");
temp += 2;
}
println!("");
println!(" ");

// a break can have a label and a value but both are optional
// break - empty break
Expand Down Expand Up @@ -170,7 +171,7 @@ fn partition<T: Ord>(slice: &mut [T]) -> usize {

for j in 0..pivot_index {
if slice[j] <= slice[pivot_index] {
i = i + 1isize;
i += 1isize;
slice.swap(i as usize, j);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/bin/03_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,5 @@ fn with_return_value() -> u32 {
}

fn with_return_keyword() -> f64 {
return 64.0;
64.0
}
2 changes: 2 additions & 0 deletions src/bin/03b_more_on_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ fn main() {
// the by value captures usually occurs for type that are non_copy
// to force closure to capture a variable by value, use the keyword move before the pipes

#[allow(clippy::useless_vec)]
let haystack = vec![1, 2, 3, 4];
let contains = move |needle: i32| haystack.contains(&needle);

Expand Down Expand Up @@ -90,6 +91,7 @@ impl _Box {

// &self is usually used in place of the full form self: &Self, where Self point to the type the function is
// defined on, self gives access to the type fields via dot notation
#[allow(clippy::needless_arbitrary_self_type)]
fn _area(self: &Self) -> u8 {
self.width * self.heigth
}
Expand Down
1 change: 1 addition & 0 deletions src/bin/03c_diverging_functions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![allow(unreachable_code)]
#![allow(clippy::diverging_sub_expression)]

fn main() {
// diverging functions are functions that never return,
Expand Down
2 changes: 1 addition & 1 deletion src/bin/04_comments.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[allow(dead_code)]
#![allow(dead_code)]

fn main() {
// usually you would find them used above the line which they are referring to
Expand Down
5 changes: 3 additions & 2 deletions src/bin/05_control_flows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ fn main() {

// if it also important to note that the condtion to be use in an if block must evaluate to a bool
// unlike languages like ruby, and javascript, rust will not try to convert non-boolean to boolean
if condition == true {
if condition {
// blocks of codes associated with an if statements are sometimes called arms
// like the arms in the match statment we saw in the game
println!("Well Well Well, Isn't today Amazing");
Expand Down Expand Up @@ -63,7 +63,7 @@ fn main() {
break;
}

count = count + 1;
count += 1;

// continue is used to skip the remaining part of the loop and go to the
// next interation
Expand Down Expand Up @@ -136,6 +136,7 @@ fn main() {
}
println!("Lift Off...");

#[allow(clippy::never_loop)]
loop {
println!("Some information..");
break;
Expand Down
3 changes: 2 additions & 1 deletion src/bin/06_0_ownership_rule_revisited.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#[allow(unused_variables)]
#![allow(unused_variables)]

// THis is a revisit of the ownership rule in Rust
/*
- Each value in Rust has an owner
Expand Down
3 changes: 3 additions & 0 deletions src/bin/06_ownership_intro.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::vec_init_then_push)]

// RESEARCH ON FILE NOT INCLUDED IN CRATE HEIRACHY WARNING
fn main() {
// create a varible to hold a string
Expand Down Expand Up @@ -70,6 +72,7 @@ fn main() {
// the Vec<Person> Owns each person inside of it
// Each person Struct owns the name and the age field
// Each Name field owns the buffer stored on the heap for the String
#[allow(clippy::vec_init_then_push)]
let mut composers = Vec::new();
composers.push(Person {
name: Some(String::from("J.Cole")),
Expand Down
5 changes: 2 additions & 3 deletions src/bin/06b_ownership_contd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ fn gives_ownership() -> String {
// return value into the function
// that calls it

let some_string = String::from("yours"); // some_string comes into scope

some_string // some_string is returned and
String::from("yours") // some_string comes into scope
// some_string is returned and
// moves out to the calling
// function
}
Expand Down
1 change: 1 addition & 0 deletions src/bin/07a_references_extended.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ fn main() {
// well that's because, the . operator implicity dereferences references when neccesary
// and in this case, the println! macro triggered a dot operation somewhere to get this behaviour
// the . operator can also implictly borrow a reference to it left operand when needed
#[allow(clippy::useless_vec)]
let mut v = vec![1, 2, 3, 4];
v.sort(); // here the sort method needs a &mut to the object so .sort borrows mut v as &mut v
// which is equivalent to (&mut v).sort()
Expand Down
2 changes: 2 additions & 0 deletions src/bin/08b_more_on_slices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ fn main() {
// array element can be safely accessed with get method, this returns an Option
// and can be matched as showned below
let fifth_element = empty_array.get(5);

#[allow(clippy::manual_unwrap_or)]
let value = match fifth_element {
Some(value) => value,
// since the value of value in the Some variant was a reference to an i32
Expand Down
4 changes: 2 additions & 2 deletions src/bin/09_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ fn build_user(username: String, email: String) -> User {
// we can simplify this by using a field init shorthand syntax like
// email, instead of email: email,
// username, instead of username: username,
email: email,
username: username,
email,
username,
sign_in_count: 1,
active: true,
}
Expand Down
2 changes: 1 addition & 1 deletion src/bin/09c_more_on_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn square(point: Point, width: u32) -> Rectangle {
};
Rectangle {
bottom_left: point,
top_right: top_right,
top_right,
}
}

Expand Down
1 change: 1 addition & 0 deletions src/bin/09f_default_values_in_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ enum Titan {
Einstein,
}

#[allow(clippy::derivable_impls)]
impl Default for Titan {
fn default() -> Self {
Titan::Brian
Expand Down
7 changes: 4 additions & 3 deletions src/bin/10_b2_more_enums.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![allow(dead_code)]
#![allow(clippy::upper_case_acronyms)]

fn main() {
// enums variant can hold any type and amount of associate data
Expand All @@ -11,15 +12,15 @@ fn main() {

impl PlayerData {
fn is_weak(&self) -> bool {
return &self.health < &30;
self.health < 30
}

fn is_strong(&self) -> bool {
return &self.health > &70;
self.health > 70
}

fn is_dead(&self) -> bool {
return &self.health == &0;
self.health == 0
}
}

Expand Down
Loading