_site/cover/narcs_bits.COVER.html

1 %% Copyright (c) 2023 Peter Morgan <peter.james.morgan@gmail.com>
2 %%
3 %% Licensed under the Apache License, Version 2.0 (the "License");
4 %% you may not use this file except in compliance with the License.
5 %% You may obtain a copy of the License at
6 %%
7 %% http://www.apache.org/licenses/LICENSE-2.0
8 %%
9 %% Unless required by applicable law or agreed to in writing, software
10 %% distributed under the License is distributed on an "AS IS" BASIS,
11 %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 %% See the License for the specific language governing permissions and
13 %% limitations under the License.
14
15
16 %% @doc Encoder combinators for bit output.
17
18 -module(narcs_bits).
19
20
21 -export([into_bit/0]).
22 -export([into_bit/1]).
23 -export([into_bitfield/1]).
24
25
26 %% @doc Given a map of flags, output a bitstring ordered using the
27 %% supplied names.
28
29 -spec into_bitfield(#{A => boolean()}) -> narcs:encoder([A], bitstring()).
30
31 into_bitfield(Flags) ->
32 3 fun
33 (Names) ->
34 4 list_to_bitstring(
35 lists:map(
36 fun
37 (Name) ->
38 11 (into_bit())(maps:get(Name, Flags, false))
39 end,
40 Names))
41 end.
42
43
44 %% @doc Output a bit from a boolean value.
45
46 -spec into_bit() -> narcs:encoder(boolean(), <<_:1>>).
47
48 into_bit() ->
49 15 fun
50 (true) ->
51 8 <<1:1>>;
52
53 (false) ->
54 6 <<0:1>>
55 end.
56
57
58 -spec into_bit(narcs:encoder(A, boolean())) -> narcs:encoder(A, <<_:1>>).
59
60 into_bit(Encoder) ->
61
:-(
fun
62 (Decoded) ->
63
:-(
(into_bit())(Encoder(Decoded))
64 end.
Line Hits Source