Skip to content

Commit

Permalink
Return telegram struct directly from parser
Browse files Browse the repository at this point in the history
  • Loading branch information
robinvdvleuten committed Mar 20, 2024
1 parent 489fa44 commit cf335c9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/dsmr.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ defmodule DSMR do
defp do_parse(string, options) do
try do
case Parser.parse(string, options) do
{:ok, header, data, checksum} ->
{:ok, %Telegram{header: header, data: data, checksum: checksum}}
{:ok, _telegram} = result ->
result

{:error, reason, rest} ->
{:error, format_parse_error({:parser, reason, rest})}
Expand Down
13 changes: 8 additions & 5 deletions lib/dsmr/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule DSMR.Parser do

import NimbleParsec

alias DSMR.{Measurement, Timestamp}
alias DSMR.{Measurement, Telegram, Timestamp}

eol = ascii_char([?\r]) |> ascii_char([?\n])

Expand Down Expand Up @@ -102,21 +102,24 @@ defmodule DSMR.Parser do
|> ignore(eol)

@spec parse(binary(), keyword()) ::
{:ok, binary(), [any()], binary()}
| {:error, binary(), {integer(), non_neg_integer()}}
def parse(input, options \\ []) do
{:ok, Telegram.t()} | {:error, binary(), binary()}
def parse(input, options) do
tokenize_opts = [context: %{floats: Keyword.get(options, :floats, :native)}]

case do_parse(input, tokenize_opts) do
{:ok, tokens, "", _, _, _} ->
[{:header, header}, {:objects, data}, {:footer, checksum}] = tokens
{:ok, header, data, checksum}
{:ok, %Telegram{header: header, data: data, checksum: checksum}}

{:error, reason, rest, _, _, _} ->
{:error, reason, rest}
end
end

@spec do_parse(binary()) ::
{:ok, [any()], binary(), map(), {pos_integer(), pos_integer()}, pos_integer()}
| {:error, String.t(), String.t(), map(), {non_neg_integer(), non_neg_integer()},
non_neg_integer()}
defparsecp(:do_parse, telegram, inline: true)

defp object_token(rest, [value, {:obis, obis}], context, _line, _offset) do
Expand Down

0 comments on commit cf335c9

Please sign in to comment.