2017-05-19 16:10:32 +00:00
|
|
|
defmodule RDF.Serialization.Encoder do
|
2017-06-16 15:33:05 +00:00
|
|
|
@moduledoc """
|
|
|
|
A behaviour for encoders of `RDF.Graph`s or `RDF.Dataset`s in a specific
|
|
|
|
`RDF.Serialization` format.
|
|
|
|
"""
|
2017-05-19 16:10:32 +00:00
|
|
|
|
2020-03-10 00:37:53 +00:00
|
|
|
alias RDF.{Dataset, Graph}
|
2017-06-16 15:33:05 +00:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Encodes a `RDF.Graph` or `RDF.Dataset`.
|
|
|
|
|
|
|
|
It returns an `{:ok, string}` tuple, with `string` being the serialized
|
|
|
|
`RDF.Graph` or `RDF.Dataset`, or `{:error, reason}` if an error occurs.
|
|
|
|
"""
|
2020-03-10 00:37:53 +00:00
|
|
|
@callback encode(Graph.t | Dataset.t, keyword | map) :: {:ok, String.t} | {:error, any}
|
2017-06-16 15:33:05 +00:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Encodes a `RDF.Graph` or `RDF.Dataset`.
|
|
|
|
|
|
|
|
As opposed to `encode`, it raises an exception if an error occurs.
|
|
|
|
|
|
|
|
Note: The `__using__` macro automatically provides an overridable default
|
|
|
|
implementation based on the non-bang `encode` function.
|
|
|
|
"""
|
2020-03-10 00:37:53 +00:00
|
|
|
@callback encode!(Graph.t | Dataset.t, keyword | map) :: String.t
|
2017-05-19 16:10:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
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 00:37:53 +00:00
|
|
|
@dialyzer {:nowarn_function, encode!: 2}
|
|
|
|
@spec encode!(Graph.t | Dataset.t, keyword) :: String.t
|
2017-05-19 16:10:32 +00:00
|
|
|
def encode!(data, opts \\ []) do
|
|
|
|
case encode(data, opts) do
|
|
|
|
{:ok, data} -> data
|
|
|
|
{:error, reason} -> raise reason
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-30 23:14:11 +00:00
|
|
|
defoverridable [encode!: 1]
|
2017-05-19 16:10:32 +00:00
|
|
|
defoverridable [encode!: 2]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|