Skip to content

Commit

Permalink
Simplify code.
Browse files Browse the repository at this point in the history
  • Loading branch information
rustyio committed Oct 6, 2009
1 parent ae68db1 commit 7f63f55
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 32 deletions.
13 changes: 7 additions & 6 deletions src/vice_decode.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
-export ([from_binary/2]).

from_binary(Schema, Binary) ->
SchemaType = vice_utils:type(Schema),
% If it's a placeholder, then encode it.
% Otherwise, continue walking the structure.
case vice_utils:is_placeholder(Schema) of
true -> decode(Schema, Binary);
false -> walk(SchemaType, Schema, Binary)
false ->

walk(Schema, Binary)
end.

walk(list, Schema, Binary) ->
walk(Schema, Binary) when is_list(Schema) ->
Length = length(Schema),
F = fun(_X, {Values, AccBinary, [S|AccSchema]}) ->
{Value, AccBinary1} = from_binary(S, AccBinary),
Expand All @@ -19,12 +20,12 @@ walk(list, Schema, Binary) ->
{Values1, Binary1, _} = lists:foldl(F, {[], Binary, Schema}, lists:seq(1, Length)),
{lists:reverse(Values1), Binary1};

walk(tuple, Schema, Binary) ->
{Values, Rest} = walk(list, tuple_to_list(Schema), Binary),
walk(Schema, Binary) when is_tuple(Schema) ->
{Values, Rest} = walk(tuple_to_list(Schema), Binary),
{list_to_tuple(Values), Rest};

%%% Didn't match anything, so ignore.
walk(_, Schema, Binary) ->
walk(Schema, Binary) ->
{Schema, Binary}.


Expand Down
17 changes: 7 additions & 10 deletions src/vice_encode.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,16 @@

%%% - TERM TO BINARY - %%%
to_binary(Schema, Term) ->
SchemaType = vice_utils:type(Schema),

% If it's a placeholder, then encode it.
% Otherwise, continue walking the structure.
case vice_utils:is_placeholder(Schema) of
true ->
vice_utils:ensure_matching_types(Schema, Term),
encode(Schema, Term);
false -> walk(SchemaType, Schema, Term)
false -> walk(Schema, Term)
end.

walk(tuple, Schema, Term) ->
walk(list, tuple_to_list(Schema), tuple_to_list(Term));

walk(list, Schema, Term) ->

walk(Schema, Term) when is_list(Schema) ->
case {length(Schema), length(Term)} of
{0, 0} -> <<>>;
{N, N} ->
Expand All @@ -29,10 +24,12 @@ walk(list, Schema, Term) ->
B;
_ -> throw({mismatched_lengths, Schema, Term})
end;


walk(Schema, Term) when is_tuple(Schema) ->
walk(tuple_to_list(Schema), tuple_to_list(Term));

%%% Didn't match anything, so ignore.
walk(_, _, _) ->
walk(_, _) ->
<<>>.


Expand Down
18 changes: 2 additions & 16 deletions src/vice_utils.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-module (vice_utils).
-export ([is_placeholder/1, ensure_matching_types/2, type/1]).
-export ([is_placeholder/1, ensure_matching_types/2]).

%%% - BINARY TO TERM - %%%

Expand Down Expand Up @@ -55,18 +55,4 @@ ensure_matching_types({tuple@, _}, O) -> is_tuple(O) orelse throw({not_a_tuple,
ensure_matching_types(dict@, O) -> is_tuple(O) orelse throw({not_a_dict, O});
ensure_matching_types({dict@, _, _}, O) -> is_tuple(O) orelse throw({not_a_dict, O});
ensure_matching_types(term@, _) -> true;
ensure_matching_types(Schema, _) -> throw({ensure_matching_types, unknown_type, Schema}).

% Return an atom that signifies the type of O.
type(O) when is_atom(O) -> atom;
type(O) when is_binary(O) -> binary;
type(O) when is_bitstring(O) -> bitstring;
type(O) when is_boolean(O) -> boolean;
type(O) when is_float(O) -> float;
type(O) when is_function(O) -> function;
type(O) when is_integer(O) -> integer;
type(O) when is_list(O) -> list;
type(O) when is_pid(O) -> pid;
type(O) when is_reference(O) -> reference;
type(O) when is_tuple(O) -> tuple;
type(O) -> throw({unexpected_type, O}).
ensure_matching_types(Schema, _) -> throw({ensure_matching_types, unknown_type, Schema}).

0 comments on commit 7f63f55

Please sign in to comment.