From 1e9648966fed1167dbcd963aa72d3953280b9fd6 Mon Sep 17 00:00:00 2001 From: Schrottkatze Date: Mon, 19 Feb 2024 12:56:47 +0100 Subject: [PATCH] experimentation: write experiment for enum architecture --- crates/prowocessing/src/lib.rs | 76 ++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/crates/prowocessing/src/lib.rs b/crates/prowocessing/src/lib.rs index f83705a..8affe30 100644 --- a/crates/prowocessing/src/lib.rs +++ b/crates/prowocessing/src/lib.rs @@ -2,3 +2,79 @@ //! //! One of the design goals for this library is, however, to be a simple, generic image processing library. //! For now, it's just indev... lets see what comes of it! + +use experimental::enum_based::{Instruction, PipelineBuilder}; + +/// just some experiments, to test whether the architecture i want is even possible (or how to do it). probably temporary. +/// Gonna first try string processing... +mod experimental { + pub mod enum_based { + pub enum Instruction { + Uppercase, + Lowercase, + } + + pub struct Pipeline { + pipeline: Vec String>, + } + + impl Pipeline { + pub fn run(&self, val: String) -> String { + let mut current = val; + + for instr in &self.pipeline { + current = instr(current); + } + + current + } + } + + pub struct PipelineBuilder { + pipeline: Vec, + } + + impl PipelineBuilder { + pub fn new() -> Self { + Self { + pipeline: Vec::new(), + } + } + + pub fn insert(mut self, instr: Instruction) -> Self { + self.pipeline.push(instr); + self + } + + pub fn build(&self) -> Pipeline { + fn uppercase(v: String) -> String { + str::to_uppercase(&v) + } + fn lowercase(v: String) -> String { + str::to_lowercase(&v) + } + + let mut res = Vec::new(); + + for item in &self.pipeline { + res.push(match item { + Instruction::Uppercase => uppercase, + Instruction::Lowercase => lowercase, + }); + } + + Pipeline { pipeline: res } + } + } + } +} + +#[test] +fn test_enums() { + let builder = PipelineBuilder::new().insert(Instruction::Uppercase); + let upr = builder.build(); + let upr_lowr = builder.insert(Instruction::Lowercase).build(); + + assert_eq!(upr.run(String::from("Test")), String::from("TEST")); + assert_eq!(upr_lowr.run(String::from("Test")), String::from("test")); +}