lib/Skilletor/TOML.pm

filter: Statementstmt 99.0% 108 / 109 Branchbran 100% 46 / 46 Conditioncond 89.5% 43 / 48 MC/DCmcdc 78.2% 18 / 23 Subroutinesub 100% 25 / 25 Podpod 100% 4 / 4 totaltotal 95.6% 244 / 255 CC 46 SCAR 16.7
CC
46
Cov
97%
_scalar
12.0
stringify_toml
8.0
_value
6.0
CRAP 5.3
« /usr/local/lib/perl5/5.44.0/Tie/Hash.pm ↑ Summary /usr/local/lib/perl5/5.44.0/version/regex.pm »
1 package Skilletor::TOML;
2 # ABSTRACT: The reference's TOML writer – what a Codex agent role needs
3  
4 401 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 401 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 401 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 401 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 401 ▶use Scalar::Util qw( blessed reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 401 ▶use Skilletor::Error::TOML;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 401 ▶use Skilletor::JSON qw( js_number js_sort_keys js_stringify js_type );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 401 ▶use Skilletor::JSON::OrderedHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 401 our $VERSION = '0.008000';
15  
16 401 our @EXPORT_OK = qw( stringify_toml toml_key toml_float is_toml_float );
17  
18 401 my $BARE_KEY = qr/\A[A-Za-z0-9_-]+\z/;
19  
20 3310 ▶sub toml_key {
Subroutine toml_key: called CC=2 SCAR=6.9
Pod documented
21 3310 my ( $key ) = @_;
22 3310 ▶ return $key =~ $BARE_KEY ? $key : _basic_string($key);
Branch: 1 branch
BranchTrueFalseTotal
$key =~ /$BARE_KEY/ ? : 3243 67 3310
23 }
24  
25 59 ▶sub toml_float { Skilletor::TOML::Float->new( $_[0] ) }
Subroutine toml_float: called CC=1 SCAR=0.0
Pod documented
26  
27 3315 ▶sub is_toml_float { blessed $_[0] && $_[0]->isa('Skilletor::TOML::Float') ? 1 : 0 }
Subroutine is_toml_float: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[0]) && $_[0]->isa('Skilletor::TOML::Float') ? : 109 3206 3315
Condition100%3/3
builtin::blessed($_[0]) and $_[0]->isa("Skilletor::TOML::Float")
!ll&&!rl&&r
319511109
MC/DC100%2/2
builtin::blessed($_[0]) and $_[0]->isa("Skilletor::TOML::Float")
builtin::blessed($_[0]) $_[0]->isa("Skilletor::TOML::Float")
Truth table100%3/3
A and B
A= builtin::blessed($_[0])B= $_[0]->isa("Skilletor::TOML::Float")
ABresult
0X0
100
111
28  
29 #### Strings ###########################################################
30  
31 401 my %ESCAPE = ( '\\' => '\\\\', '"' => '\\"', "\n" => '\\n', "\t" => '\\t', "\r" => '\\r' );
32  
33 # What a basic string escapes: `\`, `"` and every control character. The multi-line form
34 # keeps LF and tab literal.
35 401 my $BASIC_ESCAPED = qr/[\\"\x00-\x1F\x7F]/;
36 401 my $MULTILINE_ESCAPED = qr/[\\"\x00-\x08\x0B-\x1F\x7F]/;
37  
38 2592 ▶sub _escape_basic {
Subroutine _escape_basic: called CC=3 SCAR=11.0
39 2592 my ( $s, $keep_newlines ) = @_;
40 2592 ▶ my $re = $keep_newlines ? $MULTILINE_ESCAPED : $BASIC_ESCAPED;
Branch: 1 branch
BranchTrueFalseTotal
$keep_newlines ? : 820 1772 2592
41 2592 ▶ $s =~ s/($re)/ $ESCAPE{$1} \/\/ sprintf( '\\u%04X', ord $1 ) /ge;
Condition100%2/2
$ESCAPE{$1} // sprintf("\\u%04X", ord $1)
l!l
24701386
MC/DC100%1/1
$ESCAPE{$1} // sprintf("\\u%04X", ord $1)
$ESCAPE{$1}
Truth table100%2/2
A
A= $ESCAPE{$1}
Aresult
11
00
42 2592 return $s;
43 }
44  
45 1772 ▶sub _basic_string { '"'._escape_basic( $_[0], 0 ).'"' }
Subroutine _basic_string: called CC=1 SCAR=0.0
46  
47 # Control characters a literal string cannot hold (tab and LF are fine; CR is refused too, so
48 # CRLF survives exactly). A trailing `'` goes to the basic form, as in the reference.
49 401 my $LITERAL_UNSAFE = qr/[\x00-\x08\x0A-\x1F\x7F]/;
50  
51 1425 ▶sub _multiline_string {
Subroutine _multiline_string: called CC=4 SCAR=13.9
52 1425 my ( $s ) = @_;
53 1425 ( my $without_lf = $s ) =~ s/\n//g;
54 1425 ▶ return "'''\n$s'''" if index( $s, "'''" ) < 0 && $s !~ /'\z/ && $without_lf !~ $LITERAL_UNSAFE;
Branch: 1 branch
BranchTrueFalseTotal
if index($s, "'''") == -1 and not $s =~ /'\z/ and not $without_lf =~ /$LITERAL_UNSAFE/ 605 820 1425
Condition100%6/6
index($s, "'''") == -1 and not $s =~ /'\z/ and not $without_lf =~ /$LITERAL_UNSAFE/
!ll&&!rl&&r
126694605
index($s, "'''") == -1 and not $s =~ /'\z/
!ll&&!rl&&r
121141299
MC/DC100%3/3
index($s, "'''") == -1 and not $s =~ /'\z/ and not $without_lf =~ /$LITERAL_UNSAFE/
index($s, "'''") == -1 not $s =~ /'\z/ not $without_lf =~ /$LITERAL_UNSAFE/
Truth table100%4/4
A and not(B) and not(C)
A= index($s, "'''") == -1B= not $s =~ /'\z/C= not $without_lf =~ /$LITERAL_UNSAFE/
ABCresult
0XX0
10X0
1100
1111
55 820 return qq("""\n)._escape_basic( $s, 1 ).'"""';
56 }
57  
58 #### Values ############################################################
59  
60 30 ▶sub _fail { Skilletor::Error::TOML->throw( $_[0] ) }
Subroutine _fail: called CC=1 SCAR=0.0
61  
62 401 my $INF = 9**9**9;
63  
64 # String(n) for any number, NaN and the infinities included.
65 18 ▶sub _number_text {
Subroutine _number_text: called CC=5 SCAR=16.1
66 18 my ( $n ) = @_;
67 18 ▶ return 'NaN' if $n != $n;
Branch: 1 branch
BranchTrueFalseTotal
if $n != $n 3 15 18
68 15 ▶ return $n > 0 ? 'Infinity' : '-Infinity' if $n == $INF || $n == -$INF;
Branch: 2 branches
BranchTrueFalseTotal
if $n == $INF or $n == -$INF 7 8 15
$n > 0 ? : 4 3 7
Condition100%3/3
$n == $INF or $n == -$INF
l!l&&r!l&&!r
438
MC/DC100%2/2
$n == $INF or $n == -$INF
$n == $INF $n == -$INF
Truth table100%3/3
A or B
A= $n == $INFB= $n == -$INF
ABresult
1X1
011
000
69 8 return js_number($n);
70 }
71  
72 100 ▶sub _is_finite { $_[0] == $_[0] && $_[0] != $INF && $_[0] != -$INF }
Subroutine _is_finite: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
$_[0] == $_[0] and $_[0] != $INF and $_[0] != -$INF 93 7 100
Condition100%3/3
$_[0] == $_[0] and $_[0] != $INF
!ll&&!rl&&r
3493
MC/DC100%2/2
$_[0] == $_[0] and $_[0] != $INF
$_[0] == $_[0] $_[0] != $INF
Truth table100%3/3
A and B
A= $_[0] == $_[0]B= $_[0] != $INF
ABresult
0X0
100
111
73  
74 46 ▶sub _is_safe_integer {
Subroutine _is_safe_integer: called CC=3 SCAR=11.0
75 46 my ( $n ) = @_;
76 46 ▶ return _is_finite($n) && $n == int($n) && abs($n) <= 9007199254740991;
Condition100%6/6
_is_finite($n) && $n == int $n && abs $n <= 9007199254740991
!ll&&!rl&&r
7435
_is_finite($n) and $n == int $n
!ll&&!rl&&r
3439
MC/DC100%3/3
_is_finite($n) && $n == int $n && abs $n <= 9007199254740991
_is_finite($n) $n == int $n abs $n <= 9007199254740991
Truth table100%4/4
A and B && C
A= _is_finite($n)B= $n == int $nC= abs $n <= 9007199254740991
ABCresult
0XX0
10X0
1100
1111
77 }
78  
79 3286 ▶sub _is_table {
Subroutine _is_table: called CC=5 SCAR=16.3
80 3286 my ( $v ) = @_;
81 3286 ▶ return ref $v && ( reftype $v // '' ) eq 'HASH' && !is_toml_float($v) && !JSON::MaybeXS::is_bool($v);
Condition81%9/11
ref $v && (builtin::reftype($v) // '') eq 'HASH' && !is_toml_float($v) && !JSON::MaybeXS::is_bool($v)
!ll&&!rl&&r
3221065
ref $v and (builtin::reftype($v) // "") eq "HASH" and not is_toml_float($v)
!ll&&!rl&&r
31675465
ref $v and (builtin::reftype($v) // "") eq "HASH"
!ll&&!rl&&r
312839119
builtin::reftype($v) // ""
l!l
1580
MC/DC50%2/4
ref $v && (builtin::reftype($v) // '') eq 'HASH' && !is_toml_float($v) && !JSON::MaybeXS::is_bool($v)
ref $v builtin::reftype($v) not is_toml_float($v) !JSON::MaybeXS::is_bool($v)
Truth table60%3/5
A and B and not(C) && not(D)
A= ref $vB= builtin::reftype($v)C= not is_toml_float($v)D= !JSON::MaybeXS::is_bool($v)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
82 }
83  
84 3194 ▶sub _scalar {
Subroutine _scalar: called CC=12 SCAR=24.9
85 3194 my ( $v, $where, $multiline ) = @_;
86 3194 ▶ if ( is_toml_float($v) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_toml_float($v)) 54 3140 3194
87 54 my $x = $v->value;
88 54 ▶ _fail( $where.': '._number_text($x).' is not a finite float' ) unless _is_finite($x);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_finite($x) 7 47 54
89 47 my $text = js_number($x);
90 47 ▶ return $text =~ /[.eE]/ ? $text : $text.'.0';
Branch: 1 branch
BranchTrueFalseTotal
$text =~ /[.eE]/ ? : 28 19 47
91 }
92 3140 my $type = js_type($v);
93 3140 ▶ return $multiline ? _multiline_string($v) : _basic_string($v) if $type eq 'string';
Branch: 2 branches
BranchTrueFalseTotal
if $type eq "string" 3077 63 3140
$multiline ? : 1425 1652 3077
94 63 ▶ return $v ? 'true' : 'false' if $type eq 'boolean';
Branch: 2 branches
BranchTrueFalseTotal
if $type eq "boolean" 13 50 63
$v ? : 9 4 13
95 50 ▶ if ( $type eq 'number' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "number") 46 4 50
96 46 ▶ _fail( $where.': '._number_text($v).' is not an integer (use TomlFloat)' ) unless _is_safe_integer($v);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_safe_integer($v) 11 35 46
97 35 return js_number($v);
98 }
99 # JSON.stringify(v): undefined for what JSON cannot write.
100 4 ▶ my $json = defined $v ? eval { js_stringify($v) } // 'undefined' : 'null';
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 1 3 4
Condition50%1/2
eval { do { js_stringify($v) } } // "undefined"
l!l
01
MC/DC0%0/1
eval { do { js_stringify($v) } } // "undefined"
eval { do { js_stringify($v) } }
Truth table50%1/2
A
A= eval { do { js_stringify($v) } }
Aresult
11
00
101 4 _fail( $where.': unsupported value '.$json );
102 }
103  
104 3221 ▶sub _value {
Subroutine _value: called CC=6 SCAR=17.9
105 3221 my ( $v, $where, $multiline ) = @_;
106 3221 ▶ if ( ref $v && ( reftype $v // '' ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v and (builtin::reftype($v) // "") eq "ARRAY") 27 3194 3221
Condition80%4/5
ref $v and (builtin::reftype($v) // "") eq "ARRAY"
!ll&&!rl&&r
31286627
builtin::reftype($v) // ""
l!l
930
MC/DC50%1/2
ref $v and (builtin::reftype($v) // "") eq "ARRAY"
ref $v builtin::reftype($v)
Truth table66%2/3
A and B
A= ref $vB= builtin::reftype($v)
ABresult
0X0
100
111
107 27 for my $item (@$v) {
108 61 ▶ _fail( $where.': only string arrays are supported' ) unless js_type($item) eq 'string';
Branch: 1 branch
BranchTrueFalseTotal
unless js_type($item) eq "string" 7 54 61
109 }
110 20 return '['.join( ', ', map { _basic_string($_) } @$v ).']';
111 }
112 3194 return _scalar( $v, $where, $multiline );
113 }
114  
115 # A JavaScript object's property order: a tied ordered hash keeps it; a plain hash is written
116 # as JSON.stringify writes a sorted object – index keys ascending, then the rest sorted.
117 2981 ▶sub _keys {
Subroutine _keys: called CC=3 SCAR=11.1
118 2981 my ( $h ) = @_;
119 2981 my $tied = tied %$h;
120 2981 ▶ return $tied->keys_in_order if blessed $tied && $tied->isa('Skilletor::JSON::OrderedHash');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($tied) and $tied->isa("Skilletor::JSON::OrderedHash") 2979 2 2981
Condition66%2/3
builtin::blessed($tied) and $tied->isa("Skilletor::JSON::OrderedHash")
!ll&&!rl&&r
202979
MC/DC50%1/2
builtin::blessed($tied) and $tied->isa("Skilletor::JSON::OrderedHash")
builtin::blessed($tied) $tied->isa("Skilletor::JSON::OrderedHash")
Truth table66%2/3
A and B
A= builtin::blessed($tied)B= $tied->isa("Skilletor::JSON::OrderedHash")
ABresult
0X0
100
111
121 2 my @keys = keys %$h;
122 0 my @index = sort { $a <=> $b } grep { Skilletor::JSON::OrderedHash->is_index_key($_) } @keys;
123 2 return ( @index, js_sort_keys( grep { !Skilletor::JSON::OrderedHash->is_index_key($_) } @keys ) );
124 }
125  
126 2918 ▶sub stringify_toml {
Subroutine stringify_toml: called CC=8 SCAR=20.8
Pod documented
127 2918 my ( $table, $opts ) = @_;
128 2918 ▶ my %multiline = map { ( $_ => 1 ) } @{ ( $opts // {} )->{multiline} // [] };
Condition100%4/4
+($opts // {})->{"multiline"} // []
l!l
14261492
$opts // {}
l!l
14261492
MC/DC100%1/1
+($opts // {})->{"multiline"} // []
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
129 2918 my ( @lines, @tables );
130 2918 for my $k ( _keys($table) ) {
131 3216 my $v = $table->{$k};
132 3216 ▶ if ( _is_table($v) ) { push @tables, [ $k, $v ] }
Branch: 1 branch
BranchTrueFalseTotal
if (_is_table($v)) { } 64 3152 3216
133 3152 else { push @lines, toml_key($k).' = '._value( $v, $k, $multiline{$k} ) }
134 }
135 2895 for my $t (@tables) {
136 63 my ( $k, $sub ) = @$t;
137 63 push @lines, '', '['.toml_key($k).']';
138 63 for my $sk ( _keys($sub) ) {
139 70 my $sv = $sub->{$sk};
140 70 ▶ _fail( $k.'.'.$sk.': nested tables are not supported' ) if _is_table($sv);
Branch: 1 branch
BranchTrueFalseTotal
if _is_table($sv) 1 69 70
141 69 push @lines, toml_key($sk).' = '._value( $sv, $k.'.'.$sk, 0 );
142 }
143 }
144 2888 return join( "\n", @lines )."\n";
145 }
146  
147 package # hide from PAUSE
148 Skilletor::TOML::Float;
149  
150 # A TOML float: Perl, like JavaScript, cannot tell 1 from 1.0 by the value alone.
151 59 ▶sub new { my ( $class, $value ) = @_; return bless { value => $value }, $class }
Subroutine new: called CC=1 SCAR=0.0
152 55 ▶sub value { $_[0]{value} }
Subroutine value: called CC=1 SCAR=0.0
153  
154 package Skilletor::TOML;
155  
156 401 1;
157  
158 __END__
« /usr/local/lib/perl5/5.44.0/Tie/Hash.pm ↑ Summary /usr/local/lib/perl5/5.44.0/version/regex.pm »