2024-01-29 22:47:35 +01:00
|
|
|
//! # This is the image processing library for iOwO
|
|
|
|
//!
|
|
|
|
//! 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!
|
2024-02-19 12:56:47 +01:00
|
|
|
|
|
|
|
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...
|
2024-02-19 18:07:09 +01:00
|
|
|
pub mod experimental {
|
2024-02-19 12:56:47 +01:00
|
|
|
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(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-19 18:07:09 +01:00
|
|
|
#[must_use]
|
2024-02-19 12:56:47 +01:00
|
|
|
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 }
|
|
|
|
}
|
|
|
|
}
|
2024-02-19 18:07:09 +01:00
|
|
|
|
|
|
|
impl Default for PipelineBuilder {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
2024-02-19 12:56:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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"));
|
|
|
|
}
|