aboutsummaryrefslogtreecommitdiffstats
path: root/subsystem
diff options
context:
space:
mode:
authorMarcos Magueta <maguetamarcos@gmail.com>2026-10-24 16:09:32 -0300
committerMarcos Magueta <maguetamarcos@gmail.com>2026-10-24 16:09:32 -0300
commit7f48fa65f314f0105276689a0d708929106fe392 (patch)
treeafa38ab5ead523e962f2484c7cea4bdff4fe8dfb /subsystem
parent9e158f5dc42cbc51fa89e3627d2fd20918915f8b (diff)
downloadRNT-7f48fa65f314f0105276689a0d708929106fe392.tar.gz
RNT-7f48fa65f314f0105276689a0d708929106fe392.zip
rnt sending and receiving on erlang
Diffstat (limited to 'subsystem')
-rw-r--r--subsystem/karuta/rnt.erl27
1 files changed, 27 insertions, 0 deletions
diff --git a/subsystem/karuta/rnt.erl b/subsystem/karuta/rnt.erl
new file mode 100644
index 0000000..c94a01c
--- /dev/null
+++ b/subsystem/karuta/rnt.erl
@@ -0,0 +1,27 @@
+%% Tuples of an RNT relation, as Translator.ml writes and reads them:
+%% a tuple is {Type, #{Name => Value}}, its type, names, bitstrings
+%% and its integers no wider than an OCaml int.
+-module(rnt).
+-export([recv/1, send/1]).
+
+-define(MIN_INT, -(1 bsl 62)).
+-define(MAX_INT, (1 bsl 62) - 1).
+
+%% A tuple from the bytes RNT sent
+recv(Bytes) -> holdable(binary_to_term(Bytes, [safe])).
+
+%% The bytes RNT reads a tuple from
+send(Tuple) -> term_to_binary(holdable(Tuple)).
+
+%% The tuple, if RNT can hold it
+holdable({Type, Attributes} = Tuple) when is_binary(Type), is_map(Attributes) ->
+ case lists:all(fun attribute/1, maps:to_list(Attributes)) of
+ true -> Tuple;
+ false -> error(badarg, [Tuple])
+ end;
+holdable(Tuple) -> error(badarg, [Tuple]).
+
+attribute({Name, Value}) when is_binary(Name), is_binary(Value) -> true;
+attribute({Name, Value}) when is_binary(Name), is_integer(Value) ->
+ Value >= ?MIN_INT andalso Value =< ?MAX_INT;
+attribute(_) -> false.