Skip to content

Commit

Permalink
Refactor internal namespacing/module hierarchy to make more sense, be…
Browse files Browse the repository at this point in the history
… easier to navigate
  • Loading branch information
bitwalker committed Jul 1, 2015
1 parent ae723aa commit 32eff73
Show file tree
Hide file tree
Showing 21 changed files with 104 additions and 103 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Timex.DateFormat do
defmodule Timex.Format.DateTime.DateFormat do
@moduledoc """
Date formatting and parsing.
Expand All @@ -10,10 +10,10 @@ defmodule Timex.DateFormat do
module. One can also implement custom formatters for use with this module.
"""
alias Timex.DateTime
alias Timex.DateFormat.Formatters.Formatter
alias Timex.DateFormat.Formatters.StrftimeFormatter
alias Timex.Parsers.DateFormat.Parser
alias Timex.Parsers.DateFormat.StrftimeParser
alias Timex.Format.DateTime.Formatter
alias Timex.Format.DateTime.Formatters.StrftimeFormatter
alias Timex.Parse.DateTime.Parser
alias Timex.Parse.DateTime.Parsers.StrftimeParser

@doc """
Converts date values to strings according to the given template (aka format string).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
defmodule Timex.Parsers.DateFormat.Directive do
defmodule Timex.Format.DateTime.Directive do
@moduledoc """
This module defines parsing directives for all date/time
tokens timex knows about. It is composed of a Directive struct,
containing the rules for parsing a given token, and a `get/1`
function, which fetches a directive for a given token value, i.e. `:year4`.
"""
alias Timex.DateFormat.Formats
alias Timex.Parsers.DateFormat.Directive
alias Timex.Format.DateTime.Formats
alias Timex.Format.DateTime.Directive

require Formats
require Formats.Regex
Expand Down
6 changes: 3 additions & 3 deletions lib/dateformat/formats.ex → lib/format/datetime/formats.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Timex.DateFormat.Formats do
defmodule Timex.Format.DateTime.Formats do
@moduledoc """
This module defines all known (by timex) common date/time formats, in macro form.
Expand All @@ -10,8 +10,8 @@ defmodule Timex.DateFormat.Formats do
These formats are consumed by the datetime string parsers, by first tokenizing the chosen
format, then parsing the datetime string using those tokens.
"""
alias Timex.Parsers.DateFormat.Tokenizers.Default
alias Timex.Parsers.DateFormat.Tokenizers.Strftime
alias Timex.Format.DateTime.Tokenizers.Default
alias Timex.Format.DateTime.Tokenizers.Strftime

# For now, all preformatted strings will be tokenized using the Default tokenizer.
@tokenizer {:tokenizer, Default}
Expand Down
12 changes: 6 additions & 6 deletions lib/dateformat/formatter.ex → lib/format/datetime/formatter.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
defmodule Timex.DateFormat.Formatters.Formatter do
defmodule Timex.Format.DateTime.Formatter do
use Behaviour

alias Timex.Date
alias Timex.Time
alias Timex.DateTime
alias Timex.DateFormat.FormatError
alias Timex.DateFormat.Formatters.DefaultFormatter
alias Timex.Parsers.DateFormat.Directive
alias Timex.Format.FormatError
alias Timex.Format.DateTime.Formatters.DefaultFormatter
alias Timex.Format.DateTime.Directive

defcallback tokenize(format_string :: String.t) :: {:ok, [%Directive{}]} | {:error, term}
defcallback format(date :: %DateTime{}, format_string :: String.t) :: {:ok, String.t} | {:error, term}
Expand All @@ -15,9 +15,9 @@ defmodule Timex.DateFormat.Formatters.Formatter do
@doc false
defmacro __using__(_opts) do
quote do
@behaviour Timex.DateFormat.Formatters.Formatter
@behaviour Timex.Format.DateTime.Formatter

import Timex.DateFormat.Formatters.Formatter, only: [format_token: 2]
import Timex.Format.DateTime.Formatter, only: [format_token: 2]
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Timex.DateFormat.Formatters.DefaultFormatter do
defmodule Timex.Format.DateTime.Formatters.DefaultFormatter do
@moduledoc """
Date formatting language used by default by the `DateFormat` module.
Expand Down Expand Up @@ -107,13 +107,13 @@ defmodule Timex.DateFormat.Formatters.DefaultFormatter do
* `{kitchen}` - e.g. `3:25PM`
"""
use Timex.DateFormat.Formatters.Formatter
use Timex.Format.DateTime.Formatter

alias Timex.DateTime
alias Timex.Timezone
alias Timex.DateFormat.FormatError
alias Timex.Parsers.DateFormat.Directive
alias Timex.Parsers.DateFormat.Tokenizers.Default, as: Tokenizer
alias Timex.Format.FormatError
alias Timex.Format.DateTime.Directive
alias Timex.Format.DateTime.Tokenizers.Default, as: Tokenizer

@spec tokenize(String.t) :: {:ok, [%Directive{}]} | {:error, term}
defdelegate tokenize(format_string), to: Tokenizer
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Timex.DateFormat.Formatters.StrftimeFormatter do
defmodule Timex.Format.DateTime.Formatters.StrftimeFormatter do
@moduledoc """
Date formatting language defined by the `strftime` function from the Standard
C Library.
Expand Down Expand Up @@ -95,12 +95,12 @@ defmodule Timex.DateFormat.Formatters.StrftimeFormatter do
* `%v` - same as `%e-%b-%Y`
"""
use Timex.DateFormat.Formatters.Formatter
use Timex.Format.DateTime.Formatter

alias Timex.DateTime
alias Timex.Parsers.DateFormat.Directive
alias Timex.DateFormat.Formatters.DefaultFormatter
alias Timex.Parsers.DateFormat.Tokenizers.Strftime, as: Tokenizer
alias Timex.Format.DateTime.Directive
alias Timex.Format.DateTime.Formatters.DefaultFormatter
alias Timex.Format.DateTime.Tokenizers.Strftime, as: Tokenizer

@spec tokenize(String.t) :: {:ok, [%Directive{}]} | {:error, term}
defdelegate tokenize(format_string), to: Tokenizer
Expand All @@ -117,4 +117,4 @@ defmodule Timex.DateFormat.Formatters.StrftimeFormatter do
def format(%DateTime{} = date, format_string) do
DefaultFormatter.format(date, format_string, Tokenizer)
end
end
end
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
defmodule Timex.Parsers.DateFormat.Tokenizers.Default do
defmodule Timex.Format.DateTime.Tokenizers.Default do
@moduledoc """
Responsible for tokenizing date/time format strings
which use the Default formatter.
"""
alias Timex.Parsers.DateFormat.ParserState, as: State
alias Timex.Parsers.DateFormat.Directive, as: Directive
alias Timex.Parse.DateTime.ParserState, as: State
alias Timex.Format.DateTime.Directive, as: Directive

# These are all the default formatter's directives
@directives [
Expand Down Expand Up @@ -100,7 +100,7 @@ defmodule Timex.Parsers.DateFormat.Tokenizers.Default do
case get_directive(token) do
:invalid -> {:error, "Invalid token beginning at column #{state.start_index}!"}
{_, %Directive{} = directive} ->
state = %{state |
state = %{state |
:col => state.col + 1,
:padding => 0,
:token => "",
Expand Down Expand Up @@ -133,4 +133,4 @@ defmodule Timex.Parsers.DateFormat.Tokenizers.Default do
defp get_directive(dir) do
List.keyfind(@directives, dir, 0) || :invalid
end
end
end
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
defmodule Timex.Parsers.DateFormat.Tokenizers.Strftime do
defmodule Timex.Format.DateTime.Tokenizers.Strftime do
@moduledoc """
Responsible for tokenizing date/time format strings
which use the strftime formatter.
"""
alias Timex.Parsers.DateFormat.ParserState, as: State
alias Timex.Parsers.DateFormat.Directive, as: Directive
alias Timex.Parse.DateTime.ParserState, as: State
alias Timex.Format.DateTime.Directive, as: Directive

# These are all the strftime formatter's directives
@directives [
Expand Down Expand Up @@ -177,4 +177,4 @@ defmodule Timex.Parsers.DateFormat.Tokenizers.Strftime do

defp non_empty(""), do: false
defp non_empty(_), do: true
end
end
6 changes: 3 additions & 3 deletions lib/dateformat/format_error.ex → lib/format/format_error.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
defmodule Timex.DateFormat.FormatError do
defmodule Timex.Format.FormatError do
@moduledoc """
Used for errors encountered during date formatting.
"""
alias Timex.DateFormat.FormatError
alias Timex.Format.FormatError

defexception message: "Invalid format!"

def exception([message: message]) do
%FormatError{message: message}
end
end
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Timex.TimeFormatter do
defmodule Timex.Format.Time.TimeFormatter do
@moduledoc """
Handles formatting timestamp values as human readable strings.
For formatting timestamps as points in time rather than intervals,
Expand Down
20 changes: 11 additions & 9 deletions lib/parsers/dateformat/parser.ex → lib/parse/datetime/parser.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Timex.Parsers.DateFormat.Parser do
defmodule Timex.Parse.DateTime.Parser do
@moduledoc """
This is the base plugin behavior for all Timex date/time string parsers.
"""
Expand All @@ -8,8 +8,10 @@ defmodule Timex.Parsers.DateFormat.Parser do
alias Timex.Time
alias Timex.DateTime
alias Timex.Timezone
alias Timex.Parsers.ParseError
alias Timex.Parsers.DateFormat.Directive
alias Timex.Parse.ParseError
alias Timex.Format.DateTime.Directive
alias Timex.Parse.DateTime.Parsers.DefaultParser
alias Timex.Parse.DateTime.Parser

# Tokenizes the format string
defcallback tokenize(format_string :: binary) :: [%Directive{}] | {:error, term}
Expand All @@ -21,9 +23,9 @@ defmodule Timex.Parsers.DateFormat.Parser do
@doc false
defmacro __using__(_opts) do
quote do
@behaviour Timex.Parsers.DateFormat.Parser
@behaviour Timex.Parse.DateTime.Parser

import Timex.Parsers.DateFormat.Parser
import Timex.Parse.DateTime.Parser

@whitespace [32, ?\t, ?\n, ?\r]
@numerics ?0..?9 |> Enum.to_list
Expand All @@ -48,7 +50,7 @@ defmodule Timex.Parsers.DateFormat.Parser do
@spec parse(binary, binary) :: {:ok, %DateTime{}} | {:error, term}
def parse(date_string, format_string)
when is_binary(date_string) and is_binary(format_string),
do: parse(date_string, format_string, Timex.Parsers.DateFormat.DefaultParser)
do: parse(date_string, format_string, DefaultParser)

@doc """
Parses a date/time string using the provided parser. Must implement the
Expand All @@ -60,7 +62,7 @@ defmodule Timex.Parsers.DateFormat.Parser do
%Date{year: 2014, month: 7, day: 29, hour: 0, minute: 20, second: 41, ms: 196, tz: %Timezone{name: "CST"}}
"""
@spec parse(binary, binary, Timex.Parsers.DateFormat.Parser) :: {:ok, %DateTime{}} | {:error, term}
@spec parse(binary, binary, Parser) :: {:ok, %DateTime{}} | {:error, term}
def parse(date_string, format_string, parser)
when is_binary(date_string) and is_binary(format_string)
do
Expand All @@ -83,8 +85,8 @@ defmodule Timex.Parsers.DateFormat.Parser do
@doc """
Same as `parse/2` and `parse/3`, but raises on error.
"""
@spec parse!(String.t, String.t, Timex.Parsers.DateFormat.Parser | nil) :: %DateTime{} | no_return
def parse!(date_string, format_string, parser \\ Timex.Parsers.DateFormat.DefaultParser)
@spec parse!(String.t, String.t, Parser | nil) :: %DateTime{} | no_return
def parse!(date_string, format_string, parser \\ DefaultParser)
when is_binary(date_string) and is_binary(format_string)
do
case parse(date_string, format_string, parser) do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
defmodule Timex.Parsers.DateFormat.ParserState do
defmodule Timex.Parse.DateTime.ParserState do
@derive Access
defstruct col: 0, # The current column number
start_index: 0, # The last index of a starting token
padding: 0, # The amount of padding for the current token
pad_type: :zero, # The character to use for padding, :zero or :space
token: "", # The current state of the parsed token
tokens: [] # A keyword list of tokens
end
end
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
defmodule Timex.Parsers.DateFormat.DefaultParser do
defmodule Timex.Parse.DateTime.Parsers.DefaultParser do
@moduledoc """
This module is responsible for parsing date strings using
the default timex formatting syntax.
See `Timex.DateFormat.Formatters.DefaultFormatter` for more info.
"""
use Timex.Parsers.DateFormat.Parser
use Timex.Parse.DateTime.Parser

