rdf-ex/lib/rdf/serialization/decoder.ex

45 lines
1.3 KiB
Elixir
Raw Normal View History

defmodule RDF.Serialization.Decoder do
@moduledoc """
A behaviour for decoders of strings encoded in a specific `RDF.Serialization` format.
"""
2020-03-10 00:37:53 +00:00
alias RDF.{Dataset, Graph}
@doc """
Decodes a serialized `RDF.Graph` or `RDF.Dataset` from the given string.
It returns an `{:ok, data}` tuple, with `data` being the deserialized graph or
dataset, or `{:error, reason}` if an error occurs.
"""
2020-03-10 00:37:53 +00:00
@callback decode(String.t, keyword | map) :: {:ok, Graph.t | Dataset.t} | {:error, any}
@doc """
Decodes a serialized `RDF.Graph` or `RDF.Dataset` from the given string.
As opposed to `decode`, it raises an exception if an error occurs.
Note: The `__using__` macro automatically provides an overridable default
implementation based on the non-bang `decode` function.
"""
2020-03-10 00:37:53 +00:00
@callback decode!(String.t, keyword | map) :: RDF.Graph.t | RDF.Dataset.t
defmacro __using__(_) do
quote bind_quoted: [], unquote: true do
@behaviour unquote(__MODULE__)
2018-09-17 00:08:16 +00:00
@impl unquote(__MODULE__)
2020-03-10 21:44:54 +00:00
@spec decode!(String.t, keyword | map) :: RDF.Graph.t | RDF.Dataset.t
def decode!(content, opts \\ []) do
case decode(content, opts) do
{:ok, data} -> data
{:error, reason} -> raise reason
end
end
defoverridable [decode!: 2]
end
end
end