fed-sx-m1: Step 4e — bootstrap:load_genesis/strip_sx_suffix bridges read_genesis -> registry + 15 tests
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
This commit is contained in:
@@ -3,7 +3,8 @@
|
||||
read_section/2, sections/0, section_subdir/1,
|
||||
default_base/0, ends_with_sx/1,
|
||||
build_genesis/1, verify_genesis/2,
|
||||
cidhash_path/1, write_cidhash/2, read_cidhash/1]).
|
||||
cidhash_path/1, write_cidhash/2, read_cidhash/1,
|
||||
load_genesis/1, strip_sx_suffix/1]).
|
||||
|
||||
%% Genesis bundle reader per design §12.2.
|
||||
%%
|
||||
@@ -140,3 +141,47 @@ write_cidhash(BasePath, Cid) ->
|
||||
|
||||
read_cidhash(BasePath) ->
|
||||
file:read_file(cidhash_path(BasePath)).
|
||||
|
||||
%% ── Step 4e: load_genesis → registry ────────────────────────────
|
||||
%%
|
||||
%% Walks the read_genesis result and registers each file as a
|
||||
%% registry entry. The section atom is the registry kind directly
|
||||
%% (both name spaces are identical — see Step 4c sections/0 and
|
||||
%% Step 5a registry:kinds/0). The entry Name is the filename minus
|
||||
%% the `.sx` suffix, kept as a binary; the entry value is the
|
||||
%% file's raw bytes.
|
||||
%%
|
||||
%% Returns `{ok, RegistryState}` on success. Later steps (4f / the
|
||||
%% SX-parser bridge) will replace the raw bytes with parsed forms;
|
||||
%% the binary stand-in is enough to prove the bridge works.
|
||||
|
||||
load_genesis(ReadResult) ->
|
||||
case ReadResult of
|
||||
{ok, Sections} ->
|
||||
{ok, load_sections(Sections, registry:new())};
|
||||
Other ->
|
||||
{error, {bad_read_result, Other}}
|
||||
end.
|
||||
|
||||
load_sections([], State) -> State;
|
||||
load_sections([{Kind, Entries} | Rest], State) ->
|
||||
load_sections(Rest, load_entries(Kind, Entries, State)).
|
||||
|
||||
load_entries(_Kind, [], State) -> State;
|
||||
load_entries(Kind, [{Name, Bytes} | Rest], State) ->
|
||||
BaseName = strip_sx_suffix(Name),
|
||||
{ok, NewState} = registry:register(Kind, BaseName, Bytes, State),
|
||||
load_entries(Kind, Rest, NewState).
|
||||
|
||||
%% strip_sx_suffix(Binary) — drops the trailing ".sx" if present.
|
||||
%% 46='.' 115='s' 120='x'.
|
||||
strip_sx_suffix(B) when is_binary(B) ->
|
||||
case ends_with_sx(B) of
|
||||
false -> B;
|
||||
true -> take_prefix(B, byte_size(B) - 3)
|
||||
end.
|
||||
|
||||
take_prefix(_, 0) -> <<>>;
|
||||
take_prefix(<<H, Rest/binary>>, N) when N > 0 ->
|
||||
Tail = take_prefix(Rest, N - 1),
|
||||
<<H, Tail/binary>>.
|
||||
|
||||
Reference in New Issue
Block a user