Skip to content

Commit

Permalink
WIP: allow user to be able to control the parse state.
Browse files Browse the repository at this point in the history
  • Loading branch information
diasbruno committed Oct 7, 2023
1 parent 0b23904 commit b8a55ae
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions lib/nimble_csv.ex
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,16 @@ defmodule NimbleCSV do

Stream.transform(
stream,
fn -> state end,
&parse(maybe_to_utf8(&1), &2, separator, escape),
fn -> {0, state} end,
&to_enum(parse(maybe_to_utf8(&1), &2, separator, escape),
&2,
fn {count, parse_state}, kind -> do
case kind do
:line -> {count + 1, :line}
:header -> {count, :line}
end
end
),
&finalize_parser/1
)
end
Expand All @@ -351,7 +359,15 @@ defmodule NimbleCSV do
Enum.flat_map_reduce(
enumerable,
state,
&parse(maybe_to_utf8(&1), &2, separator, escape)
&to_enum(parse(maybe_to_utf8(&1), &2, separator, escape),
&2,
fn {count, parse_state}, kind -> do
case kind do
:line -> {count + 1, :line}
:header -> {count, :line}
end
end
)
)

finalize_parser(state)
Expand Down Expand Up @@ -442,20 +458,20 @@ defmodule NimbleCSV do
:ok
end

defp to_enum(result) do
defp to_enum(result, state, fstate) do
case result do
{:line, row} -> {[row], :line}
{:header, _} -> {[], :line}
{:line, row} -> {[row], fstate(state, :line)}
{:header, _} -> {[], fstate(state, :header)}
{:escape, _, _, _} = escape -> {[], escape}
end
end

defp parse(line, {:escape, entry, row, state}, separator, escape) do
to_enum(escape(line, entry, row, state, separator, escape))
escape(line, entry, row, state, separator, escape)
end

defp parse(line, state, separator, escape) do
to_enum(separator(line, [], state, separator, escape))
separator(line, [], state, separator, escape)
end

defmacrop newlines_separator!() do
Expand Down

0 comments on commit b8a55ae

Please sign in to comment.