Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix clippy #155

Merged
merged 2 commits into from
Sep 15, 2024
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
30 changes: 30 additions & 0 deletions .github/workflows/cross.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Compile for the RPi

on:
push:
branches: [ "master" ]
workflow_dispatch:

jobs:
cross-compile:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Prepare
run: cargo install cross --git https://github.com/cross-rs/cross
- name: Build
run: |
cross build --examples --bins --release --target aarch64-unknown-linux-gnu
mv target/aarch64-unknown-linux-gnu/release/STS1_EDU_Scheduler .
mv target/aarch64-unknown-linux-gnu/release/examples/cli .
cp scheduler/config.toml .
cp scheduler/scheduler.service .
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: aarch64 package
path: |
STS1_EDU_Scheduler
config.toml
scheduler.service
cli
30 changes: 8 additions & 22 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
name: Build & Test

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

Expand All @@ -21,25 +19,13 @@ jobs:
- name: Run tests
run: cargo test --release --features mock

cross-compile:
static-checks:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Prepare
run: cargo install cross --git https://github.com/cross-rs/cross
- name: Build
run: |
cross build --examples --bins --release --target aarch64-unknown-linux-gnu
mv target/aarch64-unknown-linux-gnu/release/STS1_EDU_Scheduler .
mv target/aarch64-unknown-linux-gnu/release/examples/cli .
cp scheduler/config.toml .
cp scheduler/scheduler.service .
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: aarch64 package
path: |
STS1_EDU_Scheduler
config.toml
scheduler.service
cli
- uses: actions/checkout@v4
- name: Install
run: sudo apt install libudev-dev
- name: Clippy
run: cargo clippy -- -D warnings
- name: Formatting
run: cargo fmt --all --check
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ pedantic = { level = "warn", priority = -1 }
missing_errors_doc = "allow"
module_name_repetitions = "allow"
missing_panics_doc = "allow"
should_panic_without_expect = "allow"
35 changes: 17 additions & 18 deletions filevec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,12 @@ impl<T: Serialize + DeserializeOwned> FileVec<T> {
/// **Note:** If the file exists and contains invalid data, it is interpreted as
/// empty and overwritten.
pub fn open(path: impl AsRef<Path>) -> Result<Self, std::io::Error> {
let mut file =
std::fs::OpenOptions::new().read(true).write(true).create(true).open(path)?;
let mut file = std::fs::OpenOptions::new()
.read(true)
.write(true)
.create(true)
.truncate(false)
.open(path)?;
let metadata = file.metadata()?;

let vec = if metadata.len() > 0 {
Expand Down Expand Up @@ -151,17 +155,12 @@ impl<'a, T: Serialize + DeserializeOwned> Drop for FileVecGuard<'a, T> {

#[cfg(test)]
mod test {
use std::{
fs::File,
io::{Read, Write},
ops::DerefMut,
};

use super::FileVec;
use std::io::{Read, Write};

#[test]
fn empty_vec() {
let f = FileVec::<u16>::open("__empty_vec".to_string()).unwrap();
let f = FileVec::<u16>::open("__empty_vec").unwrap();

assert_eq!(f.as_ref().len(), 0);
assert!(std::path::Path::new("__empty_vec").exists());
Expand All @@ -175,20 +174,20 @@ mod test {
let buffer = rmp_serde::to_vec(&DATA).unwrap();
std::fs::File::create("__prefilled").unwrap().write_all(&buffer).unwrap();

let f = FileVec::<u8>::open("__prefilled".to_string()).unwrap();
let f = FileVec::<u8>::open("__prefilled").unwrap();
assert_eq!(&DATA, f.as_ref().as_slice());

let _ = std::fs::remove_file("__prefilled");
}

#[test]
fn push_single() {
let mut f = FileVec::<i32>::open("__push_single".to_string()).unwrap();
let mut f = FileVec::<i32>::open("__push_single").unwrap();
f.push(123).unwrap();
assert_eq!(f[0], 123);

drop(f);
let f = FileVec::<i32>::open("__push_single".to_string()).unwrap();
let f = FileVec::<i32>::open("__push_single").unwrap();
assert_eq!(f[0], 123);

let _ = std::fs::remove_file("__push_single");
Expand All @@ -203,13 +202,13 @@ mod test {

#[test]
fn push_multiple_structs() {
let mut f = FileVec::open("__push_multiple".to_string()).unwrap();
let mut f = FileVec::open("__push_multiple").unwrap();
f.push(TestaStruct { int16: 1, uint32: 2, stringa: "Hello".into() }).unwrap();
f.push(TestaStruct { int16: 3, uint32: 4, stringa: "Hello2".into() }).unwrap();
f.push(TestaStruct { int16: 5, uint32: 6, stringa: "Hello3".into() }).unwrap();
drop(f);

let f: FileVec<TestaStruct> = FileVec::open("__push_multiple".to_string()).unwrap();
let f: FileVec<TestaStruct> = FileVec::open("__push_multiple").unwrap();
assert_eq!(f[0], TestaStruct { int16: 1, uint32: 2, stringa: "Hello".into() });
assert_eq!(f[2], TestaStruct { int16: 5, uint32: 6, stringa: "Hello3".into() });

Expand All @@ -218,8 +217,8 @@ mod test {

#[test]
fn remove() {
let mut f: FileVec<i32> = FileVec::open("__remove".to_string()).unwrap();
f.extend([0i32, 1, 2, 3, 4, 5, 6].into_iter());
let mut f: FileVec<i32> = FileVec::open("__remove").unwrap();
f.extend([0i32, 1, 2, 3, 4, 5, 6]);

let mut buffer = Vec::new();
std::fs::File::open("__remove").unwrap().read_to_end(&mut buffer).unwrap();
Expand All @@ -237,8 +236,8 @@ mod test {

#[test]
fn pop() {
let mut f = FileVec::open("__pop".to_string()).unwrap();
f.extend([0, 1, 2, 3].into_iter());
let mut f = FileVec::open("__pop").unwrap();
f.extend([0, 1, 2, 3]);

assert_eq!(f.pop().unwrap(), Some(3));
assert_eq!(f.pop().unwrap(), Some(2));
Expand Down
9 changes: 3 additions & 6 deletions scheduler/src/command/execute_program.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
use super::{CommandError, CommandResult, SyncExecutionContext};
use crate::{
command::{
check_length, terminate_student_program, truncate_to_size, Event, ProgramStatus, ResultId,
RetryEvent,
check_length, terminate_student_program, Event, ProgramStatus, ResultId, RetryEvent,
},
communication::{CEPPacket, CommunicationHandle},
};
use anyhow::anyhow;
use simple_archive::Compression;
use std::{
fs::File,
io::{ErrorKind, Read, Write},
io::{ErrorKind, Write},
path::{Path, PathBuf},
time::Duration,
};
Expand Down Expand Up @@ -152,8 +150,7 @@ fn build_result_archive(res: ResultId) -> Result<(), std::io::Error> {

let res_path =
PathBuf::from(format!("./archives/{}/results/{}", res.program_id, res.timestamp));
let student_log_path =
PathBuf::from(format!("./data/{res}.log"));
let student_log_path = PathBuf::from(format!("./data/{res}.log"));
let log_path = PathBuf::from("./log");

add_to_archive_if_exists(&mut archive, &res.to_string(), res_path, Compression::None)?;
Expand Down
5 changes: 4 additions & 1 deletion scheduler/src/command/execution_context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use filevec::FileVec;
use std::{
fmt::Display, str::FromStr, sync::{Arc, Mutex}, thread
fmt::Display,
str::FromStr,
sync::{Arc, Mutex},
thread,
};

const EVENT_SEND_TRIES: u32 = 5;
Expand Down
1 change: 1 addition & 0 deletions scheduler/tests/simulation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ pub fn execute_program(program_id: u16, timestamp: u32, timeout: u16) -> Vec<u8>
vec
}

#[allow(dead_code)]
pub fn stop_program() -> Vec<u8> {
vec![3u8]
}
Expand Down
1 change: 1 addition & 0 deletions scheduler/tests/software_tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ pub fn cleanup(unique: &str) {
let _ = std::fs::remove_file(format!("tests/tmp/{unique}_r"));
}

#[allow(dead_code)]
pub fn store_archive(program_id: u16) -> Vec<u8> {
let mut vec = vec![1u8];
vec.extend(program_id.to_le_bytes());
Expand Down