del
This commit is contained in:
65
src/main.rs
65
src/main.rs
@@ -1,68 +1,3 @@
|
||||
use itertools::Itertools;
|
||||
|
||||
mod util;
|
||||
|
||||
fn encode(group_size: usize, filler_char: char, input: String) -> String {
|
||||
let mut input = util::remove_whitespace(util::remove_punctuation(input));
|
||||
let len = input.chars().count();
|
||||
if len % group_size != 0 {
|
||||
for _ in 0..(group_size - len % group_size) {
|
||||
input.push(filler_char);
|
||||
}
|
||||
}
|
||||
|
||||
let mut result = String::new();
|
||||
for chunk in &input.chars().rev().chunks(group_size) {
|
||||
for c in chunk {
|
||||
result.push(c);
|
||||
}
|
||||
result.push(' ');
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
fn decode(_group_size: usize, filler_char: char, input: String) -> String {
|
||||
let result = util::remove_whitespace(util::remove_punctuation(input))
|
||||
.chars()
|
||||
.rev()
|
||||
.collect::<String>();
|
||||
|
||||
result.trim_end_matches(filler_char).to_string()
|
||||
}
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
let group_size = dialoguer::Input::<usize>::new()
|
||||
.with_prompt("Enter group size")
|
||||
.validate_with(|input: &usize| {
|
||||
if input > &1 {
|
||||
Ok(())
|
||||
} else {
|
||||
Err("Must be greater than 1".to_string())
|
||||
}
|
||||
})
|
||||
.interact_text()?;
|
||||
let filler_char = dialoguer::Input::<char>::new()
|
||||
.with_prompt("Filler character")
|
||||
.default('A')
|
||||
.interact_text()?;
|
||||
let input = dialoguer::Input::<String>::new()
|
||||
.with_prompt("Enter input")
|
||||
.interact_text()?;
|
||||
let mode = dialoguer::Select::new()
|
||||
.with_prompt("Mode")
|
||||
.item("Encode")
|
||||
.item("Decode")
|
||||
.default(0)
|
||||
.interact()?;
|
||||
|
||||
let result = match mode {
|
||||
0 => encode(group_size, filler_char, input),
|
||||
1 => decode(group_size, filler_char, input),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
println!("Result: {}", result);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
pub fn remove_whitespace(input: String) -> String {
|
||||
input.chars().filter(|c| !c.is_whitespace()).collect()
|
||||
}
|
||||
|
||||
pub fn remove_punctuation(input: String) -> String {
|
||||
input.chars().filter(|c| !c.is_ascii_punctuation()).collect()
|
||||
}
|
||||
Reference in New Issue
Block a user