1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
use wikipedia::Wikipedia;

extern crate wikipedia;

pub fn run(args: &Vec<String>) -> Result<(), String> {
    let flags = get_flags(args);
    let params = get_params(args)?;
    for flag in &flags {
        match parse_flags(flag, &params) {
            Ok(s) => println!("{}", s),
            Err(e) => return Err(e)
        }
    }
    Ok(())
}

/*
    Acceptable flags will likely be:
    -h for help
    -v for version
    -l for link - done
    -c for categories - done
    -r for references - done
    -i for pageid - done
    -t for table of contents - done
    -s (or no flags) searches for a page (if two args are provided, second arg is treated as a section and the command gets section content) - done
 */
fn get_flags(args: &Vec<String>) -> Vec<String> {
    let pre = args.into_iter()
        .filter(|&s| (*s).starts_with("-"))
        .collect::<Vec<&String>>();

    let mut ret: Vec<String> = vec![];
    for i in 0..pre.len() {
        if !pre[i].starts_with("--") {
            for c in pre[i].chars() {
                ret.push(c.to_string());
            }
        } else {
            ret.push(pre[i].replace("--", ""));
        }
    }

    if ret.len() == 0 {
        ret.push(String::from(""));
    }
    ret.retain(|s| s != "-");

    ret
}

fn get_params(args: &Vec<String>) -> Result<Vec<String>, &str> {
    let pre = args.into_iter()
        .filter(|&s| !(*s).starts_with("-"))
        .collect::<Vec<&String>>();

    let mut params = vec![];

    for s in pre {
        params.push(s.to_string());
    }
    params.remove(0);
    if params.len() > 2 {
        Err("Error: too many arguments provided")
    } else {
        Ok(params)
    }
}

fn parse_flags(flag: &String, params: &Vec<String>) -> Result<String, String> {
    let wiki = wikipedia::Wikipedia::<wikipedia::http::default::Client>::default();
    if flag.len() == 0 {
        return if params.len() > 1 {search_section(&wiki, params.get(0), params.last())} else {search_summary(&wiki, params.last())}
    } else {
        return match flag.as_str() {
            "s" | "--summary" => {
                if params.len() > 1 {
                    search_section(&wiki, params.get(0), params.last())
                } else {
                    search_summary(&wiki, params.last())
                }
            },
            "p" | "pageid" => pageid(&wiki, params.last()),
            "t" | "toc" => table_of_contents(&wiki, params.last()),
            "r" | "references" => references(&wiki, params.last()),
            "c" | "categories" => categories(&wiki, params.last()),
            "l" | "link" => link(&wiki, params.last()),
            "v" | "version" => version(),
            "h" | "help" => help(),
            _ => return Err(String::from("Unknown flag provided"))
        };
    }   
}

fn too_few_arguments(query: Option<&String>) -> Result<(), String> {
    if query.is_none() {
        Err(String::from("Error: too few arguments"))
    } else {
        Ok(())
    }
}

fn matchout(input: Result<String, wikipedia::Error>) -> Result<String, String> {
    match input {
        Ok(s) => Ok(s),
        Err(e) => Err(format!("Error: Wikipedia failed to fetch data: {}", e.to_string()))
    }
}

fn search_summary(wiki: &Wikipedia<wikipedia::http::default::Client>, query: Option<&String>) -> Result<String, String> {
    too_few_arguments(query)?;

    let page = wiki.page_from_title(query.unwrap().to_lowercase());

    matchout(page.get_summary())
}

fn search_section(wiki: &Wikipedia<wikipedia::http::default::Client>, title: Option<&String>, section: Option<&String>) -> Result<String, String> {
    too_few_arguments(title)?;
    too_few_arguments(section)?;

    let page = wiki.page_from_title(title.unwrap().to_lowercase());
    let sections = match page.get_sections() {
        Ok(v) => v,
        Err(e) => {return Err(format!("Error: Wikipedia failed to fetch data: {}", e.to_string()));}
    };

    let mut thesection = String::new();
    for s in sections {
        if s.eq_ignore_ascii_case(section.unwrap()) {
            thesection = s;
            break;
        }
    }
    return match page.get_section_content(&thesection) {
        Ok(s) => {
            if let Some(v) = s {
             Ok(v)  
            } else {
                Err(String::from("Error: No section with that name was found"))
            }
        },
        Err(e) => Err(format!("Error: Wikipedia failed to fetch data: {}", e.to_string()))
    }
}

fn pageid(wiki: &Wikipedia<wikipedia::http::default::Client>, query: Option<&String>) -> Result<String, String> {
    too_few_arguments(query)?;

    let page = wiki.page_from_title(query.unwrap().to_lowercase());
    
    matchout(page.get_pageid())
}

