     decode_document(data)
   end
 
-  defp decode_type(data, :embedded_list) do
+  defp decode_type(data, type) when type in [:embedded_list, :embedded_set] do
     {nitems, rest} = :small_ints.decode_zigzag_varint(data)
     <<type, rest :: binary>> = rest
 

        {{key_name, value}, acc}
      end
    end

    {Enum.into(keys_and_values, %{}), rest}
  end

  # I've been bitten a few times with FunctionClauseErrors because I still haven't
  # defined how to decode all types, so let's leave this here until we know how
  # to decode them all. Makes it easier to debug :).
  defp decode_type(data, type) do
    raise "don't know how to decode #{inspect type} from data: #{inspect data}"
  end

  defp encode_fields(fields, offset) do
    fields = Enum.map(fields, &encode_field_value/1)
    offset = offset + header_offset(fields)

    {fields, values, _} = Enum.reduce fields, {[], [], offset}, fn(%Field{} = field, {fs, vs, index}) ->
      encoded_field = %{field | pointer_to_data: index} |> encode_field_for_header
      index         = index + IO.iodata_length(field.encoded_value)

      {[encoded_field|fs], [field.encoded_value|vs], index}
    end

    [Enum.reverse(fields), 0, Enum.reverse(values)]


  # Returns the given `%Field{}` with the `:encoded_value` field set to the
  # result of encoding the `:value` field.
  defp encode_field_value(%Field{type: type, value: value} = field, offset) do
    %{field | encoded_value: encode_type(value, type, offset)}
  end

  # Encodes the given `%Field{}` for the header, i.e., just the field
