Removed " character from allowed_chars, allow second optional argument for disallowed characters, error handling for missing or incorrect arguments
This commit is contained in:
		@@ -1,6 +1,6 @@
 | 
				
			|||||||
[package]
 | 
					[package]
 | 
				
			||||||
name = "rust_pass"
 | 
					name = "rust_pass"
 | 
				
			||||||
version = "0.1.0"
 | 
					version = "1.0.0"
 | 
				
			||||||
edition = "2021"
 | 
					edition = "2021"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 | 
					# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										35
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										35
									
								
								src/main.rs
									
									
									
									
									
								
							@@ -1,11 +1,39 @@
 | 
				
			|||||||
use std::env;
 | 
					use std::env;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn main() {
 | 
					fn main() {
 | 
				
			||||||
    // Take in two command line arguments, one i32 and one str
 | 
					    // Take in two command line arguments, one i32 and one optional String
 | 
				
			||||||
    let args: Vec<String> = env::args().collect();
 | 
					    let args: Vec<String> = env::args().collect();
 | 
				
			||||||
    let new_password_length: i32 = args[1].parse().unwrap();
 | 
					    if args.len() < 2 { // Make sure at least one argument is provided
 | 
				
			||||||
    let allowed_chars: &str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-=`~!@#$%^&*()_+[]\\{}|;':\",./<>?";
 | 
					        println!("Error: First argument must be a valid integer (Ex. 32)");
 | 
				
			||||||
 | 
					        println!("Argument not supplied");
 | 
				
			||||||
 | 
					        return;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let new_password_length: Option<i32> = args[1].parse().ok();
 | 
				
			||||||
 | 
					    if new_password_length.is_none() { // Make sure i32 parsed correctly
 | 
				
			||||||
 | 
					        println!("Error: First argument must be a valid integer (Ex. 32)");
 | 
				
			||||||
 | 
					        return;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Disallowed_chars is an optional argument
 | 
				
			||||||
 | 
					    let disallowed_chars: Option<&String> = args.get(2);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let mut allowed_chars: String = String::from("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-=`~!@#$%^&*()_+[]\\{}|;':,./<>?");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Remove disallowed characters from allowed_characters if disallowed_chars is specified
 | 
				
			||||||
 | 
					    if let Some(disallowed_chars) = disallowed_chars {
 | 
				
			||||||
 | 
					        for disallowed_char in disallowed_chars.chars() {
 | 
				
			||||||
 | 
					            allowed_chars = allowed_chars.replace(disallowed_char, "");
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if allowed_chars.len() == 0 { // Make sure there are still characters left to use
 | 
				
			||||||
 | 
					            println!("Error: You have disallowed all characters");
 | 
				
			||||||
 | 
					            println!("The allowed character set includes: abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-=`~!@#$%^&*()_+[]\\{{}}|;':,./<>?");
 | 
				
			||||||
 | 
					            return;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if let Some(new_password_length) = new_password_length { // Unwrap Option in i32
 | 
				
			||||||
        // Generate a random string of length n
 | 
					        // Generate a random string of length n
 | 
				
			||||||
        let mut new_password = String::new();
 | 
					        let mut new_password = String::new();
 | 
				
			||||||
        for _ in 0..new_password_length {
 | 
					        for _ in 0..new_password_length {
 | 
				
			||||||
@@ -19,3 +47,4 @@ fn main() {
 | 
				
			|||||||
        // Print the new password
 | 
					        // Print the new password
 | 
				
			||||||
        println!("{}", new_password);
 | 
					        println!("{}", new_password);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user