fn table_of_contents(wiki: &Wikipedia<wikipedia::http::default::Client>, query: Option<&String>) -> Result<String, String> {
    too_few_arguments(query)?;

    let page = wiki.page_from_title(query.unwrap().to_lowercase());
    
    let mut out = String::from("Sections:\n");
    match page.get_sections() {
        Ok(v) => {
            for s in v {
                out.push_str(&format!("{}\n", s));
            }
        },
        Err(e) => return Err(format!("Error: Wikipedia failed to fetch data: {}", e.to_string())),
    }

    Ok(out)
}

fn references(wiki: &Wikipedia<wikipedia::http::default::Client>, query: Option<&String>) -> Result<String, String> {
    too_few_arguments(query)?;

    let page = wiki.page_from_title(query.unwrap().to_lowercase());

    let mut refs = String::new();
    match page.get_references() {
        Ok(i) => {
            for s in i {
                refs.push_str(&format!("{}\n", s.url));
            }
        },
        Err(e) => return Err(format!("Error: Wikipedia failed to fetch data: {}", e.to_string()))
    }

    Ok(refs)
}

fn categories(wiki: &Wikipedia<wikipedia::http::default::Client>, query: Option<&String>) -> Result<String, String> {
    too_few_arguments(query)?;

    let page = wiki.page_from_title(query.unwrap().to_lowercase());

    let mut cats = String::new();
    match page.get_categories() {
        Ok(i) => {
            for s in i {
                cats.push_str(&format!("{}\n", s.title));
            }
        },
        Err(e) => return Err(format!("Error: Wikipedia failed to fetch data: {}", e.to_string()))
    }

    Ok(cats)
}

fn link(wiki: &Wikipedia<wikipedia::http::default::Client>, query: Option<&String>) -> Result<String, String> {
    let pageid = pageid(wiki, query);
    return match pageid {
        Ok(s) => Ok(format!("http://en.wikipedia.org/?curid={}", s)),
        Err(e) => Err(format!("Failed to retrieve link: {}", e.to_string()))
    }
}

fn version() -> Result<String, String> {
    Ok(format!("qwiki version {}", env!("CARGO_PKG_VERSION")))
}

fn help() -> Result<String, String> {
    Ok(concat!(
        "-s --summary: Gets the summary of the Wikipedia article specified by the argument provided. If two arguments are provided instead, gets the content of the section (specified by the second argument) from the article (specified by the first argument).",
        "This flag is used by default if no flags are provided.\n",
        "-t --toc: Lists all of the sections of the Wikipedia article specified by the argument provided.\n",
        "-r --references: Gets all of the references of the Wikipedia article specified by the argument provided.\n",
        "-c --categories: Gets all of the categories of the Wikipedia article specified by the argument provided.\n",
        "-l --link: Gets a link to the Wikipedia article specified by the argument provided.\n",
        "-p --pageid: Gets the pageid of the Wikipedia article specified by the argument provided.\n",
        "-v --version: Gets the version of qwiki."
    ).to_owned())
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn check_flags() {
        let args = vec![String::from("cow"), String::from("-s"), String::from("-e"), String::from("--moose")];
        let result = get_flags(&args);
        let check = vec![String::from("s"), String::from("e"), String::from("moose")];
        let matching = result.iter().zip(check.iter()).filter(|&(a, b)| a == b).count();
        assert!(matching == result.len() && matching == check.len());
    }

    #[test]
    fn check_flags_long() {
        let args = vec![String::from("cow"), String::from("-s"), String::from("-pe")];
        let result = get_flags(&args);
        let check = vec![String::from("s"), String::from("p"), String::from("e")];
        let matching = result.iter().zip(check.iter()).filter(|&(a, b)| a == b).count();
        assert!(matching == result.len() && matching == check.len());
    }

    #[test]
    fn check_params_ok() {
        let args = vec![String::from("co-w"), String::from("moose"), String::from("-s"), String::from("-e")];
        let result = get_params(&args);
        let check = vec![String::from("moose")];
        assert!(result.is_ok());
        let matching = result.as_ref().unwrap().iter().zip(check.iter()).filter(|&(a, b)| a == b).count();
        assert!(matching == result.unwrap().len() && matching == check.len());
    }

    #[test]
    fn check_params_too_many() {
        let args = vec![String::from("-s"), String::from("cow"), String::from("moose"), String::from("geese"), String::from("jackrabbit"), String::from("-e")];
        let result = get_params(&args);
        assert!(if result.is_err() && result.unwrap_err() == "Error: too many arguments provided" {true} else {false});
    }

    #[test]
    fn check_case() {
        let wiki = wikipedia::Wikipedia::<wikipedia::http::default::Client>::default();
        let test = search_summary(&wiki, Some(&String::from("cristiano ronaldo")));
        assert!(test.is_ok());
    }
}