diff --git a/src/catalog.rs b/src/catalog.rs new file mode 100644 index 0000000..471280a --- /dev/null +++ b/src/catalog.rs @@ -0,0 +1,50 @@ +// (catalog_name, (username, password)) +pub type CatalogDefinition<'a> = (&'a str, Login<'a>); +pub type Login<'a> = Option<(&'a str, &'a str)>; + +pub static ALIASES: &[(&str, CatalogDefinition)] = &[ + ("DS_WS1617", ("16w-diskrete-strukturen", None)), + ("EIDI_WS1617", ("eidi1-2016", Some(("eidi-2016", "PGdP.16")))), + ("ERA_WS1617", ("era-2016", None)), + + ("GAD_SS17", ("17s-grundlagen-algorithmen-und-datenstrukturen", None)), + ("LA_SS17", ("17s-lineare-algebra-fuer-informatik", None)), + // EIST is on a Nextcloud +]; + +pub static ALIAS_ALIASES: &[(&str, &str)] = &[ + ("DS", "DS_WS1617"), + ("EIDI", "EIDI_WS1617"), + ("ERA", "ERA_WS1617"), + + ("GAD", "GAD_SS17"), + ("LA", "LA_SS17"), +]; + +pub fn resolve_alias(mut query: &str) -> Option { + for &(alias_alias, alias) in ALIAS_ALIASES { + if query == alias_alias { + query = alias; + } + } + + for &(alias_name, def) in ALIASES { + if query == alias_name { + return Some(def); + } + } + + None +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn check_alias_aliases() { + for &(alias, _) in ALIAS_ALIASES { + assert!(resolve_alias(alias).is_some()); + } + } +} diff --git a/src/main.rs b/src/main.rs index e8a755c..7254b27 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,8 @@ extern crate lazy_static; extern crate reqwest; extern crate zip; +mod catalog; + use std::env; use std::process::Command; use std::collections::HashMap; @@ -23,7 +25,7 @@ use reqwest::header::{Cookie, Charset, SetCookie}; use reqwest::header::{ContentDisposition, DispositionType, DispositionParam}; use zip::ZipArchive; -use catalogs::*; +use catalog::*; // TODO: Old "Publish To Go" packages get sweeped from the server and you have to request a new one; // Implement this @@ -31,20 +33,6 @@ use catalogs::*; // TODO: Add aliases with semester postfix so other can contribute aliases; move to separate file // split into files in general? -mod catalogs { - type Login = Option<(&'static str, &'static str)>; - type CatalogDef = (&'static str, Login); - - // (catalog_name, (username, password)) - pub const DS: CatalogDef = ("16w-diskrete-strukturen", None); - pub const EIDI: CatalogDef = ("eidi1-2016", Some(("eidi-2016", "PGdP.16"))); - pub const ERA: CatalogDef = ("era-2016", None); - - pub const GAD: CatalogDef = ("17s-grundlagen-algorithmen-und-datenstrukturen", None); - pub const LA: CatalogDef = ("17s-lineare-algebra-fuer-informatik", None); - // EIST is on a Nextcloud -} - const MAX_RETRIES: u8 = 10; lazy_static! { @@ -266,22 +254,16 @@ fn main() { .takes_value(true)) .get_matches(); - let catalog_def = match matches.value_of("CATALOG_NAME").unwrap() { - "DS" => DS, - "EIDI" => EIDI, - "ERA" => ERA, - "GAD" => GAD, - "LA" => LA, - n => { - if let Some(username) = matches.value_of("username") { - let password = matches.value_of("password").unwrap(); - (n, Some((username, password))) - } else { - (n, None) - } - } + let catalog_name = matches.value_of("CATALOG_NAME").unwrap(); + let login = if let Some(username) = matches.value_of("username") { + let password = matches.value_of("password").unwrap(); + Some((username, password)) + } else { + None }; + let catalog_def = resolve_alias(catalog_name).unwrap_or((catalog_name, login)); + let out_dir = Path::new(matches.value_of("OUTPUT_DIRECTORY").unwrap()); if out_dir.exists() { assert!(out_dir.is_dir());