-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("/home/benjaminpeinhardt/Desktop/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("/home/benjaminpeinhardt/Desktop/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("/home/benjaminpeinhardt/Desktop/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("/home/benjaminpeinhardt/Desktop/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("/home/benjaminpeinhardt/Desktop/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>>; [First_row | _] -> Headers = begin _pipe = First_row, _pipe@1 = maps:to_list(_pipe), _pipe@2 = gleam@list:map(_pipe@1, fun gleam@pair:first/1), gleam@list:sort(_pipe@2, fun gleam@string:compare/2) end, Rows = gleam@list:map(Input, fun(Row) -> _pipe@3 = Row, _pipe@4 = maps:to_list(_pipe@3), _pipe@5 = gleam@list:sort( _pipe@4, fun(Lhs, Rhs) -> gleam@string:compare( gleam@pair:first(Lhs), gleam@pair:first(Rhs) ) end ), gleam@list:map(_pipe@5, fun gleam@pair:second/1) end), from_lists([Headers | Rows], Separator, Line_ending) end.