View Source Mahaul.Parser (Mahaul v0.5.0)
Module for parsing string to supported environment variables.
Link to this section Summary
Functions
Parses a string value into one of the supported types.
Link to this section Functions
@spec parse(binary(), Mahaul.Constants.var_type()) :: Mahaul.Constants.error_type() | Mahaul.Constants.success_type()
Parses a string value into one of the supported types.
Given a string value and the supported type configuration, the method tries to parse the string into the provided type. A success tuple is returned if parsing was successful, otherwise an error tuple is returned.
:str
iex> Mahaul.Parser.parse("some value", :str)
{:ok, "some value"}
:enum
iex> Mahaul.Parser.parse("atom", :enum)
{:ok, :atom}
iex> Mahaul.Parser.parse("value", :enum)
{:ok, :value}
:num
iex> Mahaul.Parser.parse("10", :num)
{:ok, 10.0}
iex> Mahaul.Parser.parse("10.2345", :num)
{:ok, 10.2345}
iex> Mahaul.Parser.parse("some value", :num)
{:error, nil}
:int
iex> Mahaul.Parser.parse("10", :int)
{:ok, 10}
iex> Mahaul.Parser.parse("10.2345", :int)
{:error, nil}
iex> Mahaul.Parser.parse("some value", :int)
{:error, nil}
:bool
iex> Mahaul.Parser.parse(true, :bool)
{:ok, true}
iex> Mahaul.Parser.parse(false, :bool)
{:ok, false}
iex> Mahaul.Parser.parse("true", :bool)
{:ok, true}
iex> Mahaul.Parser.parse("false", :bool)
{:ok, false}
iex> Mahaul.Parser.parse("1", :bool)
{:ok, true}
iex> Mahaul.Parser.parse("0", :bool)
{:ok, false}
:port
iex> Mahaul.Parser.parse("1", :port)
{:ok, 1}
iex> Mahaul.Parser.parse("65535", :port)
{:ok, 65535}
iex> Mahaul.Parser.parse("8080", :port)
{:ok, 8080}
iex> Mahaul.Parser.parse("some value", :port)
{:error, nil}
iex> Mahaul.Parser.parse("-1", :port)
{:error, nil}
iex> Mahaul.Parser.parse("0", :port)
{:error, nil}
iex> Mahaul.Parser.parse("65536", :port)
{:error, nil}
:host
iex> Mahaul.Parser.parse("//localhost", :host)
{:ok, "//localhost"}
iex> Mahaul.Parser.parse("//domain.com", :host)
{:ok, "//domain.com"}
iex> Mahaul.Parser.parse("//192.168.0.1", :host)
{:ok, "//192.168.0.1"}
iex> Mahaul.Parser.parse("ftp://localhost", :host)
{:error, nil}
iex> Mahaul.Parser.parse("https://domain.com", :host)
{:error, nil}
iex> Mahaul.Parser.parse("https", :host)
{:error, nil}
iex> Mahaul.Parser.parse("https://domain.com/something", :host)
{:error, nil}
:uri
iex> Mahaul.Parser.parse("ftp://localhost", :uri)
{:ok, "ftp://localhost"}
iex> Mahaul.Parser.parse("https://domain.com", :uri)
{:ok, "https://domain.com"}
iex> Mahaul.Parser.parse("postgresql://user:pass@localhost:5432/dev_db", :uri)
{:ok, "postgresql://user:pass@localhost:5432/dev_db"}
iex> Mahaul.Parser.parse("//localhost", :uri)
{:error, nil}
iex> Mahaul.Parser.parse("//domain.com", :uri)
{:error, nil}
iex> Mahaul.Parser.parse("//192.168.0.1", :uri)
{:error, nil}