alias Timex.Parsers.DateFormat.Directive
alias Timex.Format.DateTime.Directive
alias Timex.DateTime
alias Timex.TimezoneInfo

@doc """
The tokenizer used by this parser.
"""
defdelegate tokenize(format_string), to: Timex.Parsers.DateFormat.Tokenizers.Default
defdelegate tokenize(format_string), to: Timex.Format.DateTime.Tokenizers.Default

@doc """
Constructs a DateTime from the parsed tokens
Expand All @@ -36,7 +36,7 @@ defmodule Timex.Parsers.DateFormat.DefaultParser do
end
defp apply_directives([], %DateTime{} = date), do: {:ok, date}
defp apply_directives([{token, value}|tokens], %DateTime{} = date) do
case Timex.Parsers.DateFormat.Parser.update_date(date, token, value) do
case Timex.Parse.DateTime.Parser.update_date(date, token, value) do
{:error, _} = error -> error
updated -> apply_directives(tokens, updated)
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
defmodule Timex.Parsers.DateFormat.StrftimeParser do
defmodule Timex.Parse.DateTime.Parsers.StrftimeParser do
@moduledoc """
This module is responsible for parsing date strings using
the strftime formatting syntax.
See `Timex.DateFormat.Formatters.StrftimeFormatter` for more info.
"""
use Timex.Parsers.DateFormat.Parser
use Timex.Parse.DateTime.Parser

@doc """
The tokenizer used by this parser.
"""
defdelegate tokenize(format_string), to: Timex.Parsers.DateFormat.Tokenizers.Strftime
defdelegate tokenize(format_string), to: Timex.Format.DateTime.Tokenizers.Strftime

@doc """
Extracts the value for a given directive.
"""
defdelegate parse_directive(date_string, directive), to: Timex.Parsers.DateFormat.DefaultParser
defdelegate parse_directive(date_string, directive), to: Timex.Parse.DateTime.Parsers.DefaultParser

@doc """
Constructs a DateTime from the parsed tokens
"""
defdelegate apply_directives(tokens), to: Timex.Parsers.DateFormat.DefaultParser
end
defdelegate apply_directives(tokens), to: Timex.Parse.DateTime.Parsers.DefaultParser
end
6 changes: 3 additions & 3 deletions lib/parsers/parse_error.ex → lib/parse/parse_error.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
defmodule Timex.Parsers.ParseError do
defmodule Timex.Parse.ParseError do
@moduledoc """
Used for errors encountered during date parsing.
"""
alias Timex.Parsers.ParseError
alias Timex.Parse.ParseError

defexception message: "Invalid input!"

def exception(message) do
%ParseError{message: message}
end
end
end
Loading

0 comments on commit 32eff73

Please sign in to comment.