WIP: image processing library (or libraries?) #12

Draft
schrottkatze wants to merge 15 commits from schrottkatze/iowo:proc-libs into main
3 changed files with 66 additions and 68 deletions
Showing only changes of commit 78d800b461 - Show all commits

View file

@ -0,0 +1 @@
pub mod enum_based;

View file

@ -0,0 +1,64 @@
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(),
}
}
#[must_use]
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 }
}
}
impl Default for PipelineBuilder {
fn default() -> Self {
Self::new()
}
}

View file

@ -7,74 +7,7 @@ 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. /// 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... /// Gonna first try string processing...
pub mod experimental { pub 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(),
}
}
#[must_use]
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 }
}
}
impl Default for PipelineBuilder {
fn default() -> Self {
Self::new()
}
}
}
}
#[test] #[test]
fn test_enums() { fn test_enums() {