blib/lib/Zuzu/Marshal/CBOR.pm

filter: Statementstmt 95.0% 210 / 221 Branchbran 85.7% 96 / 112 Conditioncond 80.4% 33 / 41 MC/DCmcdc 68.0% 17 / 25 Subroutinesub 100% 42 / 42 Podpod 0.0% 0 / 16 totaltotal 87.0% 398 / 457 CC 74 SCAR 18.6
CC
74
Cov
91%
_scan_item
16.2
_scan_simple_or_float
11.0
_to_cbor_free
10.1
CRAP 6.4
« blib/lib/Zuzu/Runtime/Async/TaskGroup.pm ↑ Summary blib/lib/Zuzu/Value/Dict.pm »
1 package Zuzu::Marshal::CBOR;
2  
3 8 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 8 our $VERSION = '0.007002';
6  
7 8 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use B qw( SVf_IOK SVf_NOK SVf_POK svref_2object );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use Types::Serialiser ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use Zuzu::Util::Number qw( is_finite_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 8 ▶use CBOR::Free ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use CBOR::Free::Decoder ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 8 our @EXPORT_OK = qw(
18 byte_string
19 bytes_value
20 cbor_false
21 cbor_true
22 decode_one
23 encode_one
24 is_byte_string
25 is_cbor_bool
26 is_tagged
27 is_text_string
28 tag
29 tag_number
30 tag_value
31 text_string
32 text_value
33 validate_profile
34 );
35  
36 8 ▶use constant MAX_SAFE_INTEGER => 9007199254740991;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 {
39 package Zuzu::Marshal::CBOR::TextString;
40 8 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 8 has 'value' => ( is => 'ro', required => 1 );
42 }
43  
44 {
45 8 package Zuzu::Marshal::CBOR::ByteString;
46 8 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 8 has 'bytes' => ( is => 'ro', required => 1 );
48 }
49  
50 {
51 8 package Zuzu::Marshal::CBOR::Tagged;
52 8 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 8 has 'tag' => ( is => 'ro', required => 1 );
54 8 has 'value' => ( is => 'ro' );
55 }
56  
57 111 ▶sub text_string {
Subroutine text_string: called CC=2 SCAR=7.1
Pod undocumented
58 111 my ( $value ) = @_;
59 111 ▶ $value //= '';
Condition50%1/2
$value //= ""
l!l
1110
MC/DC0%0/1
$value //= ""
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
60 111 return Zuzu::Marshal::CBOR::TextString->new( value => "$value" );
61 }
62  
63 6 ▶sub byte_string {
Subroutine byte_string: called CC=2 SCAR=7.1
Pod undocumented
64 6 my ( $bytes ) = @_;
65 6 ▶ $bytes //= '';
Condition50%1/2
$bytes //= ""
l!l
60
MC/DC0%0/1
$bytes //= ""
$bytes
Truth table50%1/2
A
A= $bytes
Aresult
11
00
66 6 return Zuzu::Marshal::CBOR::ByteString->new(
67 bytes => _bytes_scalar($bytes),
68 );
69 }
70  
71 39 ▶sub tag {
Subroutine tag: called CC=1 SCAR=0.0
Pod undocumented
72 39 my ( $tag, $value ) = @_;
73 39 return Zuzu::Marshal::CBOR::Tagged->new(
74 tag => 0 + $tag,
75 value => $value,
76 );
77 }
78  
79 2 ▶sub cbor_true { Types::Serialiser::true() }
Subroutine cbor_true: called CC=1 SCAR=0.0
Pod undocumented
80 1 ▶sub cbor_false { Types::Serialiser::false() }
Subroutine cbor_false: called CC=1 SCAR=0.0
Pod undocumented
81  
82 321 ▶sub is_cbor_bool {
Subroutine is_cbor_bool: called CC=2 SCAR=6.9
Pod undocumented
83 321 my ( $value ) = @_;
84 321 ▶ return Types::Serialiser::is_bool($value) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
&Types::Serialiser::is_bool($value) ? : 7 314 321
85 }
86  
87 480 ▶sub is_text_string {
Subroutine is_text_string: called CC=3 SCAR=11.0
Pod undocumented
88 480 my ( $value ) = @_;
89 480 ▶ return blessed($value)
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($value) && $value->isa('Zuzu::Marshal::CBOR::TextString') ? : 177 303 480
Condition100%3/3
builtin::blessed($value) and $value->isa("Zuzu::Marshal::CBOR::TextString")
!ll&&!rl&&r
2976177
MC/DC100%2/2
builtin::blessed($value) and $value->isa("Zuzu::Marshal::CBOR::TextString")
builtin::blessed($value) $value->isa("Zuzu::Marshal::CBOR::TextString")
Truth table100%3/3
A and B
A= builtin::blessed($value)B= $value->isa("Zuzu::Marshal::CBOR::TextString")
ABresult
0X0
100
111
90 && $value->isa('Zuzu::Marshal::CBOR::TextString') ? 1 : 0;
91 }
92  
93 307 ▶sub is_byte_string {
Subroutine is_byte_string: called CC=3 SCAR=11.0
Pod undocumented
94 307 my ( $value ) = @_;
95 307 ▶ return blessed($value)
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($value) && $value->isa('Zuzu::Marshal::CBOR::ByteString') ? : 7 300 307
Condition100%3/3
builtin::blessed($value) and $value->isa("Zuzu::Marshal::CBOR::ByteString")
!ll&&!rl&&r
29737
MC/DC100%2/2
builtin::blessed($value) and $value->isa("Zuzu::Marshal::CBOR::ByteString")
builtin::blessed($value) $value->isa("Zuzu::Marshal::CBOR::ByteString")
Truth table100%3/3
A and B
A= builtin::blessed($value)B= $value->isa("Zuzu::Marshal::CBOR::ByteString")
ABresult
0X0
100
111
96 && $value->isa('Zuzu::Marshal::CBOR::ByteString') ? 1 : 0;
97 }
98  
99 715 ▶sub is_tagged {
Subroutine is_tagged: called CC=3 SCAR=11.0
Pod undocumented
100 715 my ( $value ) = @_;
101 715 ▶ return blessed($value)
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($value) && $value->isa('Zuzu::Marshal::CBOR::Tagged') ? : 106 609 715
Condition100%3/3
builtin::blessed($value) and $value->isa("Zuzu::Marshal::CBOR::Tagged")
!ll&&!rl&&r
56643106
MC/DC100%2/2
builtin::blessed($value) and $value->isa("Zuzu::Marshal::CBOR::Tagged")
builtin::blessed($value) $value->isa("Zuzu::Marshal::CBOR::Tagged")
Truth table100%3/3
A and B
A= builtin::blessed($value)B= $value->isa("Zuzu::Marshal::CBOR::Tagged")
ABresult
0X0
100
111
102 && $value->isa('Zuzu::Marshal::CBOR::Tagged') ? 1 : 0;
103 }
104  
105 69 ▶sub text_value {
Subroutine text_value: called CC=2 SCAR=7.1
Pod undocumented
106 69 my ( $value ) = @_;
107 69 ▶ die "Not a CBOR text string" if !is_text_string($value);
Branch: 1 branch
BranchTrueFalseTotal
unless is_text_string($value) 0 69 69
108 69 return $value->value;
109 }
110  
111 1 ▶sub bytes_value {
Subroutine bytes_value: called CC=2 SCAR=7.1
Pod undocumented
112 1 my ( $value ) = @_;
113 1 ▶ die "Not a CBOR byte string" if !is_byte_string($value);
Branch: 1 branch
BranchTrueFalseTotal
unless is_byte_string($value) 0 1 1
114 1 return $value->bytes;
115 }
116  
117 28 ▶sub tag_number {
Subroutine tag_number: called CC=2 SCAR=7.1
Pod undocumented
118 28 my ( $value ) = @_;
119 28 ▶ die "Not a CBOR tagged value" if !is_tagged($value);
Branch: 1 branch
BranchTrueFalseTotal
unless is_tagged($value) 0 28 28
120 28 return $value->tag;
121 }
122  
123 29 ▶sub tag_value {
Subroutine tag_value: called CC=2 SCAR=7.1
Pod undocumented
124 29 my ( $value ) = @_;
125 29 ▶ die "Not a CBOR tagged value" if !is_tagged($value);
Branch: 1 branch
BranchTrueFalseTotal
unless is_tagged($value) 0 29 29
126 29 return $value->value;
127 }
128  
129 21 ▶sub encode_one {
Subroutine encode_one: called CC=1 SCAR=0.0
Pod undocumented
130 21 my ( $item ) = @_;
131 21 my $bytes = CBOR::Free::encode( _to_cbor_free($item) );
132 21 validate_profile($bytes);
133 20 return $bytes;
134 }
135  
136 35 ▶sub decode_one {
Subroutine decode_one: called CC=1 SCAR=0.0
Pod undocumented
137 35 my ( $bytes ) = @_;
138 35 $bytes = _bytes_scalar($bytes);
139 35 validate_profile($bytes);
140 my $decoder = CBOR::Free::Decoder->new->set_tag_handlers(
141 10 ▶ 55799 => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
142 10 return Zuzu::Marshal::CBOR::Tagged->new(
143 tag => 55799,
144 value => $_[0],
145 );
146 },
147 20 );
148 20 return _from_cbor_free( $decoder->decode($bytes) );
149 }
150  
151 57 ▶sub validate_profile {
Subroutine validate_profile: called CC=2 SCAR=6.9
Pod undocumented
152 57 my ( $bytes ) = @_;
153 57 $bytes = _bytes_scalar($bytes);
154 57 my $offset = _scan_item( \$bytes, 0, 1 );
155 42 ▶ if ( $offset != length($bytes) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset != length $bytes) 1 41 42
156 1 die "Invalid Zuzu Marshal CBOR: trailing bytes after item";
157 }
158  
159 41 return 1;
160 }
161  
162 675 ▶sub _scan_item {
Subroutine _scan_item: called CC=16 SCAR=27.9
163 675 my ( $bytes_ref, $offset, $top_level ) = @_;
164  
165 675 _require_available( $bytes_ref, $offset, 1, 'initial byte' );
166 675 my $initial = unpack( 'C', substr( $$bytes_ref, $offset, 1 ) );
167 675 $offset++;
168  
169 675 my $major = int( $initial / 32 );
170 675 my $ai = $initial % 32;
171 675 ▶ die "Invalid Zuzu Marshal CBOR: indefinite-length item"
Branch: 1 branch
BranchTrueFalseTotal
if $ai == 31 2 673 675
172 if $ai == 31;
173 673 ▶ die "Invalid Zuzu Marshal CBOR: reserved additional information"
Branch: 1 branch
BranchTrueFalseTotal
if $ai >= 28 and $ai <= 30 0 673 673
Condition33%1/3
$ai >= 28 and $ai <= 30
!ll&&!rl&&r
67300
MC/DC0%0/2
$ai >= 28 and $ai <= 30
$ai >= 28 $ai <= 30
Truth table33%1/3
A and B
A= $ai >= 28B= $ai <= 30
ABresult
0X0
100
111
174 if $ai >= 28 and $ai <= 30;
175  
176 673 ▶ if ( $major == 7 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($major == 7) 34 639 673
177 34 return _scan_simple_or_float( $bytes_ref, $offset, $ai );
178 }
179  
180 639 my ( $value, $next ) = _read_argument( $bytes_ref, $offset, $ai );
181 637 $offset = $next;
182  
183 637 ▶ if ( $major == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($major == 0) 230 407 637
184 230 _assert_unsigned_number_range($value);
185 229 return $offset;
186 }
187 407 ▶ if ( $major == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($major == 1) 1 406 407
188 1 _assert_negative_number_range($value);
189 0 return $offset;
190 }
191 406 ▶ if ( $major == 2 or $major == 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($major == 2 or $major == 3) 77 329 406
Condition100%3/3
$major == 2 or $major == 3
l!l&&r!l&&!r
671329
MC/DC100%2/2
$major == 2 or $major == 3
$major == 2 $major == 3
Truth table100%3/3
A or B
A= $major == 2B= $major == 3
ABresult
1X1
011
000
192 77 _require_available( $bytes_ref, $offset, $value, 'string payload' );
193 77 return $offset + $value;
194 }
195 329 ▶ if ( $major == 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($major == 4) 282 47 329
196 282 for ( 1 .. $value ) {
197 589 $offset = _scan_item( $bytes_ref, $offset, 0 );
198 }
199 281 return $offset;
200 }
201 47 ▶ if ( $major == 5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($major == 5) 23 24 47
202 23 for ( 1 .. $value ) {
203 4 $offset = _scan_item( $bytes_ref, $offset, 0 );
204 4 $offset = _scan_item( $bytes_ref, $offset, 0 );
205 }
206 23 return $offset;
207 }
208 24 ▶ if ( $major == 6 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($major == 6) 24 0 24
209 24 ▶ die "Invalid Zuzu Marshal CBOR: unsupported tag $value"
Branch: 1 branch
BranchTrueFalseTotal
if not $top_level or $value != 55799 3 21 24
Condition100%3/3
not $top_level or $value != 55799
l!l&&r!l&&!r
1221
MC/DC100%2/2
not $top_level or $value != 55799
not $top_level $value != 55799
Truth table100%3/3
not(A) or B
A= not $top_levelB= $value != 55799
ABresult
1X1
011
000
210 if !$top_level or $value != 55799;
211 21 return _scan_item( $bytes_ref, $offset, 0 );
212 }
213  
214 0 die "Invalid Zuzu Marshal CBOR: unsupported major type";
215 }
216  
217 34 ▶sub _scan_simple_or_float {
Subroutine _scan_simple_or_float: called CC=11 SCAR=24.0
218 34 my ( $bytes_ref, $offset, $ai ) = @_;
219  
220 34 ▶ return $offset if $ai == 20 or $ai == 21 or $ai == 22;
Branch: 1 branch
BranchTrueFalseTotal
if $ai == 20 or $ai == 21 or $ai == 22 26 8 34
Condition100%6/6
$ai == 20 or $ai == 21 or $ai == 22
l!l&&r!l&&!r
6208
$ai == 20 or $ai == 21
l!l&&r!l&&!r
2428
MC/DC100%3/3
$ai == 20 or $ai == 21 or $ai == 22
$ai == 20 $ai == 21 $ai == 22
Truth table100%4/4
A or B or C
A= $ai == 20B= $ai == 21C= $ai == 22
ABCresult
1XX1
01X1
0011
0000
221 8 ▶ die "Invalid Zuzu Marshal CBOR: unsupported simple value"
Branch: 1 branch
BranchTrueFalseTotal
if $ai < 24 or $ai == 24 2 6 8
Condition100%3/3
$ai < 24 or $ai == 24
l!l&&r!l&&!r
116
MC/DC100%2/2
$ai < 24 or $ai == 24
$ai < 24 $ai == 24
Truth table100%3/3
A or B
A= $ai < 24B= $ai == 24
ABresult
1X1
011
000
222 if $ai < 24 or $ai == 24;
223 6 ▶ die "Invalid Zuzu Marshal CBOR: half-precision float is invalid"
Branch: 1 branch
BranchTrueFalseTotal
if $ai == 25 1 5 6
224 if $ai == 25;
225 5 ▶ die "Invalid Zuzu Marshal CBOR: single-precision float is invalid"
Branch: 1 branch
BranchTrueFalseTotal
if $ai == 26 1 4 5
226 if $ai == 26;
227 4 ▶ if ( $ai == 27 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ai == 27) 4 0 4
228 4 _require_available( $bytes_ref, $offset, 8, 'binary64 float' );
229 4 my $float = unpack( 'd>', substr( $$bytes_ref, $offset, 8 ) );
230 4 ▶ die "Invalid Zuzu Marshal CBOR: NaN is invalid"
Branch: 1 branch
BranchTrueFalseTotal
if $float != $float 1 3 4
231 if $float != $float;
232 3 ▶ die "Invalid Zuzu Marshal CBOR: infinite float is invalid"
Branch: 1 branch
BranchTrueFalseTotal
unless is_finite_number($float) 1 2 3
233 if !is_finite_number($float);
234 2 return $offset + 8;
235 }
236  
237 0 die "Invalid Zuzu Marshal CBOR: unsupported simple value";
238 }
239  
240 639 ▶sub _read_argument {
Subroutine _read_argument: called CC=6 SCAR=18.4
241 639 my ( $bytes_ref, $offset, $ai ) = @_;
242  
243 639 ▶ return ( $ai, $offset ) if $ai < 24;
Branch: 1 branch
BranchTrueFalseTotal
if $ai < 24 605 34 639
244 34 ▶ if ( $ai == 24 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ai == 24) 8 26 34
245 8 _require_available( $bytes_ref, $offset, 1, 'uint8 argument' );
246 7 my $value = unpack( 'C', substr( $$bytes_ref, $offset, 1 ) );
247 7 _assert_shortest_argument( $value, 24 );
248 6 return ( $value, $offset + 1 );
249 }
250 26 ▶ if ( $ai == 25 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ai == 25) 24 2 26
251 24 _require_available( $bytes_ref, $offset, 2, 'uint16 argument' );
252 24 my $value = unpack( 'n', substr( $$bytes_ref, $offset, 2 ) );
253 24 _assert_shortest_argument( $value, 256 );
254 24 return ( $value, $offset + 2 );
255 }
256 2 ▶ if ( $ai == 26 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ai == 26) 0 2 2
257 0 _require_available( $bytes_ref, $offset, 4, 'uint32 argument' );
258 0 my $value = unpack( 'N', substr( $$bytes_ref, $offset, 4 ) );
259 0 _assert_shortest_argument( $value, 65536 );
260 0 return ( $value, $offset + 4 );
261 }
262 2 ▶ if ( $ai == 27 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ai == 27) 2 0 2
263 2 _require_available( $bytes_ref, $offset, 8, 'uint64 argument' );
264 2 my ( $hi, $lo ) = unpack( 'NN', substr( $$bytes_ref, $offset, 8 ) );
265 2 my $value = $hi * 4294967296 + $lo;
266 2 _assert_shortest_argument( $value, 4294967296 );
267 2 return ( $value, $offset + 8 );
268 }
269  
270 0 die "Invalid Zuzu Marshal CBOR: unsupported argument width";
271 }
272  
273 33 ▶sub _assert_shortest_argument {
Subroutine _assert_shortest_argument: called CC=2 SCAR=6.9
274 33 my ( $value, $minimum ) = @_;
275  
276 33 ▶ die "Invalid Zuzu Marshal CBOR: non-shortest integer or length"
Branch: 1 branch
BranchTrueFalseTotal
if $value < $minimum 1 32 33
277 if $value < $minimum;
278 }
279  
280 230 ▶sub _assert_unsigned_number_range {
Subroutine _assert_unsigned_number_range: called CC=2 SCAR=6.9
281 230 my ( $value ) = @_;
282  
283 230 ▶ die "Invalid Zuzu Marshal CBOR: integer outside Zuzu Number range"
Branch: 1 branch
BranchTrueFalseTotal
if $value > '9007199254740991' 1 229 230
284 if $value > MAX_SAFE_INTEGER;
285 }
286  
287 1 ▶sub _assert_negative_number_range {
Subroutine _assert_negative_number_range: called CC=2 SCAR=7.2
288 1 my ( $value ) = @_;
289  
290 1 ▶ die "Invalid Zuzu Marshal CBOR: integer outside Zuzu Number range"
Branch: 1 branch
BranchTrueFalseTotal
if $value > '9007199254740990' 1 0 1
291 if $value > MAX_SAFE_INTEGER - 1;
292 }
293  
294 790 ▶sub _require_available {
Subroutine _require_available: called CC=3 SCAR=11.1
295 790 my ( $bytes_ref, $offset, $length, $label ) = @_;
296  
297 790 ▶ die "Invalid Zuzu Marshal CBOR: incomplete $label"
Branch: 1 branch
BranchTrueFalseTotal
if $length < 0 or $offset + $length > length $$bytes_ref 1 789 790
Condition66%2/3
$length < 0 or $offset + $length > length $$bytes_ref
l!l&&r!l&&!r
01789
MC/DC50%1/2
$length < 0 or $offset + $length > length $$bytes_ref
$length < 0 $offset + $length > length $$bytes_ref
Truth table66%2/3
A or B
A= $length < 0B= $offset + $length > length $$bytes_ref
ABresult
1X1
011
000
298 if $length < 0 or $offset + $length > length($$bytes_ref);
299 }
300  
301 325 ▶sub _to_cbor_free {
Subroutine _to_cbor_free: called CC=10 SCAR=23.2
302 325 my ( $value ) = @_;
303  
304 325 ▶ return undef if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 9 316 325
305 316 ▶ return CBOR::Free::tag( $value->tag, _to_cbor_free( $value->value ) )
Branch: 1 branch
BranchTrueFalseTotal
if is_tagged($value) 11 305 316
306 if is_tagged($value);
307 305 ▶ return _text_scalar( $value->value ) if is_text_string($value);
Branch: 1 branch
BranchTrueFalseTotal
if is_text_string($value) 34 271 305
308 271 ▶ return _bytes_scalar( $value->bytes ) if is_byte_string($value);
Branch: 1 branch
BranchTrueFalseTotal
if is_byte_string($value) 3 268 271
309 268 ▶ return $value if is_cbor_bool($value);
Branch: 1 branch
BranchTrueFalseTotal
if is_cbor_bool($value) 3 265 268
310  
311 265 ▶ if ( ref($value) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY") 139 126 265
312 139 return [ map { _to_cbor_free($_) } @{ $value } ];
313 }
314 126 ▶ if ( ref($value) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "HASH") 11 115 126
315 return {
316 map {
317 2 _text_scalar($_) => _to_cbor_free( $value->{$_} )
318 11 } sort CORE::keys %{ $value }
319 };
320 }
321 115 ▶ if ( ref($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 0 115 115
322 0 die "Unsupported CBOR adapter value: " . ref($value);
323 }
324  
325 115 ▶ return $value if _scalar_is_number($value);
Branch: 1 branch
BranchTrueFalseTotal
if _scalar_is_number($value) 115 0 115
326 0 return _text_scalar($value);
327 }
328  
329 323 ▶sub _from_cbor_free {
Subroutine _from_cbor_free: called CC=9 SCAR=22.0
330 323 my ( $value ) = @_;
331  
332 323 ▶ return undef if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 9 314 323
333 314 ▶ if ( is_tagged($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_tagged($value)) 10 304 314
334 10 return tag( $value->tag, _from_cbor_free( $value->value ) );
335 }
336 304 ▶ if ( ref($value) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY") 139 165 304
337 139 return [ map { _from_cbor_free($_) } @{ $value } ];
338 }
339 165 ▶ if ( ref($value) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "HASH") 11 154 165
340 return {
341 map {
342 2 $_ => _from_cbor_free( $value->{$_} )
343 11 } sort CORE::keys %{ $value }
344 };
345 }
346 154 ▶ if ( ref($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 3 151 154
347 3 ▶ return $value if is_cbor_bool($value);
Branch: 1 branch
BranchTrueFalseTotal
if is_cbor_bool($value) 3 0 3
348 0 die "Unsupported decoded CBOR adapter value: " . ref($value);
349 }
350  
351 151 ▶ return $value if _scalar_is_number($value);
Branch: 1 branch
BranchTrueFalseTotal
if _scalar_is_number($value) 115 36 151
352 36 ▶ return utf8::is_utf8($value)
Branch: 1 branch
BranchTrueFalseTotal
utf8::is_utf8($value) ? : 33 3 36
353 ? text_string($value)
354 : byte_string($value);
355 }
356  
357 36 ▶sub _text_scalar {
Subroutine _text_scalar: called CC=2 SCAR=7.1
358 36 my ( $value ) = @_;
359 36 ▶ $value //= '';
Condition50%1/2
$value //= ""
l!l
360
MC/DC0%0/1
$value //= ""
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
360 36 return Encode::decode(
361 'UTF-8',
362 Encode::encode( 'UTF-8', "$value", Encode::FB_CROAK ),
363 Encode::FB_CROAK,
364 );
365 }
366  
367 101 ▶sub _bytes_scalar {
Subroutine _bytes_scalar: called CC=4 SCAR=14.5
368 101 my ( $value ) = @_;
369 101 ▶ $value //= '';
Condition50%1/2
$value //= ""
l!l
1010
MC/DC0%0/1
$value //= ""
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
370 101 my $bytes = "$value";
371 101 ▶ utf8::encode($bytes) if utf8::is_utf8($bytes);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($bytes) 0 101 101
372 101 ▶ utf8::downgrade( $bytes, 1 )
Branch: 1 branch
BranchTrueFalseTotal
utf8::downgrade($bytes, 1) or die "CBOR byte string contains non-byte characters" 101 0 101
373 or die "CBOR byte string contains non-byte characters";
374 101 return $bytes;
375 }
376  
377 266 ▶sub _scalar_is_number {
Subroutine _scalar_is_number: called CC=4 SCAR=14.1
378 266 my ( $value ) = @_;
379 266 ▶ return 0 if ref($value);
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 0 266 266
380 266 my $flags = svref_2object(\$value)->FLAGS;
381 266 ▶ return ( $flags & ( SVf_IOK | SVf_NOK ) ) && !( $flags & SVf_POK )
Branch: 1 branch
BranchTrueFalseTotal
$flags & 768 && !($flags & 1024) ? : 230 36 266
Condition66%2/3
$flags & 768 and not $flags & 1024
!ll&&!rl&&r
360230
MC/DC50%1/2
$flags & 768 and not $flags & 1024
$flags & 768 not $flags & 1024
Truth table66%2/3
A and not(B)
A= $flags & 768B= not $flags & 1024
ABresult
0X0
100
111
382 ? 1
383 : 0;
384 }
385  
386 8 1;
387  
388 =pod
389  
390 =head1 NAME
391  
392 Zuzu::Marshal::CBOR - narrow CBOR adapter for Zuzu Marshal
393  
394 =head1 DESCRIPTION
395  
396 Wraps C<CBOR::Free> behind explicit operations for the CBOR data types
397 used by Zuzu Marshal.
398  
399 =head1 COPYRIGHT AND LICENCE
400  
401 B<< Zuzu::Marshal::CBOR >> is copyright Toby Inkster.
402  
403 It is free software; you may redistribute it and/or modify it under
404 the terms of either the Artistic License 1.0 or the GNU General Public
405 License version 2.
406  
407 =cut
« blib/lib/Zuzu/Runtime/Async/TaskGroup.pm ↑ Summary blib/lib/Zuzu/Value/Dict.pm »