-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
178 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
defmodule BADParser do | ||
@behaviour Parser | ||
|
||
@impl Parser | ||
def parse, do: {:ok, "something bad"} | ||
|
||
@impl Parser | ||
def extensions, do: ["bad"] | ||
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,9 @@ | ||
defmodule CSVParser do | ||
@behaviour Parser | ||
|
||
@impl Parser | ||
def parse(str), do: {:ok, "some csv " <> str} # ... parse CSV | ||
|
||
@impl Parser | ||
def extensions, do: [".csv"] | ||
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,26 @@ | ||
defmodule ExampleParser do | ||
@spec parse_path(Path.t(), [module()]) :: {:ok, term} | {:error, atom} | ||
def parse_path(filename, parsers) do | ||
with {:ok, ext} <- parse_extension(filename), | ||
{:ok, parser} <- find_parser(ext, parsers), | ||
{:ok, contents} <- File.read(filename) do | ||
parser.parse(contents) | ||
end | ||
end | ||
|
||
defp parse_extension(filename) do | ||
if ext = Path.extname(filename) do | ||
{:ok, ext} | ||
else | ||
{:error, :no_extension} | ||
end | ||
end | ||
|
||
defp find_parser(ext, parsers) do | ||
if parser = Enum.find(parsers, fn parser -> ext in parser.extensions() end) do | ||
{:ok, parser} | ||
else | ||
{:error, :no_matching_parser} | ||
end | ||
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,9 @@ | ||
defmodule JSONParser do | ||
@behaviour Parser | ||
|
||
@impl Parser | ||
def parse(str), do: {:ok, "some json " <> str} # ... parse JSON | ||
|
||
@impl Parser | ||
def extensions, do: [".json"] | ||
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,11 @@ | ||
defmodule Parser do | ||
@doc """ | ||
Parses a string. | ||
""" | ||
@callback parse(String.t) :: {:ok, term} | {:error, atom} | ||
|
||
@doc """ | ||
Lists all supported file extensions. | ||
""" | ||
@callback extensions() :: [String.t] | ||
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,9 @@ | ||
defmodule StringHelpers do | ||
@typedoc "A word from the dictionary" | ||
@type word() :: String.t() | ||
|
||
@spec long_word?(word()) :: boolean() | ||
def long_word?(word) when is_binary(word) do | ||
String.length(word) > 8 | ||
end | ||
end |