diff --git a/src/main.rs b/src/main.rs index 4cd3f65..ce7e7d7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -55,13 +55,33 @@ impl Display for CharacterSet { } #[derive(Debug, Clone, ValueEnum)] -enum BranchType { +enum WhenFileExists { + Exit, + Error, + Override, + + CheckContent +} + +impl Display for WhenFileExists { + fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result { + match self { + Self::Exit => write!(formatter, "Exit"), + Self::Error => write!(formatter, "Error"), + Self::Override => write!(formatter, "Override"), + Self::CheckContent => write!(formatter, "CheckInvalidContent"), + } + } +} + +#[derive(Debug, Clone, ValueEnum)] +enum WhenFileContentIsInvalid { Exit, Error, Override, } -impl Display for BranchType { +impl Display for WhenFileContentIsInvalid { fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result { match self { Self::Exit => write!(formatter, "Exit"), @@ -71,6 +91,7 @@ impl Display for BranchType { } } + /// Generate random strings and put them in a file #[derive(Parser, Debug)] #[command(author, version, about, long_about = None)] @@ -89,11 +110,11 @@ struct Arguments { /// What to do when a file already exists at the given path #[arg(long, env, default_value = "error")] - when_file_exists: BranchType, + when_file_exists: WhenFileExists, /// What to do when the content of a file at the given path does not match the character set or length #[arg(long, env, default_value = "error")] - when_file_content_does_not_match: BranchType, + when_file_content_is_invalid: WhenFileContentIsInvalid, } fn main() -> io::Result<()> { @@ -118,49 +139,49 @@ fn main() -> io::Result<()> { if exists { match arguments.when_file_exists { - BranchType::Exit => { + WhenFileExists::Exit => { log::info!("File exists, exiting."); process::exit(0); }, - BranchType::Error => { + WhenFileExists::Error => { log::error!("File exists, exiting."); process::exit(1); - } - BranchType::Override => (), - }; + }, + WhenFileExists::CheckContent => { + let content = fs::read_to_string(path.clone().unwrap())?; - let content = fs::read_to_string(path.clone().unwrap())?; + let non_matching_characters = character_set.non_matching_characters(&content); + let content_length = content.len(); - let non_matching_characters = character_set.non_matching_characters(&content); - let content_length = content.len(); - - if non_matching_characters.is_empty() && content_length == length { - log::debug!("File exists, matches character set and length, exiting."); - - return Ok(()); - } else { - let found_non_matching = non_matching_characters.iter().take(5).collect::(); - let found_non_matching = escape_string::escape(found_non_matching.as_str()); - let found_non_matching = if non_matching_characters.len() > 5 { - found_non_matching + "..." - } else { - found_non_matching - }; - - match arguments.when_file_content_does_not_match { - BranchType::Exit => { - log::info!("File exists, does not match character set (found [{}]) or length (found {}), exiting.", found_non_matching, content_length); + if non_matching_characters.is_empty() && content_length == length { + log::debug!("File exists, valid character set and length, exiting."); process::exit(0); - }, - BranchType::Error => { - log::error!("File exists, does not match character set (found [{}]) or length (found {}), exiting.", found_non_matching, content_length); - process::exit(1); - }, - BranchType::Override => { - log::debug!("File exists, does not match character set (found [{}]) or length (found {}).", found_non_matching, content_length); - }, - }; - } + } else { + let found_non_matching = non_matching_characters.iter().take(5).collect::(); + let found_non_matching = escape_string::escape(found_non_matching.as_str()); + let found_non_matching = if non_matching_characters.len() > 5 { + found_non_matching + "..." + } else { + found_non_matching + }; + + match arguments.when_file_content_is_invalid { + WhenFileContentIsInvalid::Exit => { + log::info!("File exists, invalid character set (found [{}]) or length (found {}), exiting.", found_non_matching, content_length); + process::exit(0); + }, + WhenFileContentIsInvalid::Error => { + log::error!("File exists, invalid character set (found [{}]) or length (found {}), exiting.", found_non_matching, content_length); + process::exit(1); + }, + WhenFileContentIsInvalid::Override => { + log::debug!("File exists, invalid character set (found [{}]) or length (found {}).", found_non_matching, content_length); + }, + }; + } + }, + WhenFileExists::Override => (), + }; } log::debug!("Generating using {} with length {}", character_set, length);