-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
800e599
commit a91e48e
Showing
4 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
mod config; | ||
mod optics; | ||
mod output; | ||
mod utils; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
pub type Get<S, A> = Box<dyn Fn(S) -> A>; | ||
pub type Apply<S, A> = Box<dyn Fn(A) -> S>; | ||
pub type Set<S, A> = Box<dyn Fn(S, A) -> S>; | ||
pub type GetOption<S, A> = Box<dyn Fn(S) -> Option<A>>; | ||
|
||
pub struct Iso<S, A> { | ||
get: Get<S, A>, | ||
apply: Apply<S, A>, | ||
} | ||
|
||
impl<S> Iso<S, S> { | ||
pub fn id() -> Self { | ||
Self { | ||
get: Box::new(|s| s), | ||
apply: Box::new(|s| s), | ||
} | ||
} | ||
} | ||
|
||
impl<S, A> Iso<S, A> | ||
where | ||
S: 'static, | ||
A: 'static, | ||
{ | ||
pub fn map_invariant<B>( | ||
self, | ||
covariant: impl Fn(A) -> B + 'static, | ||
contravariant: impl Fn(B) -> A + 'static, | ||
) -> Iso<S, B> { | ||
let Self { get, apply } = self; | ||
Iso { | ||
get: Box::new(move |s| covariant(get(s))), | ||
apply: Box::new(move |b| apply(contravariant(b))), | ||
} | ||
} | ||
|
||
pub fn lens(self) -> Lens<S, A> { | ||
let Self { get, apply } = self; | ||
Lens { | ||
get, | ||
set: Box::new(move |_s, a| apply(a)), | ||
} | ||
} | ||
} | ||
|
||
pub struct Lens<S, A> { | ||
get: Get<S, A>, | ||
set: Set<S, A>, | ||
} | ||
|
||
impl<S: 'static> Lens<S, S> { | ||
pub fn id() -> Self { | ||
Self { | ||
get: Box::new(|s| s), | ||
set: Box::new(|_s, a| a), | ||
} | ||
} | ||
} | ||
|
||
impl<S, A> Lens<S, A> | ||
where | ||
S: 'static, | ||
A: 'static, | ||
{ | ||
pub fn map_invariant<B>( | ||
self, | ||
covariant: impl Fn(A) -> B + 'static, | ||
contravariant: impl Fn(B) -> A + 'static, | ||
) -> Lens<S, B> { | ||
let Self { get, set } = self; | ||
Lens { | ||
get: Box::new(move |s| covariant(get(s))), | ||
set: Box::new(move |s, b| set(s, contravariant(b))), | ||
} | ||
} | ||
|
||
pub fn prism(self) -> Prism<S, A> { | ||
let Self { get, set } = self; | ||
Prism { | ||
get_option: Box::new(move |s| Some(get(s))), | ||
set, | ||
} | ||
} | ||
} | ||
|
||
pub struct Prism<S, A> { | ||
get_option: GetOption<S, A>, | ||
set: Set<S, A>, | ||
} | ||
|
||
impl<S: 'static> Prism<S, S> { | ||
pub fn id() -> Self { | ||
Self { | ||
get_option: Box::new(|s| Some(s)), | ||
set: Box::new(|_s, a| a), | ||
} | ||
} | ||
} | ||
|
||
impl<S, A> Prism<S, A> | ||
where | ||
S: 'static, | ||
A: 'static, | ||
{ | ||
pub fn map_invariant<B>( | ||
self, | ||
covariant: impl Fn(A) -> B + 'static, | ||
contravariant: impl Fn(B) -> A + 'static, | ||
) -> Prism<S, B> { | ||
let Self { get_option, set } = self; | ||
Prism { | ||
get_option: Box::new(move |s| get_option(s).map(&covariant)), | ||
set: Box::new(move |s, b| set(s, contravariant(b))), | ||
} | ||
} | ||
|
||
pub fn optional(self) -> Optional<S, A> { | ||
let Self { get_option, set } = self; | ||
Optional { | ||
get_option, | ||
replace: set, | ||
} | ||
} | ||
} | ||
|
||
pub struct Optional<S, A> { | ||
get_option: GetOption<S, A>, | ||
replace: Set<S, A>, | ||
} | ||
|
||
impl<S: 'static> Optional<S, S> { | ||
pub fn id() -> Self { | ||
Self { | ||
get_option: Box::new(|s| Some(s)), | ||
replace: Box::new(|_s, a| a), | ||
} | ||
} | ||
} | ||
|
||
impl<S, A> Optional<S, A> | ||
where | ||
S: 'static, | ||
A: 'static, | ||
{ | ||
pub fn map_invariant<B>( | ||
self, | ||
covariant: impl Fn(A) -> B + 'static, | ||
contravariant: impl Fn(B) -> A + 'static, | ||
) -> Optional<S, B> { | ||
let Self { | ||
get_option, | ||
replace, | ||
} = self; | ||
Optional { | ||
get_option: Box::new(move |s| get_option(s).map(&covariant)), | ||
replace: Box::new(move |s, b| replace(s, contravariant(b))), | ||
} | ||
} | ||
} | ||
|
||
pub trait Traversal<S, A> { | ||
type Traversable: Iterator<Item = A>; | ||
|
||
fn get_all(s: S) -> Self::Traversable; | ||
fn modify_all<F>(f: F) -> fn(S) -> S | ||
where | ||
F: Fn(A) -> A; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// first thing I think of is JS values go in, js values come out | ||
// a hashmap | ||
|
||
use std::collections::HashMap; | ||
|
||
use wasm_bindgen::JsValue; | ||
|
||
use crate::{optics::Optional}; | ||
|
||
// #/**/* | ||
pub struct Scope(String); | ||
|
||
// so we send this map back to the client, how will the client like that? | ||
pub struct OptionalsByScope(HashMap<Scope, Optional<JsValue, JsValue>>); | ||
|
||
// need Schema to get the scopes | ||
// Validate Schema against UISchema | ||
// fn UISchema -> (Scope, Value) -> Data -> Data | ||
// fn (UISchema -> Scopes) -> (Scope, Value) -> Data -> Data |