import Base import ./entity_key.bend as K import ./world.bend as W import ./relation_types.bend as T import ./execution_relation_operation.bend as O # Existing Failure represents relate/reorder. Unrelate remains explicit rather # than reporting a different operation to fit that older record. type Message<-S:Data,-descriptor:T.Descriptor> is Data: RelationFailed{operation:O.Operation,failure:T.Failure} UnrelateFailed{operation:O.Operation,source:K.Key,error:T.Error} def error_target(error:T.Error,parent:K.Key) -> K.Key: match error: case T.MissingChildEntity{_,child,_}:child case T.DuplicateChild{_,child,_}:child case T.ChildNotRelatedToParent{_,child,_}:child case _:parent def failed(~S:Data,~descriptor:T.Descriptor,operation:O.Operation,+error:T.Error) -> Message: match operation: 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}} case O.Unrelate{+source}:UnrelateFailed{O.Unrelate{source},W.handle_key(~S,source),error} 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}} def refused(~S:Data,~descriptor:T.Descriptor,operation:O.Operation,reason:O.Reason) -> Maybe<&2,Message>: match reason: case O.RelationError{error}:Some{failed(~S,~descriptor,operation,error)} case O.HierarchyRequired{}:None{}