Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions lib/excbor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ defmodule CBOR do
def decode_non_finite(0, 0), do: {CBOR.Tag, :float, :inf}
def decode_non_finite(1, 0), do: {CBOR.Tag, :float, :"-inf"}
def decode_non_finite(_, _), do: {CBOR.Tag, :float, :nan}
def default_newmap(), do: []
def default_add_to_map(map, k, v), do: [{k,v}|map]
def default_finish_map(map), do: Enum.reverse(map)
def default_empty_map(), do: [{}]
def default_newmap(), do: %{}
def default_add_to_map(map, k, v), do: Dict.put(map, k, v)
def default_finish_map(map), do: map
def default_empty_map(), do: %{}
def hash_add_to_map(map, k, v), do: HashDict.put(map, k, v)
def hash_empty_map(), do: HashDict.new
def identity(x), do: x
Expand All @@ -41,12 +41,12 @@ defmodule CBOR do
val::[unsigned, size(64)], rest::binary >>), do: {mt, val, rest}
def decode_header(<< mt::size(3), 31::size(5), rest::binary >>), do: {mt, :indefinite, rest}
def decode_with_hashes(bin), do: decode(bin, @hash_treatment)
def decode(bin, treatment // @default_treatment) do
def decode(bin, treatment \\ @default_treatment) do
{term, ""} = decode_with_rest(bin, treatment)
term
end
def decode_with_hashes_with_rest(bin), do: decode_with_rest(bin, @hash_treatment)
def decode_with_rest(bin, treatment // @default_treatment) do
def decode_with_rest(bin, treatment \\ @default_treatment) do
{mt, val, rest} = decode_header(bin)
case val do
:indefinite ->
Expand Down Expand Up @@ -125,7 +125,7 @@ defmodule CBOR do
defp decode_array(len, rest, treatment) do
decode_array1(len, treatment, [], rest)
end
defp decode_array1(0, treatment, acc, bin) do
defp decode_array1(0, _treatment, acc, bin) do
{Enum.reverse(acc), bin}
end
defp decode_array1(len, treatment, acc, bin) do
Expand Down Expand Up @@ -255,6 +255,13 @@ defmodule CBOR do
fn(v, acc) -> CBOR.Encoder.encode_into(v, acc) end)
end
end
defimpl Encoder, for: Map do
def encode_into(dict, acc) do
Enum.reduce(dict, CBOR.encode_head(5, Dict.size(dict), acc),
fn({k, v}, acc) ->
CBOR.Encoder.encode_into(v, CBOR.Encoder.encode_into(k, acc)) end)
end
end
# defimpl Encoder, for: Tuple do # anything else we can do here?
# end
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Excbor.Mixfile do
def project do
[ app: :excbor,
version: "0.0.1",
elixir: "~> 0.12.3-dev",
elixir: "~> 0.12",
deps: deps ]
end

Expand Down
16 changes: 8 additions & 8 deletions test/excbor_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -353,27 +353,27 @@ defmodule(CBORTest) do
end
test("RFC 7049 Appendix A Example 67") do
encoded = <<160>>
assert(d(encoded) == [{}])
assert(d(encoded) == %{})
assert(CBOR.encode(d(encoded)) == encoded)
end
test("RFC 7049 Appendix A Example 68") do
encoded = <<162, 1, 2, 3, 4>>
assert(d(encoded) == [{1, 2}, {3, 4}])
assert(d(encoded) == %{1 => 2, 3 => 4})
assert(CBOR.encode(d(encoded)) == encoded)
end
test("RFC 7049 Appendix A Example 69") do
encoded = <<162, 97, 97, 1, 97, 98, 130, 2, 3>>
assert(d(encoded) == [{"a", 1}, {"b", [2, 3]}])
assert(d(encoded) == %{"a" => 1, "b" => [2, 3]})
assert(CBOR.encode(d(encoded)) == encoded)
end
test("RFC 7049 Appendix A Example 70") do
encoded = <<130, 97, 97, 161, 97, 98, 97, 99>>
assert(d(encoded) == ["a", [{"b", "c"}]])
assert(d(encoded) == ["a", %{"b" => "c"}])
assert(CBOR.encode(d(encoded)) == encoded)
end
test("RFC 7049 Appendix A Example 71") do
encoded = <<165, 97, 97, 97, 65, 97, 98, 97, 66, 97, 99, 97, 67, 97, 100, 97, 68, 97, 101, 97, 69>>
assert(d(encoded) == [{"a", "A"}, {"b", "B"}, {"c", "C"}, {"d", "D"}, {"e", "E"}])
assert(d(encoded) == %{"a" => "A", "b" => "B", "c" => "C", "d" => "D", "e" => "E"})
assert(CBOR.encode(d(encoded)) == encoded)
end
test("RFC 7049 Appendix A Example 72") do
Expand Down Expand Up @@ -418,17 +418,17 @@ defmodule(CBORTest) do
end
test("RFC 7049 Appendix A Example 80") do
encoded = <<191, 97, 97, 1, 97, 98, 159, 2, 3, 255, 255>>
assert(d(encoded) == [{"a", 1}, {"b", [2, 3]}])
assert(d(encoded) == %{"a" => 1, "b" => [2, 3]})
# (no roundtrip)
end
test("RFC 7049 Appendix A Example 81") do
encoded = <<130, 97, 97, 191, 97, 98, 97, 99, 255>>
assert(d(encoded) == ["a", [{"b", "c"}]])
assert(d(encoded) == ["a", %{"b" => "c"}])
# (no roundtrip)
end
test("RFC 7049 Appendix A Example 82") do
encoded = <<191, 99, 70, 117, 110, 245, 99, 65, 109, 116, 33, 255>>
assert(d(encoded) == [{"Fun", true}, {"Amt", -2}])
assert(d(encoded) == %{"Fun" => true, "Amt" => -2})
# (no roundtrip)
end
end