-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* correctly parses command and emit errors * correctly builds a CLI struct * split parser error
- Loading branch information
Zoey de Souza Pessanha
committed
Aug 26, 2023
1 parent
ae5c7fe
commit a58e238
Showing
10 changed files
with
113 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
defmodule Nexus.Command.Input do | ||
@moduledoc """ | ||
Define a structure to easy pattern matching the input | ||
on commands dispatched | ||
""" | ||
|
||
@type t :: %__MODULE__{value: term, raw: list(binary())} | ||
|
||
@enforce_keys ~w(value raw)a | ||
defstruct value: nil, raw: nil | ||
|
||
@spec parse!(term, list(binary())) :: Nexus.Command.Input.t() | ||
def parse!(value, raw) do | ||
%__MODULE__{value: value, raw: raw} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# credo:disable-for-next-line | ||
defmodule Nexus.FailedCommandParsing do | ||
defexception [:message] | ||
|
||
@impl true | ||
def exception(reason) do | ||
%__MODULE__{message: "Error parsing command: #{reason}"} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
defmodule Nexus.Parser do | ||
@moduledoc "Should parse the command and return the value" | ||
|
||
alias Nexus.Command | ||
alias Nexus.FailedCommandParsing, as: Error | ||
|
||
@spec command_from_raw!(Command.t(), binary | list(binary)) :: {term, list(binary)} | ||
def command_from_raw!(cmd, raw) when is_binary(raw) do | ||
command_from_raw!(cmd, String.split(raw, ~r/\s/)) | ||
end | ||
|
||
def command_from_raw!(%Command{name: name, type: t}, args) when is_list(args) do | ||
ns = to_string(name) | ||
|
||
case args do | ||
[^ns, value | args] -> {string_to!(value, t), args} | ||
args -> raise "Failed to parse command #{ns} with args #{inspect(args)}" | ||
end | ||
end | ||
|
||
defp string_to!(raw, :string), do: raw | ||
|
||
defp string_to!(raw, :integer) do | ||
case Integer.parse(raw) do | ||
{int, ""} -> int | ||
_ -> raise Error, "#{raw} is not a valid integer" | ||
end | ||
end | ||
|
||
defp string_to!(raw, :float) do | ||
case Float.parse(raw) do | ||
{float, ""} -> float | ||
_ -> raise Error, "#{raw} is not a valid float" | ||
end | ||
end | ||
|
||
# final user should not be used very often | ||
defp string_to!(raw, :atom) do | ||
String.to_atom(raw) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters