execution_relation_failure_message.bend

bendvy-ecs-core@0.1.0.3 · 26 lines · 4 definitions

1import Base
2import bendvy-ecs-core@0.1.0.3/entity_key.bend as K
3import bendvy-ecs-core@0.1.0.3/world.bend as W
4import bendvy-ecs-core@0.1.0.3/relation_types.bend as T
5import bendvy-ecs-core@0.1.0.3/execution_relation_operation.bend as O
6
7# Existing Failure represents relate/reorder. Unrelate remains explicit rather
8# than reporting a different operation to fit that older record.
9type Message<-S:Data,-descriptor:T.Descriptor<S>> is Data:
10  RelationFailed{operation:O.Operation<S>,failure:T.Failure<S>}
11  UnrelateFailed{operation:O.Operation<S>,source:K.Key,error:T.Error}
12def error_target(error:T.Error,parent:K.Key) -> K.Key:
13  match error:
14    case T.MissingChildEntity{_,child,_}:child
15    case T.DuplicateChild{_,child,_}:child
16    case T.ChildNotRelatedToParent{_,child,_}:child
17    case _:parent
18def failed(~S:Data,~descriptor:T.Descriptor<S>,operation:O.Operation<S>,+error:T.Error) -> Message<S,descriptor>:
19  match operation:
20    case O.Relate{+source,+target}:RelationFailed{O.Relate{source,target},T.Failure{descriptor,T.Relate{},W.handle_key(~S,source),W.handle_key(~S,target),error}}
21    case O.Unrelate{+source}:UnrelateFailed{O.Unrelate{source},W.handle_key(~S,source),error}
22    case O.Reorder{+parent,children}:RelationFailed{O.Reorder{parent,children},T.Failure{descriptor,T.ReorderChildren{},W.handle_key(~S,parent),error_target(error,W.handle_key(~S,parent)),error}}
23def refused(~S:Data,~descriptor:T.Descriptor<S>,operation:O.Operation<S>,reason:O.Reason) -> Maybe<&2,Message<S,descriptor>>:
24  match reason:
25    case O.RelationError{error}:Some{failed(~S,~descriptor,operation,error)}
26    case O.HierarchyRequired{}:None{}