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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
use cursive::traits::{Boxable, Scrollable};
use cursive::views::{Dialog, EditView, SelectView};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Stores the State of the program and the user's data. Gets serialized after every change.
#[derive(Serialize, Deserialize)]
pub struct State {
    pub reviews: HashMap<usize, u8>,
}

pub struct ChapterInfo {
    pub human_readable: &'static str,
    extra_info: &'static str,
}

impl ChapterInfo {
    /// Used to get a human readable format of extra_info data associated with a ChapterInfo struct
    pub fn chapter_resources_string(&self) -> String {
        if self.extra_info.is_empty() {
            String::from("Make sure to look at the Rust by Example online book for some examples!")
        } else {
            format!("Check out these helpful resources:\n\n{}", self.extra_info)
        }
    }
}

/// Stores the name of all the rust book chapters. The indexes of each chapter string are referenced throughout the program.
pub const CHAPTERS: [ChapterInfo; 97] = [
    ChapterInfo {
        human_readable: "1. Getting Started",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "1.1. Installation",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "1.2. Hello, World!",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "1.3. Hello, Cargo!",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "2. Programming a Guessing Game",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "3. Common Programming Concepts",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "3.1. Variables and Mutability",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "3.2. Data Types",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "3.3. Functions",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "3.4. Comments",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "3.5. Control Flow",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "4. Understanding Ownership",
        extra_info: "https://medium.com/@thomascountz/ownership-in-rust-part-1-112036b1126b\nhttps://dev.to/domcorvasce/learning-rust-1-ownership-g0",
    },
    ChapterInfo {
        human_readable: "4.1. What is Ownership?",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "4.2. References and Borrowing",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "4.3. The Slice Type",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "5. Using Structs to Structure Related Data",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "5.1. Defining and Instantiating Structs",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "5.2. An Example Program Using Structs",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "5.3. Method Syntax",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "6. Enums and Pattern Matching",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "6.1. Defining an Enum",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "6.2. The match Control Flow Operator",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "6.3. Concise Control Flow with if let",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "7. Managing Growing Projects with Packages, Crates, and Modules",
        extra_info: "https://stevedonovan.github.io/rust-gentle-intro/4-modules.html",
    },
    ChapterInfo {
        human_readable: "7.1. Packages and Crates",
        extra_info: "https://stevedonovan.github.io/rust-gentle-intro/4-modules.html",
    },
    ChapterInfo {
        human_readable: "7.2. Defining Modules to Control Scope and Privacy",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "7.3. Paths for Referring to an Item in the Module Tree",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "7.4. Bringing Paths Into Scope with the use Keyword",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "7.5. Separating Modules into Different Files",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "8. Common Collections",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "8.1. Storing Lists of Values with Vectors",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "8.2. Storing UTF-8 Encoded Text with Strings",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "8.3. Storing Keys with Associated Values in Hash Maps",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "9. Error Handling",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "9.1. Unrecoverable Errors with panic!",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "9.2. Recoverable Errors with Result",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "9.3. To panic! or Not To panic!",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "10. Generic Types, Traits, and Lifetimes",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "10.1. Generic Data Types",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "10.2. Traits: Defining Shared Behavior",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "10.3. Validating References with Lifetimes",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "11. Writing Automated Tests",
        extra_info: "https://medium.com/@KalROFL/plp-rust-agjks-395d1d870432",
    },
    ChapterInfo {
        human_readable: "11.1. How to Write Tests",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "11.2. Controlling How Tests Are Run",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "11.3. Test Organization",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "12. An I/O Project: Building a Command Line Program",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "12.1. Accepting Command Line Arguments",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "12.2. Reading a File",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "12.3. Refactoring to Improve Modularity and Error Handling",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "12.4. Developing the Library’s Functionality with Test Driven Development",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "12.5. Working with Environment Variables",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "12.6. Writing Error Messages to Standard Error Instead of Standard Output",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "13. Functional Language Features: Iterators and Closures",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "13.1. Closures: Anonymous Functions that Can Capture Their Environment",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "13.2. Processing a Series of Items with Iterators",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "13.3. Improving Our I/O Project",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "13.4. Comparing Performance: Loops vs. Iterators",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "14. More about Cargo and Crates.io",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "14.1. Customizing Builds with Release Profiles",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "14.2. Publishing a Crate to Crates.io",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "14.3. Cargo Workspaces",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "14.4. Installing Binaries from Crates.io with cargo install",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "14.5. Extending Cargo with Custom Commands",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "15. Smart Pointers",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "15.1. Using Box to Point to Data on the Heap",
        extra_info: "",
    },
    ChapterInfo {
        human_readable:
            "15.2. Treating Smart Pointers Like Regular References with the Deref Trait",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "15.3. Running Code on Cleanup with the Drop Trait",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "15.4. Rc, the Reference Counted Smart Pointer",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "15.5. RefCell and the Interior Mutability Pattern",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "15.6. Reference Cycles Can Leak Memory",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "16. Fearless Concurrency",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "16.1. Using Threads to Run Code Simultaneously",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "16.2. Using Message Passing to Transfer Data Between Threads",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "16.3. Shared-State Concurrency",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "16.4. Extensible Concurrency with the Sync and Send Traits",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "17. Object Oriented Programming Features of Rust",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "17.1. Characteristics of Object-Oriented Languages",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "17.2. Using Trait Objects That Allow for Values of Different Types",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "17.3. Implementing an Object-Oriented Design Pattern",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "18. Patterns and Matching",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "18.1. All the Places Patterns Can Be Used",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "18.2. Refutability: Whether a Pattern Might Fail to Match",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "18.3. Pattern Syntax",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "19. Advanced Features",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "19.1. Unsafe Rust",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "19.2. Advanced Traits",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "19.3. Advanced Types",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "19.4. Advanced Functions and Closures",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "19.5. Macros",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "20. Final Project: Building a Multithreaded Web Server",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "20.1. Building a Single-Threaded Web Server",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "20.2. Turning Our Single-Threaded Server into a Multithreaded Server",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "20.3. Graceful Shutdown and Cleanup",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "21.1. A - Keywords",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "21.2. B - Operators and Symbols",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "21.3. C - Derivable Traits",
        extra_info: "",
    },
    ChapterInfo {
        human_readable: "21.4. D - Useful Development Tools",
        extra_info: "",
    },
];

