shtola

ssg in rust
git clone https://tilde.team/~marisa/repo/shtola.git
Log | Files | Refs | LICENSE

commit 121a32e8f9e2359a129807b4d6b26f2f907d8fe3
Author: marisa <mokou@posteo.de>
Date:   Wed, 30 Oct 2019 20:54:29 +0100

Initial commit

Diffstat:
A.editorconfig | 12++++++++++++
A.gitignore | 1+
ACargo.lock | 6++++++
ACargo.toml | 2++
Aware/Cargo.toml | 7+++++++
Aware/src/lib.rs | 79+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 107 insertions(+), 0 deletions(-)

diff --git a/.editorconfig b/.editorconfig @@ -0,0 +1,12 @@ +# EditorConfig is awesome: https://EditorConfig.org + +# top-most EditorConfig file +root = true + +# Unix-style newlines with a newline ending every file +[*] +end_of_line = lf +insert_final_newline = true +indent_style = tab +charset = utf-8 +tab_width = 2 diff --git a/.gitignore b/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/Cargo.lock b/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "ware" +version = "0.1.0" + diff --git a/Cargo.toml b/Cargo.toml @@ -0,0 +1,2 @@ +[workspace] +members = ["ware"] diff --git a/ware/Cargo.toml b/ware/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "ware" +version = "0.1.0" +authors = ["marisa <mokou@posteo.de>"] +edition = "2018" + +[dependencies] diff --git a/ware/src/lib.rs b/ware/src/lib.rs @@ -0,0 +1,79 @@ +pub struct Ware<R> { + pub fns: Vec<Box<dyn Fn(R) -> R>>, +} + +impl<R> Ware<R> { + pub fn new() -> Ware<R> { + let vec: Vec<Box<dyn Fn(R) -> R>> = Vec::new(); + Ware { fns: vec } + } + + pub fn wrap(&mut self, func: Box<dyn Fn(R) -> R>) { + self.fns.push(func); + } + + pub fn run(&self, arg: R) -> R { + self.fns.iter().fold(arg, |acc, func| func(acc)) + } +} + +pub struct Ware2<R, S> { + pub fns: Vec<Box<dyn Fn(R, S) -> (R, S)>>, +} + +impl<R, S> Ware2<R, S> { + pub fn new() -> Ware2<R, S> { + let vec: Vec<Box<dyn Fn(R, S) -> (R, S)>> = Vec::new(); + Ware2 { fns: vec } + } + + pub fn wrap(&mut self, func: Box<dyn Fn(R, S) -> (R, S)>) { + self.fns.push(func); + } + + pub fn run (&self, arg1: R, arg2: S) -> (R, S) { + self.fns.iter().fold((arg1, arg2), |acc, func| func(acc.0, acc.1)) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_works() { + let value = 1; + let mut w: Ware<i32> = Ware::new(); + w.wrap(Box::new(|num| num + 1)); + assert_eq!(w.run(value), 2); + } + + #[test] + fn it_is_immutable() { + let value = 1; + let closure = |num| { + let num = num + 1; + num + }; + let mut w: Ware<i32> = Ware::new(); + w.wrap(Box::new(closure)); + assert_eq!(w.run(value), 2); + assert_eq!(value, 1); + } + + #[test] + fn ware2_works() { + let val1 = 2; + let val2 = "a".to_string(); + let closure = |num: i32, st: String| { + let num = num - 1; + let mut string = String::new(); + string.push_str(&st); + string.push('b'); + (num, string) + }; + let mut w: Ware2<i32, String> = Ware2::new(); + w.wrap(Box::new(closure)); + assert_eq!(w.run(val1, val2), (1, String::from("ab"))); + } +}