fully engineered cli

a
This commit is contained in:
Christoph J. Scherr 2023-05-03 13:36:59 +02:00
parent 2e2e19fda5
commit 42571e7a0e
Signed by: PlexSheep
GPG Key ID: 25B4ACF7D88186CC
2 changed files with 60 additions and 20 deletions

View File

@ -5,11 +5,11 @@
use pyo3::prelude::*; use pyo3::prelude::*;
#[pyfunction] #[pyfunction]
pub fn rotl32 (value: u32, count: i32) -> u32 { pub fn rotl32 (value: u32, count: u32) -> u32 {
value.rotate_left(count as u32) value.rotate_left(count as u32)
} }
#[pyfunction] #[pyfunction]
pub fn rotr32 (value: u32, count: i32) -> u32 { pub fn rotr32 (value: u32, count: u32) -> u32 {
value.rotate_right(count as u32) value.rotate_right(count as u32)
} }

View File

@ -1,14 +1,14 @@
mod binary; mod binary;
mod modular_exponentiation; mod modular_exponentiation;
use std::{str::{FromStr, Matches}, fmt::Debug}; use std::{str::FromStr, fmt::Debug};
use modular_exponentiation::modular_exponentiation; use modular_exponentiation::modular_exponentiation;
use clap::{Args, Parser, Subcommand, ValueEnum}; use clap::{Args, Parser, Subcommand};
use num_bigint; use num_bigint;
/*************************************************************************************************/
// This is just structures for parsing Cli args
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)] #[command(author, version, about, long_about = None)]
struct Cli { struct Cli {
@ -25,7 +25,8 @@ struct Cli {
enum Commands { enum Commands {
/// Use math functions /// Use math functions
Math(MathCommand), Math(MathCommand),
Binary /// Use binary functions
Binary(BinaryCommand)
} }
#[derive(Args, Clone, Debug, PartialEq, Eq)] #[derive(Args, Clone, Debug, PartialEq, Eq)]
@ -34,39 +35,78 @@ struct MathCommand {
action: MathActions action: MathActions
} }
#[derive(Args, Clone, Debug, PartialEq, Eq)]
struct BinaryCommand {
#[command(subcommand)]
action: BinaryActions
}
#[derive(Subcommand, Clone, Debug, PartialEq, Eq)] #[derive(Subcommand, Clone, Debug, PartialEq, Eq)]
enum MathActions { enum MathActions {
#[command(name="modexp")] #[command(name="modexp")]
ModExp(ModExpArgs), Modexp(ModexpArgs),
} }
#[derive(Args, Clone, Debug, PartialEq, Eq)] #[derive(Args, Clone, Debug, PartialEq, Eq)]
struct ModExpArgs { struct ModexpArgs {
Base: String, base: String,
Exp: String, exp: String,
Field: String field: String
} }
#[derive(Subcommand, Clone, Debug, PartialEq, Eq)]
enum BinaryActions {
/// bit rotation/circular shifting (only 32bit)
#[command(name="rotate")]
Rotate(RotateArgs),
}
#[derive(Args, Clone, Debug, PartialEq, Eq)]
struct RotateArgs {
#[arg(short, long, default_value_t = false)]
left: bool,
base: u32,
shift_width: u32,
}
/*************************************************************************************************/
pub fn main() { pub fn main() {
let args = Cli::parse(); let args = Cli::parse();
match args.command { match args.command {
Commands::Math(action) => { Commands::Math(action) => {
match action.action { match action.action {
MathActions::ModExp(mod_exp_args) => { MathActions::Modexp(mod_exp_args) => {
let b = num_bigint::BigInt::from_str(&mod_exp_args.Base.as_str()).expect("a"); let b = num_bigint::BigInt::from_str(&mod_exp_args.base.as_str()).expect("a");
let e = num_bigint::BigInt::from_str(&mod_exp_args.Exp.as_str()).expect("a"); let e = num_bigint::BigInt::from_str(&mod_exp_args.exp.as_str()).expect("a");
let f = num_bigint::BigInt::from_str(&mod_exp_args.Field.as_str()).expect("a"); let f = num_bigint::BigInt::from_str(&mod_exp_args.field.as_str()).expect("a");
let r = modular_exponentiation(b.clone(), e, f); let result = modular_exponentiation(b.clone(), e, f);
if args.machine { if args.machine {
print!("{}\n", r) println!("{}", result)
} }
else { else {
print!("res is {}\n", r) println!("result is {}", result)
} }
} }
} }
} }
Commands::Binary => { Commands::Binary(action) => {
match action.action {
BinaryActions::Rotate(bin_rot_args) => {
let result: u32;
if bin_rot_args.left {
result = binary::rotl32(bin_rot_args.base, bin_rot_args.shift_width);
}
else {
result = binary::rotl32(bin_rot_args.base, bin_rot_args.shift_width);
}
if args.machine {
println!("{}", result)
}
else {
println!("result is {}", result)
}
}
}
} }