Commit 196b8157 authored by Robert Czechowski's avatar Robert Czechowski

Move file traversal for contest info from main.rs into contestreader_yaml.rs

parent 9e6b4882
......@@ -15,6 +15,7 @@
use db_objects::{Contest, Task, Taskgroup};
use serde_yaml;
use std::path::Path;
extern crate time;
......@@ -40,7 +41,7 @@ struct ContestYaml {
// The task path is stored relatively to the contest.yaml for easier identificationy
// Concatenation happens in functions::show_task
pub fn parse_yaml(content: &str, filename: &str, directory: &str) -> Option<Contest> {
fn parse_yaml(content: &str, filename: &str, directory: &str) -> Option<Contest> {
let config: ContestYaml = serde_yaml::from_str(&content).unwrap();
use self::time::{strptime, Timespec};
......@@ -118,3 +119,46 @@ pub fn parse_yaml(content: &str, filename: &str, directory: &str) -> Option<Cont
Some(contest)
}
fn read_contest(p: &Path) -> Option<Contest> {
use std::fs::File;
use std::io::Read;
let mut file = File::open(p).unwrap();
let mut contents = String::new();
file.read_to_string(&mut contents).ok()?;
parse_yaml(&contents, p.file_name().to_owned()?.to_str()?, &format!("{}/", p.parent().unwrap().to_str()?))
}
pub fn get_all_contest_info(task_dir: &str) -> Vec<Contest> {
fn walk_me_recursively(p: &Path, contests: &mut Vec<Contest>) {
if let Ok(paths) = std::fs::read_dir(p) {
print!("…");
use std::io::Write;
std::io::stdout().flush().unwrap();
let mut paths: Vec<_> = paths.filter_map(|r| r.ok()).collect();
paths.sort_by_key(|dir| dir.path());
for path in paths {
let p = path.path();
walk_me_recursively(&p, contests);
}
}
if p.file_name().unwrap().to_string_lossy().to_string().ends_with(".yaml") {
read_contest(p).map(|contest| contests.push(contest));
};
}
let mut contests = Vec::new();
match std::fs::read_dir(task_dir) {
Err(why) => println!("Error opening tasks directory! {:?}", why.kind()),
Ok(paths) => {
for path in paths {
walk_me_recursively(&path.unwrap().path(), &mut contests);
}
}
};
contests
}
......@@ -57,59 +57,11 @@ mod db_objects;
mod webfw_iron;
use db_conn::{MedalConnection, MedalObject};
use db_objects::*;
use helpers::SetPassword;
use webfw_iron::start_server;
use config::Config;
use std::path::Path;
fn read_contest(p: &Path) -> Option<Contest> {
use std::fs::File;
use std::io::Read;
let mut file = File::open(p).unwrap();
let mut contents = String::new();
file.read_to_string(&mut contents).ok()?;
contestreader_yaml::parse_yaml(&contents,
p.file_name().to_owned()?.to_str()?,
&format!("{}/", p.parent().unwrap().to_str()?))
}
fn get_all_contest_info(task_dir: &str) -> Vec<Contest> {
fn walk_me_recursively(p: &Path, contests: &mut Vec<Contest>) {
if let Ok(paths) = std::fs::read_dir(p) {
print!("…");
use std::io::Write;
std::io::stdout().flush().unwrap();
let mut paths: Vec<_> = paths.filter_map(|r| r.ok()).collect();
paths.sort_by_key(|dir| dir.path());
for path in paths {
let p = path.path();
walk_me_recursively(&p, contests);
}
}
if p.file_name().unwrap().to_string_lossy().to_string().ends_with(".yaml") {
read_contest(p).map(|contest| contests.push(contest));
};
}
let mut contests = Vec::new();
match std::fs::read_dir(task_dir) {
Err(why) => println!("Error opening tasks directory! {:?}", why.kind()),
Ok(paths) => {
for path in paths {
walk_me_recursively(&path.unwrap().path(), &mut contests);
}
}
};
contests
}
fn refresh_all_contests<C>(conn: &mut C)
where C: MedalConnection,
db_objects::Contest: db_conn::MedalObject<C>
......@@ -117,7 +69,7 @@ fn refresh_all_contests<C>(conn: &mut C)
conn.reset_all_contest_visibilities();
conn.reset_all_taskgroup_visibilities();
let v = get_all_contest_info("tasks/");
let v = contestreader_yaml::get_all_contest_info("tasks/");
for mut contest_info in v {
contest_info.save(conn);
......
use super::*;
use db_objects::{Contest, Task, Taskgroup};
use reqwest::StatusCode;
use std::path::Path;
fn addsimpleuser(conn: &mut rusqlite::Connection, username: String, password: String, is_t: bool, is_a: bool) {
let mut test_user = conn.new_session("");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment