Current section
Files
Jump to
Current section
Files
lib/fast_atom.ex
defmodule FastAtom do
@moduledoc """
Parse Atom quickly using a Rust NIF.
"""
defmodule Native do
@moduledoc false
use Rustler, otp_app: :fast_atom, crate: "fastatom"
# When your NIF is loaded, it will override this function.
def parse(_a), do: :erlang.nif_error(:nif_not_loaded)
end
@doc """
Parse a RSS string into a map.
"""
@spec parse(String.t()) :: {:ok, map()} | {:error, String.t()}
def parse(""), do: {:error, "Cannot parse blank string"}
def parse(rss_string) when is_binary(rss_string) do
rss_string
|> Native.parse()
|> map_to_tuple()
end
def parse(_somethig_else), do: {:error, "Atom feed must be passed in as a string"}
defp map_to_tuple(%{"Ok" => map}), do: {:ok, map}
defp map_to_tuple({:ok, map}), do: {:ok, map}
defp map_to_tuple(%{"Err" => msg}), do: {:error, msg}
defp map_to_tuple({:error, msg}), do: {:error, msg}
end