Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support SAX handler as a function #96

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/saxy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ defmodule Saxy do
alias Saxy.{
Encoder,
Parser,
State
Parser.State
}

@doc ~S"""
Expand Down
8 changes: 6 additions & 2 deletions lib/saxy/emitter.ex
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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: [?&]
Expand Down
2 changes: 1 addition & 1 deletion lib/saxy/state.ex → lib/saxy/parser/state.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Saxy.State do
defmodule Saxy.Parser.State do
@moduledoc false

@enforce_keys [
Expand Down
2 changes: 1 addition & 1 deletion lib/saxy/partial.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions test/saxy/emitter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down