Packages

ProtoDef compiler for Elixir. (mostly) Compatible with https://github.com/ProtoDef-io/ProtoDef.

Current section

Files

Jump to
proto_def lib gen elixir impl pstring.ex
Raw

lib/gen/elixir/impl/pstring.ex

defimpl ProtoDef.Gen.Elixir.Protocol, for: ProtoDef.Type.PString do
@data_var ProtoDef.Type.data_var
@input_var ProtoDef.Type.input_var
def decoder(descr, ctx) do
count_ast = ProtoDef.Gen.Elixir.Count.decoder(descr.count, ctx)
quote do
with do
{count, unquote(@data_var)} = unquote(count_ast)
<<str::binary-size(count), unquote(@data_var)::binary>> = unquote(@data_var)
{str, unquote(@data_var)}
end
end
end
def encoder(descr, ctx) do
count_var = Macro.var(:count, ProtoDef.Type.PString)
count_encoder = ProtoDef.Gen.Elixir.Count.encoder(descr.count, count_var, ctx)
quote do
with do
unquote(count_var) = byte_size(unquote(@input_var))
count_head = unquote(count_encoder)
[count_head, unquote(@input_var)]
end
end
end
end