decode_fallible_constructed_requests.bend

bendvy-ecs-core@0.1.0.1 · 32 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 Error<Custom:Data> is Data:Construction{error:Constructor.Error<Custom>} Operation{error:Request.Error}
13type Output<S:Data,I:Type,P:Type,Custom:Data> is Type:
14  Accepted{target:W.Handle<S>,spawned:Bool,canonical:D.Raw,undo:P -> I}
15  Refused{owner:Maybe<&1,I>,error:Error<Custom>}
16def refused(~S:Data,~I:Type,~P:Type,~Custom:Data,undo:P -> I,owner:Maybe<&1,P>,error:Request.Error) -> Output<S,I,P,Custom>:
17  match owner:
18    case None{}:Refused{None{},Operation{error}}
19    case Some{owner}:Refused{Some{undo(owner)},Operation{error}}
20def returned(~S:Data,~I:Type,~P:Type,~Custom:Data,~H:Type,undo:P -> I,result:H & Request.Output<S,P>) -> H & Output<S,I,P,Custom>:
21  match result:
22    case (ctx,Request.Accepted{target,spawned,canonical}):(ctx,Accepted{target,spawned,canonical,undo})
23    case (ctx,Request.Refused{owner,error}):(ctx,refused(~S,~I,~P,~Custom,undo,owner,error))
24def constructed(~S:Data,~I:Type,~P:Type,~Custom:Data,~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,Constructor.Error<Custom>>) -> H & Output<S,I,P,Custom>:
25  match result:
26    case Build.Invalid{owner,error}:(ctx,Refused{Some{owner},Construction{error}})
27    case Build.Constructed{owner,undo}:returned(~S,~I,~P,~Custom,~H,undo,Cap.owned_request(~H,~Request.Input<S,P>,~Request.Output<S,P>,~request,ctx,Request.Input{operation,owner}))
28def apply(~S:Data,~I:Type,~P:Type,~Custom:Data,~identity:Identity.Declaration<S,P>,~project:P -> P & D.Raw,~incoming:I -> I & D.Raw,~build:I -> D.Raw -> Build.Construction<I,P,Custom>,~factory:Constructor.FallibleFactory<S,I,P,Custom,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,Custom>:
29  match input:
30    case Input{operation,raw}:constructed(~S,~I,~P,~Custom,~H,~request,ctx,operation,Constructor.fallible_construct(~S,~I,~P,~Custom,~identity,~project,~incoming,~build,factory,raw))
31def grant(~S:Data,~I:Type,~P:Type,~Custom:Data,~identity:Identity.Declaration<S,P>,~project:P -> P & D.Raw,~incoming:I -> I & D.Raw,~build:I -> D.Raw -> Build.Construction<I,P,Custom>,~factory:Constructor.FallibleFactory<S,I,P,Custom,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,Custom>>:
32  Cap.OwnedRequest{ctx => input => apply(~S,~I,~P,~Custom,~identity,~project,~incoming,~build,~factory,~H,~request,ctx,input)}