restore_graph_mapping.bend

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

1import Base
2import bendvy-ecs-core@0.1.0.0/entity_key.bend as Key
3import bendvy-ecs-core@0.1.0.0/world.bend as W
4import bendvy-ecs-core@0.1.0.0/restore_data.bend as Basic
5import bendvy-ecs-core@0.1.0.0/relation_types.bend as G
6import bendvy-ecs-core@0.1.0.0/relation_graph.bend as Graph
7
8type Mapped<S:Data> is Data:
9  Mapped{graph:G.Graph<S>}
10  MissingBinding{saved:Key.Key}
11def target(~S:Data,descriptor:G.Descriptor<S>,source:W.Handle<S>,saved:Key.Key,result:Maybe<&2,W.Handle<S>>,graph:G.Graph<S>,next:G.Graph<S> -> Mapped<S>) -> Mapped<S>:
12  match source result graph:
13    case _ None{} _:MissingBinding{saved}
14    case W.Handle{_,source} Some{W.Handle{_,target}} G.Graph{edges,inverses}:next(Graph.updated(~S,False{},edges,inverses,descriptor,source,target))
15def source(~S:Data,descriptor:G.Descriptor<S>,saved:Key.Key,+other:Key.Key,+bindings:List<&2,Basic.Binding<S>>,result:Maybe<&2,W.Handle<S>>,graph:G.Graph<S>,next:G.Graph<S> -> Mapped<S>) -> Mapped<S>:
16  match result:
17    case None{}:MissingBinding{saved}
18    case Some{handle}:target(~S,descriptor,handle,other,Basic.lookup(~S,other,bindings),graph,next)
19def edges(~S:Data,values:List<&2,G.Edge<S>>,+bindings:List<&2,Basic.Binding<S>>,graph:G.Graph<S>) -> Mapped<S>:
20  match values:
21    case []:Mapped{graph}
22    case G.Edge{descriptor,+saved,other} <> rest:source(~S,descriptor,saved,other,bindings,Basic.lookup(~S,saved,bindings),graph,graph => edges(~S,rest,bindings,graph))
23def remap(~S:Data,graph:G.Graph<S>,bindings:List<&2,Basic.Binding<S>>) -> Mapped<S>:
24  match graph:
25    case G.Graph{values,_}:edges(~S,values,bindings,G.Graph{[],[]})