Current section

Files

Jump to
gsv src gsv.erl
Raw

src/gsv.erl

-module(gsv).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([to_lists/1, to_dicts/1, from_lists/3, from_dicts/3]).
-export_type([line_ending/0]).
-type line_ending() :: windows | unix.
-file("/Users/benjamin/Personal/gsv/src/gsv.gleam", 15).
-spec to_lists(binary()) -> {ok, list(list(binary()))} | {error, binary()}.
to_lists(Input) ->
_pipe = Input,
_pipe@1 = gsv@internal@token:scan(_pipe),
_pipe@2 = gsv@internal@token:with_location(_pipe@1),
_pipe@3 = gsv@internal@ast:parse(_pipe@2),
gleam@result:map_error(
_pipe@3,
fun(E) ->
{parse_error, {location, Line, Column}, Msg} = E,
<<<<<<<<<<<<"["/utf8, "line "/utf8>>/binary,
(gleam@int:to_string(Line))/binary>>/binary,
" column "/utf8>>/binary,
(gleam@int:to_string(Column))/binary>>/binary,
"] of csv: "/utf8>>/binary,
Msg/binary>>
end
).
-file("/Users/benjamin/Personal/gsv/src/gsv.gleam", 40).
-spec to_dicts(binary()) -> {ok, list(gleam@dict:dict(binary(), binary()))} |
{error, binary()}.
to_dicts(Input) ->
gleam@result:'try'(to_lists(Input), fun(Lol) -> _pipe = case Lol of
[] ->
[];
[Headers | Rows] ->
Headers@1 = gleam@list:index_fold(
Headers,
gleam@dict:new(),
fun(Acc, X, I) ->
case gleam@string:trim(X) =:= <<""/utf8>> of
true ->
Acc;
false ->
gleam@dict:insert(Acc, I, X)
end
end
),
gleam@list:map(
Rows,
fun(Row) ->
gleam@list:index_fold(
Row,
gleam@dict:new(),
fun(Acc@1, X@1, I@1) ->
case gleam@dict:get(Headers@1, I@1) of
{error, nil} ->
Acc@1;
{ok, H} ->
case gleam@string:trim(X@1) of
<<""/utf8>> ->
Acc@1;
T ->
gleam@dict:insert(
Acc@1,
gleam@string:trim(H),
T
)
end
end
end
)
end
)
end,
{ok, _pipe} end).
-file("/Users/benjamin/Personal/gsv/src/gsv.gleam", 77).
-spec le_to_string(line_ending()) -> binary().
le_to_string(Le) ->
case Le of
windows ->
<<"\r\n"/utf8>>;
unix ->
<<"\n"/utf8>>
end.
-file("/Users/benjamin/Personal/gsv/src/gsv.gleam", 89).
-spec from_lists(list(list(binary())), binary(), line_ending()) -> binary().
from_lists(Input, Separator, Line_ending) ->
_pipe = Input,
_pipe@1 = gleam@list:map(
_pipe,
fun(Row) ->
gleam@list:map(
Row,
fun(Entry) ->
Entry@1 = gleam@string:replace(
Entry,
<<"\""/utf8>>,
<<"\"\""/utf8>>
),
case (gleam_stdlib:contains_string(Entry@1, Separator)
orelse gleam_stdlib:contains_string(Entry@1, <<"\n"/utf8>>))
orelse gleam_stdlib:contains_string(Entry@1, <<"\""/utf8>>) of
true ->
<<<<"\""/utf8, Entry@1/binary>>/binary, "\""/utf8>>;
false ->
Entry@1
end
end
)
end
),
_pipe@2 = gleam@list:map(
_pipe@1,
fun(Row@1) -> gleam@string:join(Row@1, Separator) end
),
gleam@string:join(_pipe@2, le_to_string(Line_ending)).
-file("/Users/benjamin/Personal/gsv/src/gsv.gleam", 120).
-spec from_dicts(
list(gleam@dict:dict(binary(), binary())),
binary(),
line_ending()
) -> binary().
from_dicts(Input, Separator, Line_ending) ->
case Input of
[] ->
<<""/utf8>>;
_ ->
Headers = begin
_pipe = Input,
_pipe@1 = gleam@list:map(_pipe, fun gleam@dict:keys/1),
_pipe@2 = gleam@list:flatten(_pipe@1),
_pipe@3 = gleam@list:unique(_pipe@2),
gleam@list:sort(_pipe@3, fun gleam@string:compare/2)
end,
Rows = begin
_pipe@4 = gleam@list:map(
Input,
fun(Row) ->
gleam@list:fold(
Headers,
[],
fun(Acc, H) -> case gleam@dict:get(Row, H) of
{ok, V} ->
[V | Acc];
{error, nil} ->
[<<""/utf8>> | Acc]
end end
)
end
),
gleam@list:map(_pipe@4, fun gleam@list:reverse/1)
end,
from_lists([Headers | Rows], Separator, Line_ending)
end.