ordinary_optional_resource_declaration.bend

bendvy-ecs-core@0.1.0.0 · 49 lines · 0 definitions

1import Base
2import bendvy-ecs-core@0.1.0.0/ordinary_optional_resource.bend as O
3import bendvy-ecs-core@0.1.0.0/optional_resource_binding.bend as B
4import bendvy-ecs-core@0.1.0.0/ordinary_schema.bend as Schema
5import bendvy-ecs-core@0.1.0.0/owner_product.bend as Product
6import bendvy-ecs-core@0.1.0.0/ordinary_resource_field_system.bend as Field
7import bendvy-ecs-core@0.1.0.0/ordinary_query_declaration.bend as Q
8import bendvy-ecs-core@0.1.0.0/world.bend as W
9import bendvy-ecs-core@0.1.0.0/resource.bend as Resource
10import bendvy-ecs-core@0.1.0.0/transaction.bend as T
11import bendvy-ecs-core@0.1.0.0/capabilities.bend as Cap
12# One declaration carries actual operation, preflight and owner-return observer.
13type Declaration<-S:Data,-C:Type,-R:Type,-E:Data,-Ops:Type -> Type,-V:Data> is Type:
14  Declaration{ordinary:Field.Declaration<S,T.Tx<W.World<S,C,R,E>,E>,Ops(T.Tx<W.World<S,C,R,E>,E>)>,required:W.World<S,C,R,E> -> W.World<S,C,R,E> & List<&2,String>,observe:W.World<S,C,R,E> -> W.World<S,C,R,E> & V,render:V -> String}
15def Ops(~S:Data,~P:Type,~identity:Schema.Resource<S,P>,~V:Data,H:Type) -> Type:
16  Field.Read<S,P,identity,H,Maybe<&2,V>>
17def provided(~S:Data,~C:Type,~R:Type,~E:Data,world:W.World<S,C,R,E>) -> W.World<S,C,R,E> & List<&2,String>:
18  (world,[])
19def built(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~Rest:Type,~V:Data,~identity:Schema.Resource<S,P>,~lens:Product.Lens<R,O.Slot<S,P,identity>,Rest>,~binding:B.Binding<S,R,P,Rest,identity,lens>,~view:P -> P & V,required:W.World<S,C,R,E> -> W.World<S,C,R,E> & List<&2,String>,render:Maybe<&2,V> -> String) -> Declaration<S,C,R,E,Ops(~S,~P,~identity,~V),Maybe<&2,V>>:
20  Declaration{Field.Declaration{Field.Read{Cap.ValueRead{Resource.tx_get(~S,~C,~R,~E,~Maybe<&2,V>,~O.project(~S,~R,~P,~Rest,~V,~identity,~lens,~view))}},[Q.Clause{Schema.resource_key(~S,~P,identity),Q.Read{}}]},required,O.observe(~S,~C,~R,~E,~P,~Rest,~V,~identity,~lens,~view,~O.Declaration{}),render}
21def optional(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~Rest:Type,~V:Data,~identity:Schema.Resource<S,P>,~lens:Product.Lens<R,O.Slot<S,P,identity>,Rest>,~binding:B.Binding<S,R,P,Rest,identity,lens>,~view:P -> P & V,render:Maybe<&2,V> -> String) -> Declaration<S,C,R,E,Ops(~S,~P,~identity,~V),Maybe<&2,V>>:
22  built(~S,~C,~R,~E,~P,~Rest,~V,~identity,~lens,~binding,~view,provided(~S,~C,~R,~E),render)
23def required(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~Rest:Type,~V:Data,~identity:Schema.Resource<S,P>,~lens:Product.Lens<R,O.Slot<S,P,identity>,Rest>,~binding:B.Binding<S,R,P,Rest,identity,lens>,~view:P -> P & V,render:Maybe<&2,V> -> String) -> Declaration<S,C,R,E,Ops(~S,~P,~identity,~V),Maybe<&2,V>>:
24  built(~S,~C,~R,~E,~P,~Rest,~V,~identity,~lens,~binding,~view,O.required(~S,~C,~R,~E,~P,~Rest,~V,~identity,~lens,~view,~O.Declaration{}),render)
25def PairOps(~L:Type -> Type,~Right:Type -> Type,H:Type) -> Type:
26  Cap.Bundle<L(H),Right(H)>
27# Extra operational grants describe distinct actual fields, not a metadata mirror.
28def with(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type -> Type,~Right:Type -> Type,~V:Data,left:Declaration<S,C,R,E,L,V>,right:Field.Declaration<S,T.Tx<W.World<S,C,R,E>,E>,Right(T.Tx<W.World<S,C,R,E>,E>)>) -> Declaration<S,C,R,E,PairOps(~L,~Right),V>:
29  match left:
30    case Declaration{ordinary,required,observe,render}:Declaration{Field.pair(~S,~T.Tx<W.World<S,C,R,E>,E>,~L(T.Tx<W.World<S,C,R,E>,E>),~Right(T.Tx<W.World<S,C,R,E>,E>),ordinary,right),required,observe,render}
31def ordinary(~S:Data,~C:Type,~R:Type,~E:Data,~Ops:Type -> Type,~V:Data,declaration:Declaration<S,C,R,E,Ops,V>) -> Field.Declaration<S,T.Tx<W.World<S,C,R,E>,E>,Ops(T.Tx<W.World<S,C,R,E>,E>)>:
32  match declaration:
33    case Declaration{ordinary,_,_,_}:ordinary
34def preflight(~S:Data,~C:Type,~R:Type,~E:Data,~Ops:Type -> Type,~V:Data,declaration:Declaration<S,C,R,E,Ops,V>,world:W.World<S,C,R,E>) -> W.World<S,C,R,E> & List<&2,String>:
35  match declaration:
36    case Declaration{_,required,_,_}:required(world)
37def observer(~S:Data,~C:Type,~R:Type,~E:Data,~Ops:Type -> Type,~V:Data,declaration:Declaration<S,C,R,E,Ops,V>) -> W.World<S,C,R,E> -> W.World<S,C,R,E> & V:
38  match declaration:
39    case Declaration{_,_,observe,_}:observe
40def presenter(~S:Data,~C:Type,~R:Type,~E:Data,~Ops:Type -> Type,~V:Data,declaration:Declaration<S,C,R,E,Ops,V>) -> V -> String:
41  match declaration:
42    case Declaration{_,_,_,render}:render
43def names(~S:Data,clauses:List<&2,Q.Clause<S>>) -> String:
44  match clauses:
45    case []:""
46    case Q.Clause{name,_} <> []:name
47    case Q.Clause{name,_} <> rest:name ++ "," ++ names(~S,rest)
48def name(~S:Data,~C:Type,~R:Type,~E:Data,~Ops:Type -> Type,~V:Data,declaration:Declaration<S,C,R,E,Ops,V>) -> String:
49  names(~S,Field.clauses(~S,~T.Tx<W.World<S,C,R,E>,E>,~Ops(T.Tx<W.World<S,C,R,E>,E>),ordinary(~S,~C,~R,~E,~Ops,~V,declaration)))