Advertisement
tomdodd4598

Untitled

Oct 23rd, 2021
1,230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.87 KB | None | 0 0
  1. use linked_list::dodd::helpers;
  2. use linked_list::dodd::item::Item;
  3. use linked_list::dodd::node::Node;
  4.  
  5. use num::bigint::BigInt;
  6. use once_cell::sync::OnceCell;
  7. use regex::Regex;
  8.  
  9. use std::io;
  10. use std::str::FromStr;
  11.  
  12. fn trim_newline(string: &mut String) {
  13.     if string.ends_with('\n') {
  14.         string.pop();
  15.         if string.ends_with('\r') {
  16.             string.pop();
  17.         }
  18.     }
  19. }
  20.  
  21. fn is_valid_string(string: &str) -> bool {
  22.     static VALID_REGEX: OnceCell<Regex> = OnceCell::new();
  23.     let regex = VALID_REGEX.get_or_init(
  24.         || Regex::new("^(0|-?[1-9][0-9]*|[A-Za-z][0-9A-Z_a-z]*)$").unwrap()
  25.     );
  26.     regex.is_match(string)
  27. }
  28.  
  29. fn insert_before(val: &String, oth: &Item<String>) -> bool {
  30.     if let Ok(x) = BigInt::from_str(val) {
  31.         if let Ok(y) = BigInt::from_str(&oth.value) {
  32.             return x <= y
  33.         }
  34.     }
  35.     val <= &oth.value
  36. }
  37.  
  38. fn value_equals(item: &Item<String>, val: &String) -> bool {
  39.     &item.value == val
  40. }
  41.  
  42. fn main() {
  43.     let mut start = Node::empty();
  44.    
  45.     let mut begin = true;
  46.     let mut input;
  47.    
  48.     loop {
  49.         if !begin {
  50.             println!();
  51.         }
  52.         else {
  53.             begin = false;
  54.         }
  55.        
  56.         println!("Awaiting input...");
  57.         let mut string = String::new();
  58.         io::stdin().read_line(&mut string).expect("Failed to read line!");
  59.         trim_newline(&mut string);
  60.        
  61.         if string.is_empty() {
  62.             println!("\nProgram terminated!");
  63.             return;
  64.         }
  65.         else if string.starts_with('~') {
  66.             if string.len() == 1 {
  67.                 println!("\nDeleting list...");
  68.                 helpers::remove_all(&mut start);
  69.             }
  70.             else {
  71.                 input = &string[1..];
  72.                 if is_valid_string(input) {
  73.                     println!("\nRemoving item...");
  74.                     helpers::remove_item(&mut start, String::from(input), value_equals);
  75.                 }
  76.                 else {
  77.                     println!("\nCould not parse input!");
  78.                 }
  79.             }
  80.         }
  81.         else if string == "l" {
  82.             println!("\nList print...");
  83.             helpers::print_list(&start);
  84.         }
  85.         else if string == "i" {
  86.             println!("\nIterator print...");
  87.             helpers::print_iterator(&start);
  88.         }
  89.         else if string == "a" {
  90.             println!("\nArray print...");
  91.             helpers::print_array(&start);
  92.         }
  93.         else if string == "r" {
  94.             println!("\nRecursive print...");
  95.             helpers::print_recursive(&start);
  96.         }
  97.         else if is_valid_string(&string[..]) {
  98.             println!("\nInserting item...");
  99.             helpers::insert_item(&mut start, string, insert_before);
  100.         }
  101.         else {
  102.             println!("\nCould not parse input!");
  103.         }
  104.     }
  105. }
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement