Packages

Type-safe finite state machine library for Gleam

Current section

Files

Jump to
scamper src scamper@history.erl
Raw

src/scamper@history.erl

-module(scamper@history).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/scamper/history.gleam").
-export([new_record/5, append/3, filter_by_from/2, filter_by_to/2, filter_by_event/2, last_n/2]).
-export_type([transition_record/3]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(" Transition history recording and querying for scamper FSMs.\n").
-type transition_record(GYE, GYF, GYG) :: {transition_record,
GYE,
GYF,
GYE,
integer(),
gleam@option:option(GYG)}.
-file("src/scamper/history.gleam", 18).
?DOC(" Create a new transition record.\n").
-spec new_record(GYH, GYI, GYH, integer(), gleam@option:option(GYJ)) -> transition_record(GYH, GYI, GYJ).
new_record(From, Event, To, Timestamp, Context_snapshot) ->
{transition_record, From, Event, To, Timestamp, Context_snapshot}.
-file("src/scamper/history.gleam", 36).
?DOC(
" Append a record to the history (newest first).\n"
" Trims to the limit during append if a limit is set.\n"
).
-spec append(
list(transition_record(GYO, GYP, GYQ)),
transition_record(GYO, GYP, GYQ),
gleam@option:option(integer())
) -> list(transition_record(GYO, GYP, GYQ)).
append(History, Record, Limit) ->
New_history = [Record | History],
case Limit of
none ->
New_history;
{some, N} ->
gleam@list:take(New_history, N)
end.
-file("src/scamper/history.gleam", 49).
?DOC(" Filter history records by the source state.\n").
-spec filter_by_from(list(transition_record(GZD, GZE, GZF)), GZD) -> list(transition_record(GZD, GZE, GZF)).
filter_by_from(History, From) ->
gleam@list:filter(
History,
fun(Record) -> erlang:element(2, Record) =:= From end
).
-file("src/scamper/history.gleam", 57).
?DOC(" Filter history records by the destination state.\n").
-spec filter_by_to(list(transition_record(GZO, GZP, GZQ)), GZO) -> list(transition_record(GZO, GZP, GZQ)).
filter_by_to(History, To) ->
gleam@list:filter(
History,
fun(Record) -> erlang:element(4, Record) =:= To end
).
-file("src/scamper/history.gleam", 65).
?DOC(" Filter history records by the event.\n").
-spec filter_by_event(list(transition_record(GZZ, HAA, HAB)), HAA) -> list(transition_record(GZZ, HAA, HAB)).
filter_by_event(History, Event) ->
gleam@list:filter(
History,
fun(Record) -> erlang:element(3, Record) =:= Event end
).
-file("src/scamper/history.gleam", 73).
?DOC(" Get the last n records from history (newest first).\n").
-spec last_n(list(transition_record(HAK, HAL, HAM)), integer()) -> list(transition_record(HAK, HAL, HAM)).
last_n(History, N) ->
gleam@list:take(History, N).