pub mod screens;

pub mod state;

/// Used to find the index of a user-inputted chapter name in the `CHAPTERS` array. Will not panic if it does not exist, returns `None` instead.
///
/// # Examples
/// ```
/// use svalbard::{CHAPTERS, find_chapter_index};
/// assert_eq!(Some(0), find_chapter_index("1. Getting Started", CHAPTERS));
/// ```
pub fn find_chapter_index(search_string: &str, chapters_list: [ChapterInfo; 97]) -> Option<usize> {
    for (index, chapter) in chapters_list.iter().enumerate() {
        if chapter.human_readable.starts_with(search_string) {
            return Some(index);
        }
    }

    None
}

/// Finds the lowest ranked chapter and returns the an Option with the index of the chapter in the `CHAPTERS` array.
///
/// # Examples
/// ```
/// use svalbard::{State, smallest_value_in_hashmap};
/// use std::collections::HashMap;
///
/// let mut hashmap_example = HashMap::new();
/// hashmap_example.insert(1, 10);
/// hashmap_example.insert(90, 1);
///
/// let lowest_ranked_chapter = smallest_value_in_hashmap(&hashmap_example).unwrap();
/// assert_eq!(lowest_ranked_chapter, 90);
/// ```

/// Returns the key corresponding to the SMALLEST value in a Hashmap.
pub fn smallest_value_in_hashmap(hashmap: &HashMap<usize, u8>) -> Option<usize> {
    let mut entry = (&0usize, &255);

    for (key, value) in hashmap {
        if value < entry.1 {
            entry.0 = key;
            entry.1 = value;
        }
    }

    // If tuple is still default state
    if entry == (&0usize, &255) {
        None
    } else {
        // If tuple changed:
        Some(*entry.0)
    }
}

#[cfg(test)]
mod tests {
    use crate::{find_chapter_index, smallest_value_in_hashmap, State, CHAPTERS};

    #[test]
    fn chapter_search() {
        assert_eq!(Some(94), find_chapter_index("21.2", CHAPTERS));
    }

    #[test]
    fn unknown_chapter_search() {
        assert_eq!(None, find_chapter_index("9999.9", CHAPTERS));
    }

    #[test]
    fn smallest_in_hashmap() {
        use std::collections::HashMap;

        let mut hashmap_example = HashMap::new();
        hashmap_example.insert(21, 9);
        hashmap_example.insert(44, 3);
        hashmap_example.insert(12, 7);
        hashmap_example.insert(99, 6);

        assert_eq!(smallest_value_in_hashmap(&hashmap_example).unwrap(), 44);

        hashmap_example.insert(90, 1);

        assert_eq!(smallest_value_in_hashmap(&hashmap_example).unwrap(), 90);
    }

    #[test]
    fn empty_smallest_in_hashmap() {
        use std::collections::HashMap;

        let hashmap_example = HashMap::new();
        assert_eq!(smallest_value_in_hashmap(&hashmap_example), None);
    }
}