From 5f22690ef7d98f3e7f60a57a76f316fd3d644831 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Wed, 26 Nov 2025 01:38:11 +0200 Subject: [PATCH] feat: add CipherContext struct --- cipher-factory/src/context.rs | 32 +++++++++++++++++++++++++++ cipher-factory/src/lib.rs | 5 ++++- cli/src/args.rs | 14 +++++++++++- cli/src/main.rs | 41 +++++------------------------------ 4 files changed, 55 insertions(+), 37 deletions(-) create mode 100644 cipher-factory/src/context.rs diff --git a/cipher-factory/src/context.rs b/cipher-factory/src/context.rs new file mode 100644 index 0000000..7891285 --- /dev/null +++ b/cipher-factory/src/context.rs @@ -0,0 +1,32 @@ +use crate::{Algorithm, OperationChoice, OutputFormat}; +use cipher_core::{BlockCipher, CipherResult}; + +pub struct CipherContext { + pub algorithm: Algorithm, + pub operation: OperationChoice, + pub key: String, + pub input_text: String, + pub output_format: OutputFormat, +} + +impl CipherContext { + pub fn process(&self) -> CipherResult { + let text_bytes = self.algorithm.parse_text(&self.input_text)?; + let cipher = self.algorithm.new_cipher(&self.key)?; + self.execute(cipher.as_ref(), &text_bytes) + } + + fn execute(&self, cipher: &dyn BlockCipher, text_bytes: &[u8]) -> CipherResult { + match self.operation { + OperationChoice::Encrypt => { + let ciphertext = cipher.encrypt(text_bytes)?; + Ok(format!("{ciphertext:X}")) + } + OperationChoice::Decrypt => { + let plaintext = cipher.decrypt(text_bytes)?; + let output = self.output_format.to_string(&plaintext); + Ok(output) + } + } + } +} diff --git a/cipher-factory/src/lib.rs b/cipher-factory/src/lib.rs index 2cee782..2bfd4fb 100644 --- a/cipher-factory/src/lib.rs +++ b/cipher-factory/src/lib.rs @@ -1,5 +1,8 @@ mod algorithm; +mod context; mod operation; mod output; -pub use {algorithm::Algorithm, operation::OperationChoice, output::OutputFormat}; +pub use { + algorithm::Algorithm, context::CipherContext, operation::OperationChoice, output::OutputFormat, +}; diff --git a/cli/src/args.rs b/cli/src/args.rs index 6abd58b..c43a187 100644 --- a/cli/src/args.rs +++ b/cli/src/args.rs @@ -1,4 +1,4 @@ -use cipher_factory::{Algorithm, OperationChoice, OutputFormat}; +use cipher_factory::{Algorithm, CipherContext, OperationChoice, OutputFormat}; use clap::Parser; #[derive(Debug, Clone, Parser)] @@ -24,3 +24,15 @@ pub struct Args { #[arg(short = 'f', long)] pub output_format: Option, } + +impl From for CipherContext { + fn from(args: Args) -> Self { + Self { + algorithm: args.algorithm, + operation: args.operation, + key: args.key, + input_text: args.text, + output_format: args.output_format.unwrap_or_default(), + } + } +} diff --git a/cli/src/main.rs b/cli/src/main.rs index 008bcbe..9acc950 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -1,46 +1,17 @@ mod args; use crate::args::Args; -use cipher_core::BlockCipher; -use cipher_factory::{self, OperationChoice, OutputFormat}; +use cipher_factory::CipherContext; use clap::Parser; -use color_eyre::eyre::{Ok, Result}; +use color_eyre::eyre::Result; fn main() -> Result<()> { color_eyre::install()?; - let Args { - operation, - algorithm, - key, - text, - output_format, - } = Args::parse(); + let args = Args::parse(); + let context = CipherContext::from(args); - let text_bytes = algorithm.parse_text(&text)?; - let cipher = algorithm.new_cipher(&key)?; - - execute_cipher(operation, cipher.as_ref(), &text_bytes, output_format)?; - - Ok(()) -} - -fn execute_cipher( - operation: OperationChoice, - cipher: &dyn BlockCipher, - text_bytes: &[u8], - output_format: Option, -) -> Result<()> { - match operation { - OperationChoice::Encrypt => { - let ciphertext = cipher.encrypt(text_bytes)?; - println!("{ciphertext:X}"); - } - OperationChoice::Decrypt => { - let plaintext = cipher.decrypt(text_bytes)?; - let output = output_format.unwrap_or_default().to_string(&plaintext); - println!("{output}"); - } - } + let output = context.process()?; + println!("{output}"); Ok(()) }