From d92a5a1ae60181fdf73db52a2817144a1edaad01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=E1=BA=A9m=20Hu=E1=BB=B3nh?= Date: Sat, 25 Sep 2021 21:03:53 +0200 Subject: [PATCH] WIP --- lib/saxy.ex | 2 +- lib/saxy/emitter.ex | 8 ++++++-- lib/saxy/{ => parser}/state.ex | 2 +- lib/saxy/partial.ex | 2 +- test/saxy/emitter_test.exs | 1 + 5 files changed, 10 insertions(+), 5 deletions(-) rename lib/saxy/{ => parser}/state.ex (87%) diff --git a/lib/saxy.ex b/lib/saxy.ex index ccc94e2..54bc4ee 100644 --- a/lib/saxy.ex +++ b/lib/saxy.ex @@ -113,7 +113,7 @@ defmodule Saxy do alias Saxy.{ Encoder, Parser, - State + Parser.State } @doc ~S""" diff --git a/lib/saxy/emitter.ex b/lib/saxy/emitter.ex index 5a244ba..f775ba1 100644 --- a/lib/saxy/emitter.ex +++ b/lib/saxy/emitter.ex @@ -1,7 +1,7 @@ defmodule Saxy.Emitter do @moduledoc false - alias Saxy.State + alias Saxy.Parser.State def emit(event_type, data, state, on_halt) do case emit(event_type, data, state) do @@ -22,10 +22,14 @@ defmodule Saxy.Emitter do end end - defp do_emit(event_type, data, handler, user_state) do + defp do_emit(event_type, data, handler, user_state) when is_atom(handler) do handler.handle_event(event_type, data, user_state) end + defp do_emit(event_type, data, handler, user_state) do + handler.(event_type, data, user_state) + end + @compile {:inline, [convert_entity_reference: 2]} def convert_entity_reference("amp", _state), do: [?&] diff --git a/lib/saxy/state.ex b/lib/saxy/parser/state.ex similarity index 87% rename from lib/saxy/state.ex rename to lib/saxy/parser/state.ex index 325de91..6079af3 100644 --- a/lib/saxy/state.ex +++ b/lib/saxy/parser/state.ex @@ -1,4 +1,4 @@ -defmodule Saxy.State do +defmodule Saxy.Parser.State do @moduledoc false @enforce_keys [ diff --git a/lib/saxy/partial.ex b/lib/saxy/partial.ex index 48bf2ac..12a830b 100644 --- a/lib/saxy/partial.ex +++ b/lib/saxy/partial.ex @@ -45,7 +45,7 @@ defmodule Saxy.Partial do character_data_max_length = Keyword.get(options, :character_data_max_length, :infinity) cdata_as_characters = Keyword.get(options, :cdata_as_characters, true) - state = %Saxy.State{ + state = %Parser.State{ prolog: nil, handler: handler, user_state: initial_state, diff --git a/test/saxy/emitter_test.exs b/test/saxy/emitter_test.exs index 4b083ac..235c96b 100644 --- a/test/saxy/emitter_test.exs +++ b/test/saxy/emitter_test.exs @@ -74,6 +74,7 @@ defmodule Saxy.EmitterTest do defp parse(data, handler, state) do assert result = Saxy.parse_string(data, handler, state) assert Saxy.parse_stream([data], handler, state) == result + assert Saxy.parse_stream([data], &handler.handle_event/3, state) == result result end