-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from sunny-g/v0.2.0
update to elixir 1.6, replace macros with guards, mix format everything, unify `__using__` API
- Loading branch information
Showing
45 changed files
with
592 additions
and
591 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[ | ||
inputs: ["{config,lib,test}/**/*.{ex,exs}"] | ||
] |
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 |
---|---|---|
@@ -1,58 +1,50 @@ | ||
defmodule XDR.Type.DoubleFloat.Validation do | ||
@moduledoc false | ||
|
||
defmacro is_valid_double_float?(float) do | ||
quote do | ||
is_float(unquote(float)) or is_integer(unquote(float)) | ||
end | ||
end | ||
end | ||
|
||
defmodule XDR.Type.DoubleFloat do | ||
@moduledoc """ | ||
RFC 4506, Section 4.7 - Double-precision Floating Point | ||
""" | ||
|
||
alias XDR.Type.Base | ||
import XDR.Util.Macros | ||
import XDR.Type.DoubleFloat.Validation | ||
|
||
@behaviour XDR.Type.Base | ||
|
||
@typedoc """ | ||
Double-precision float | ||
""" | ||
@type t :: number | ||
@type xdr :: <<_ :: _ * 64>> | ||
@type xdr :: <<_::_*64>> | ||
|
||
@length 8 | ||
|
||
defguard is_xdr_double(double) when is_float(double) or is_integer(double) | ||
|
||
@doc false | ||
def length, do: @length | ||
|
||
@doc false | ||
def new(float \\ 0.0) | ||
def new(float) when is_valid_double_float?(float), do: {:ok, float} | ||
def new(float) when is_xdr_double(float), do: {:ok, float} | ||
def new(_), do: {:error, :invalid} | ||
|
||
@doc """ | ||
Determines if a value is a valid 8-byte float or integer | ||
""" | ||
@spec valid?(float :: t) :: boolean | ||
def valid?(float), do: is_valid_double_float?(float) | ||
def valid?(float), do: is_xdr_double(float) | ||
|
||
@doc """ | ||
Encodes a double-precision float or integer into an 8-byte binary | ||
""" | ||
@spec encode(float :: t) :: {:ok, xdr :: xdr} | {:error, :invalid} | ||
def encode(float) when not is_valid_double_float?(float), do: {:error, :invalid} | ||
def encode(float), do: {:ok, <<float :: big-signed-float-size(64)>>} | ||
def encode(float) when not is_xdr_double(float), do: {:error, :invalid} | ||
def encode(float), do: {:ok, <<float::big-signed-float-size(64)>>} | ||
|
||
@doc """ | ||
Decodes an 8-byte binary into an double-precision float | ||
""" | ||
@spec decode(xdr :: xdr) :: {:ok, {float :: t, rest :: Base.xdr}} | {:error, :invalid | :out_of_bounds} | ||
def decode(xdr) when not is_valid_xdr?(xdr), do: {:error, :invalid} | ||
def decode(<<float :: big-signed-float-size(64), rest :: binary>>), do: {:ok, {float, rest}} | ||
@spec decode(xdr :: xdr) :: | ||
{:ok, {float :: t, rest :: Base.xdr()}} | {:error, :invalid | :out_of_bounds} | ||
def decode(xdr) when not is_valid_xdr(xdr), do: {:error, :invalid} | ||
def decode(<<float::big-signed-float-size(64), rest::binary>>), do: {:ok, {float, rest}} | ||
def decode(_), do: {:error, :invalid} | ||
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
Oops, something went wrong.