Skip to content

Commit

Permalink
adds fetch challenge info flags and functions
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbrain-za committed Dec 9, 2023
1 parent 0c401b2 commit c8715af
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
24 changes: 24 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,30 @@ impl RunMode {
std::process::exit(0);
}

if args.contains(&String::from("-g")) || args.contains(&String::from("--get")) {
let challenges = Challenges::new();

let index = args.iter().position(|r| r == "-g").unwrap();
let c = args.get(index + 1);
if let Some(c) = c {
for challenge in challenges.challenges.iter() {
if challenge.command == *c {
let json =
serde_json::to_string(&challenge).expect("Error getting challenges");
print!("{}", json);
std::process::exit(0);
}
}
println!("Unknown challenge: {}", c);
std::process::exit(0);
}

let open_challenges = challenges.get_open_challenges();
let json = serde_json::to_string(&open_challenges).expect("Error getting challenges");
print!("{}", json);
std::process::exit(0);
}

if args.contains(&String::from("--version")) {
println!("judge {}", env!("CARGO_PKG_VERSION"));
std::process::exit(0);
Expand Down
60 changes: 58 additions & 2 deletions src/generator/challenges.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use super::{Generator, G2331, G2332, G2333, G2334, G2411, G2412, G2413, G2414};
use crate::settings;
use log::debug;
use std::fmt::Display;

#[derive(Debug, Clone)]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct Challenge {
pub name: String,
pub command: String,
Expand Down Expand Up @@ -81,7 +82,62 @@ impl Challenges {
}

pub fn get_challenge(&self, challenge: &str) -> Option<&Challenge> {
self.challenges.iter().find(|c| c.command == challenge)
let challenge = match self.challenges.iter().find(|c| c.command == challenge) {
Some(c) => c,
None => {
log::error!("No such challenge: {}", challenge);
return None;
}
};

let settings = match settings::Settings::load(None) {
Ok(settings) => settings,
Err(e) => {
log::error!("Failed to load settings: {}", e);
return None;
}
};

match settings.allowed_to_run(&challenge.command) {
Ok(b) => {
if b {
Some(challenge)
} else {
None
}
}
Err(e) => {
log::error!("Failed to check if challenge is allowed: {}", e);
None
}
}
}

pub fn get_open_challenges(&self) -> Vec<&Challenge> {
let settings = match settings::Settings::load(None) {
Ok(settings) => settings,
Err(e) => {
log::error!("Failed to load settings: {}", e);
return vec![];
}
};

let mut open_challenges = vec![];

for challenge in &self.challenges {
match settings.allowed_to_run(&challenge.command) {
Ok(b) => {
if b {
open_challenges.push(challenge);
}
}
Err(e) => {
log::error!("Failed to check if challenge is allowed: {}", e);
}
}
}

open_challenges
}

pub fn make_generator(&self, challenge: &str, test: bool) -> Option<Box<dyn Generator>> {
Expand Down

0 comments on commit c8715af

Please sign in to comment.