decode_constructed_requests.bend
bendvy-ecs-core@0.1.0.1 · 31 lines · 0 definitions
1import Base
2import decode_construction.bend as Constructor
3import ordinary_declaration.bend as Identity
4import decode_data.bend as D
5import bundle_construction.bend as Build
6import decode_requests.bend as Request
7import bundle_requests.bend as Commands
8import world.bend as W
9import capabilities.bend as Cap
10
11type Input<S:Data,I:Type> is Type:Input{operation:Commands.Operation<S>,raw:I}
12type Output<S:Data,I:Type,P:Type> is Type:
13 Accepted{target:W.Handle<S>,spawned:Bool,canonical:D.Raw,undo:P -> I}
14 Refused{owner:Maybe<&1,I>,error:Request.Error}
15def refused(~S:Data,~I:Type,~P:Type,undo:P -> I,owner:Maybe<&1,P>,error:Request.Error) -> Output<S,I,P>:
16 match owner:
17 case None{}:Refused{None{},error}
18 case Some{owner}:Refused{Some{undo(owner)},error}
19def returned(~S:Data,~I:Type,~P:Type,~H:Type,undo:P -> I,result:H & Request.Output<S,P>) -> H & Output<S,I,P>:
20 match result:
21 case (ctx,Request.Accepted{target,spawned,canonical}):(ctx,Accepted{target,spawned,canonical,undo})
22 case (ctx,Request.Refused{owner,error}):(ctx,refused(~S,~I,~P,undo,owner,error))
23def constructed(~S:Data,~I:Type,~P:Type,~H:Type,~request:Cap.OwnedRequest<H,Request.Input<S,P>,Request.Output<S,P>>,ctx:H,operation:Commands.Operation<S>,result:Build.Construction<I,P,D.Error>) -> H & Output<S,I,P>:
24 match result:
25 case Build.Invalid{owner,error}:(ctx,Refused{Some{owner},Request.Validation{error}})
26 case Build.Constructed{owner,undo}:returned(~S,~I,~P,~H,undo,Cap.owned_request(~H,~Request.Input<S,P>,~Request.Output<S,P>,~request,ctx,Request.Input{operation,owner}))
27def apply(~S:Data,~I:Type,~P:Type,~identity:Identity.Declaration<S,P>,~project:P -> P & D.Raw,~incoming:I -> I & D.Raw,~build:I -> D.Raw -> Build.Construction<I,P,D.Error>,~factory:Constructor.Factory<S,I,P,identity,project,incoming,build>,~H:Type,~request:Cap.OwnedRequest<H,Request.Input<S,P>,Request.Output<S,P>>,ctx:H,input:Input<S,I>) -> H & Output<S,I,P>:
28 match input:
29 case Input{operation,raw}:constructed(~S,~I,~P,~H,~request,ctx,operation,Constructor.construct(~S,~I,~P,~identity,~project,~incoming,~build,factory,raw))
30def grant(~S:Data,~I:Type,~P:Type,~identity:Identity.Declaration<S,P>,~project:P -> P & D.Raw,~incoming:I -> I & D.Raw,~build:I -> D.Raw -> Build.Construction<I,P,D.Error>,~factory:Constructor.Factory<S,I,P,identity,project,incoming,build>,~H:Type,~request:Cap.OwnedRequest<H,Request.Input<S,P>,Request.Output<S,P>>) -> Cap.OwnedRequest<H,Input<S,I>,Output<S,I,P>>:
31 Cap.OwnedRequest{ctx => input => apply(~S,~I,~P,~identity,~project,~incoming,~build,~factory,~H,~request,ctx,input)}