Gleam-inspired typed configuration language (POC)
1%% Stdio helpers for the Glint language server.
2%% Only used on the Erlang target.
3
4-module(glint_lsp_ffi).
5-export([read_bytes/1, write_bytes/1, read_line/0]).
6
7%% Read exactly N bytes from stdin as a bit array / binary.
8read_bytes(N) when is_integer(N), N >= 0 ->
9 case file:read(standard_io, N) of
10 {ok, Data} when is_binary(Data) ->
11 {ok, Data};
12 {ok, Data} when is_list(Data) ->
13 {ok, list_to_binary(Data)};
14 eof ->
15 {error, <<"eof">>};
16 {error, Reason} ->
17 {error, iolist_to_binary(io_lib:format("~p", [Reason]))}
18 end.
19
20%% Write raw bytes to stdout (must not go through the logger).
21write_bytes(Data) when is_binary(Data) ->
22 ok = file:write(standard_io, Data),
23 ok;
24write_bytes(Data) when is_list(Data) ->
25 ok = file:write(standard_io, list_to_binary(Data)),
26 ok.
27
28%% Read one line from stdin (including the trailing newline when present).
29read_line() ->
30 case io:get_line("") of
31 eof ->
32 {error, <<"eof">>};
33 {error, Reason} ->
34 {error, iolist_to_binary(io_lib:format("~p", [Reason]))};
35 Line when is_list(Line) ->
36 {ok, unicode:characters_to_binary(Line)};
37 Line when is_binary(Line) ->
38 {ok, Line}
39 end.