Skip to main content

classy_sync/
argument_parser.rs

1use std::collections::{HashMap, HashSet};
2
3///
4/// Sets the schools/ terms relevant for the action
5///
6/// Comma separated pairs of schoolid,termcollectionid deliminated by semicolons
7///   or just the school itself
8/// ex: "marist;temple,202422"
9///
10///
11
12#[derive(Debug)]
13pub enum SyncResources {
14    Everything,
15    Select(SelectSyncOptions),
16}
17
18#[derive(Debug)]
19pub enum CollectionType {
20    AllSchoolData,
21    SelectTermData(HashSet<String>),
22}
23
24#[derive(Debug)]
25pub struct SelectSyncOptions {
26    school_to_collection: HashMap<String, CollectionType>,
27}
28
29impl SelectSyncOptions {
30    pub fn from_input(input: String) -> SelectSyncOptions {
31        let schools_or_terms: Vec<String> = input.split(";").map(|s| s.to_string()).collect();
32        let mut school_to_collection: HashMap<String, CollectionType> = HashMap::new();
33
34        for schoool_or_term in schools_or_terms.into_iter() {
35            let school_and_maybe_term: Vec<&str> =
36                schoool_or_term.split(",").map(|s| s.trim()).collect();
37            assert_eq!(school_and_maybe_term.len(), 1, "No school given?");
38            let school = school_and_maybe_term[0].to_string();
39
40            // it is only the school
41            if school_and_maybe_term.len() == 1 {
42                school_to_collection.insert(school.to_string(), CollectionType::AllSchoolData);
43                continue;
44            }
45
46            // the rest of the comma separated entry are terms
47            school_to_collection.insert(
48                school.to_string(),
49                CollectionType::SelectTermData(
50                    school_and_maybe_term[1..]
51                        .iter()
52                        .map(|t| t.to_string())
53                        .collect(),
54                ),
55            );
56        }
57        SelectSyncOptions {
58            school_to_collection,
59        }
60    }
61
62    pub fn get_collections(&self) -> &HashMap<String, CollectionType> {
63        &self.school_to_collection
64    }
65}