summaryrefslogtreecommitdiff
path: root/lib/irc/parser/capabs.ex
blob: 1448d75c37f8563034be75265c3b60dd1f909526 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
defmodule Irc.Parser.Capabs do
  @moduledoc "Helper to parse capability lists"
  @type t :: %{capab_name() => capab_value()}
  @type capab_name :: String.t()
  @type capab_value :: true | any()

  @spec parse(String.t()) :: t()
  def parse(string) do
    string
    |> String.split(" ")
    |> Enum.map(&parse_capab/1)
    |> Enum.into(Map.new)
  end

  defp parse_capab(string) do
    case String.split(string, "=", parts: 2) do
      [capab] ->
        {capab, true}
      [capab, args] ->
        parse_capab_with_args(capab, args)
    end
  end

  defp parse_capab_with_args(capab, args) do
    args = args
     |> String.split(",")
     |> Map.reduce(%{}, fn(k_v, acc) ->
        [k, v] = String.split(k_v, "=")
        Map.put(acc, k, v)
     end)
    {capab, args}
  end

end