Skip to content

Commit

Permalink
feat(rust): nicer output and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
neferin12 committed Feb 23, 2024
1 parent 156244e commit eb9ce31
Show file tree
Hide file tree
Showing 8 changed files with 261 additions and 20 deletions.
179 changes: 178 additions & 1 deletion rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ path = "src/main.rs"
[dependencies]
clap = { version = "4.4.18", features = ["derive"] }
rand = "0.8.5"
serde = { version = "1.0.197", features = ["derive"] }
indicatif = "0.17.8"
console = "0.15.8"
tabled = "0.15.0"
4 changes: 2 additions & 2 deletions rust/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fs;
use crate::types::{Seminar, SeminarType, Student};

pub fn import_students(students_path: &str, seminars: &Vec<Seminar>) -> Vec<Student> {
println!("Importing students from {students_path}");
// println!("Importing students from {students_path}");
let mut students: Vec<Student> = Vec::new();

let contents = fs::read_to_string(students_path)
Expand Down Expand Up @@ -30,7 +30,7 @@ pub fn import_students(students_path: &str, seminars: &Vec<Seminar>) -> Vec<Stud
}

pub fn import_seminars(seminars_path: &str) -> Vec<Seminar> {
println!("Importing seminars from {seminars_path}");
// println!("Importing seminars from {seminars_path}");
let mut seminars: Vec<Seminar> =Vec::new();
let contents = fs::read_to_string(seminars_path)
.expect(&*format!("Could not read file {seminars_path}"));
Expand Down
5 changes: 3 additions & 2 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod io;
pub mod types;
pub mod algorithm;
pub mod constants;
pub mod constants;
pub mod rism_classic;
pub mod rism_model_checking;
64 changes: 61 additions & 3 deletions rust/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
use clap::Parser;
use rism::algorithm::run_algorithm;
use serde::Serialize;
use rism::rism_classic::run_algorithm;
use rism::constants::get_default_points;
use rism::io::{import_students, import_seminars};
use rism::rism_model_checking::run_model_check;
use console::style;
use tabled::{Table, Tabled};
use tabled::settings::Style;

#[derive(
clap::ValueEnum, Clone, Default, Debug, Serialize, PartialEq
)]
#[serde(rename_all = "kebab-case")]
enum ExecutionVariants {
#[default]
Classic,

ModelChecking,
}

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
Expand All @@ -17,13 +33,55 @@ struct Args {
/// Number of Iterations
#[arg(short, long)]
iterations: u32,

/// Choose variant of calculation
#[clap(short, long, default_value_t, value_enum)]
variant: ExecutionVariants,
}

#[derive(Tabled)]
struct PrintableStudent {
name: String,
points: u16,
w_seminar: String,
p_seminar: String,
}

fn main() {
let args = Args::parse();
let seminars = import_seminars(&args.seminars_path);
let students = import_students(&args.students_path, &seminars);

let best_iteration = run_algorithm(&students, &seminars, args.iterations, get_default_points());
print!("Points: {}", best_iteration.total_points());

let best_iteration = match args.variant {
ExecutionVariants::Classic => Some(run_algorithm(&students, &seminars, args.iterations, get_default_points())),
ExecutionVariants::ModelChecking => run_model_check(&students, &seminars, get_default_points(), 400)
};
if let Some(bi_unwr) = best_iteration {

let mut students_table_data = Vec::new();

for a in &bi_unwr.assignments {
let ps = PrintableStudent {
name: a.student.name.clone(),
points: a.points,
w_seminar: match a.w_seminar {
Some(s) => s.name.clone(),
None => "Null".to_string()
},
p_seminar: match a.p_seminar {
Some(s) => s.name.clone(),
None => "Null".to_string()
},
};
students_table_data.push(ps);
}

let mut table = Table::new(students_table_data);
table.with(Style::rounded());
print!("{}\n", table.to_string());
print!("Total points: {}\n", style(bi_unwr.total_points()).bold().green());
} else {
print!("{} Try the classic variant or increase the max points.\n", style("No result was found!").bold().yellow())
}
}
Loading

0 comments on commit eb9ce31

Please sign in to comment.