You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
1.9 KiB
81 lines
1.9 KiB
use clap::{Parser, ValueEnum}; |
|
|
|
#[derive(ValueEnum, Debug, Clone)] |
|
enum System { |
|
#[value(name = "major-en")] |
|
MajorEn, |
|
#[value(name = "major-pl")] |
|
MajorPl, |
|
} |
|
|
|
trait SystemEncoder { |
|
fn encode(&self, word: &str) -> String; |
|
} |
|
|
|
struct MajorEncoder { |
|
dict: String, // TODO |
|
} |
|
|
|
impl SystemEncoder for MajorEncoder { |
|
fn encode(&self, word: &str) -> String { |
|
let num_word: String = word |
|
.chars() |
|
.map(|c| c.to_digit(10).unwrap_or(0).to_string()) |
|
.collect(); |
|
format!("{}_{} -> {}", word, self.dict, num_word) |
|
} |
|
} |
|
|
|
fn create_encoder(system: &System) -> Box<dyn SystemEncoder> { |
|
match system { |
|
System::MajorPl => Box::new(MajorEncoder { |
|
dict: String::from("dict-major-pl"), |
|
}), |
|
System::MajorEn => Box::new(MajorEncoder { |
|
dict: String::from("dict-major-en"), |
|
}), |
|
} |
|
} |
|
|
|
#[derive(Parser)] |
|
#[command(author, version, about)] |
|
struct CliArgs { |
|
/// System name |
|
#[arg(short, long, default_value = "major-pl")] |
|
system: System, |
|
|
|
/// Encode given word |
|
#[arg(short, long)] |
|
encode: Option<String>, |
|
|
|
/// List supported systems |
|
#[arg(long)] |
|
list_systems: bool, |
|
} |
|
|
|
fn main() -> Result<(), Box<dyn std::error::Error>> { |
|
let args = CliArgs::parse(); |
|
|
|
if args.list_systems { |
|
println!("Supported systems:"); |
|
for system in System::value_variants() { |
|
if let Some(possible_value) = system.to_possible_value() { |
|
println!("- {}", possible_value.get_name()); |
|
} |
|
} |
|
return Ok(()); |
|
} |
|
|
|
if let Some(word) = args.encode { |
|
println!( |
|
"Encoding {} with system {:?}...", |
|
word, |
|
args.system.to_possible_value().unwrap().get_name() |
|
); |
|
let encoder = create_encoder(&args.system); |
|
let encoded_word = encoder.encode(&word); |
|
println!("Encoded: [{}]", encoded_word); |
|
} |
|
|
|
Ok(()) |
|
}
|
|
|