prowocessing: extract experiment into its own file
This commit is contained in:
parent
db9228dec4
commit
d9a07c8898
3 changed files with 66 additions and 68 deletions
1
crates/prowocessing/src/experimental.rs
Normal file
1
crates/prowocessing/src/experimental.rs
Normal file
|
@ -0,0 +1 @@
|
||||||
|
pub mod enum_based;
|
64
crates/prowocessing/src/experimental/enum_based.rs
Normal file
64
crates/prowocessing/src/experimental/enum_based.rs
Normal 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()
|
||||||
|
}
|
||||||
|
}
|
|
@ -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() {
|
||||||
|
|
Loading…
Reference in a new issue