mirror of
https://forge.katzen.cafe/katzen-cafe/iowo.git
synced 2024-11-05 15:26:24 +01:00
experimentation: write experiment for enum architecture
This commit is contained in:
parent
a2695a2a11
commit
1e9648966f
|
@ -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<fn(String) -> 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<Instruction>,
|
||||
}
|
||||
|
||||
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"));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue