blib/lib/Zuzu/Module/YAML.pm

filter: Statementstmt 34.3% 33 / 96 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 38.1% 8 / 21 Podpod 0.0% 0 / 1 totaltotal 21.9% 41 / 187 CC 30 SCAR 33.8
CC
30
Cov
21%
__ANON__
90.0
_path_tiny_from_object
42.0
_assert_binary_string
30.0
CRAP 29.4
« blib/lib/Zuzu/Value/Task.pm ↑ Summary blib/lib/Zuzu/Util/NativeHelpers.pm »
1 package Zuzu::Module::YAML;
2  
3 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 our $VERSION = '0.007002';
6  
7 2 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use YAML::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Zuzu::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Zuzu::Value::BinaryString;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Zuzu::Util::NativeHelpers qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
13 native_class
14 native_function
15 native_object
16 perl_to_zuzu
17 zuzu_bool
18 zuzu_to_perl
19 2 );
20  
21 0 ▶sub _assert_binary_string {
Subroutine _assert_binary_string: not called CC=5 SCAR=34.0
22 0 my ( $value, $label ) = @_;
23  
24 0 ▶ return $value
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($value) and $value->isa("Zuzu::Value::BinaryString") 0 0 0
Condition0%0/3
builtin::blessed($value) and $value->isa("Zuzu::Value::BinaryString")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($value) and $value->isa("Zuzu::Value::BinaryString")
builtin::blessed($value) $value->isa("Zuzu::Value::BinaryString")
Truth table0%0/3
A and B
A= builtin::blessed($value)B= $value->isa("Zuzu::Value::BinaryString")
ABresult
0X0
100
111
25 if blessed($value) and $value->isa('Zuzu::Value::BinaryString');
26  
27 0 ▶ my $type = !defined($value) ? 'Null' : ref($value) ? ref($value) : 'String';
Branch: 2 branches
BranchTrueFalseTotal
!defined($value) ? : 0 0 0
ref $value ? : 0 0 0
28 0 die Zuzu::Error->new_runtime(
29 message => "TypeException: $label expects BinaryString, got $type",
30 file => '<std/data/yaml>',
31 line => 0,
32 );
33 }
34  
35 0 ▶sub _decode_yaml_to_zuzu {
Subroutine _decode_yaml_to_zuzu: not called CC=1 SCAR=6.9
36 0 my ( $self, $yaml ) = @_;
37  
38 0 my $perl = $self->slots->{_yaml}->load_string($yaml);
39 return perl_to_zuzu(
40 $perl,
41 0 ▶ is_boolean => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
42 0 ▶ return JSON::PP::is_bool( $_[0] ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
JSON::PP::is_bool($_[0]) ? : 0 0 0
43 },
44 0 );
45 }
46  
47 2 ▶sub IMPORT {
Subroutine IMPORT: called CC=1 SCAR=5.4
Pod undocumented
48 2 my ( $class, $runtime ) = @_;
49  
50 2 my $yaml_class = native_class(
51 name => 'YAML',
52 );
53  
54 0 ▶ $yaml_class->native_constructor( sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
55 0 my ( $rt, $klass, $positional, $named ) = @_;
56 0 ▶ my %config = %{ $named // {} };
Condition0%0/2
$named // {}
l!l
00
MC/DC0%0/1
$named // {}
$named
Truth table0%0/2
A
A= $named
Aresult
11
00
57 0 ▶ if ( @{ $positional // [] } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$positional // [];}) 0 0 0
Condition0%0/2
$positional // []
l!l
00
MC/DC0%0/1
$positional // []
$positional
Truth table0%0/2
A
A= $positional
Aresult
11
00
58 0 my $first = $positional->[0];
59 0 ▶ if ( ref($first) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $first) 0 0 0
60 0 my $first_hash = zuzu_to_perl( $first );
61 0 ▶ if ( ref($first_hash) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $first_hash eq "HASH") 0 0 0
62 0 for my $key ( CORE::keys %{ $first_hash } ) {
63 0 $config{$key} = $first_hash->{$key};
64 }
65 }
66 }
67 }
68  
69 0 my $yp = YAML::PP->new(
70 boolean => 'JSON::PP',
71 schema => [qw/ JSON Merge /],
72 );
73  
74 return native_object(
75 class => $klass,
76 slots => {
77 _yaml => $yp,
78 _pretty => zuzu_bool( $config{pretty}, 0 ) ? 1 : 0,
79 0 ▶ _canonical => zuzu_bool( $config{canonical}, 0 ) ? 1 : 0,
Branch: 2 branches
BranchTrueFalseTotal
zuzu_bool($config{'pretty'}, 0) ? : 0 0 0
zuzu_bool($config{'canonical'}, 0) ? : 0 0 0
80 },
81 const => {
82 _yaml => 1,
83 _pretty => 1,
84 _canonical => 1,
85 },
86 );
87 2 } );
88  
89 $yaml_class->methods->{encode} = native_function(
90 name => 'encode',
91 0 ▶ native => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
92 0 my ( $self, @args ) = @_;
93 0 ▶ my $value = @args ? $args[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 0 0 0
94 my $perl = zuzu_to_perl(
95 $value,
96 0 ▶ boolean_mapper => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
97 0 ▶ return $_[0] ? JSON::PP::true() : JSON::PP::false();
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 0 0 0
98 },
99 0 );
100 0 my $yaml = $self->slots->{_yaml}->dump_string( $perl );
101 0 ▶ $yaml = '' if not defined $yaml;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $yaml 0 0 0
102 0 ▶ if ( not $self->slots->{_pretty} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->slots->{"_pretty"}) 0 0 0
103 0 $yaml =~ s/\n\z//;
104 }
105 0 return $yaml;
106 },
107 2 );
108  
109 $yaml_class->methods->{encode_binarystring} = native_function(
110 name => 'encode_binarystring',
111 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
112 0 my ( $self, @args ) = @_;
113 return Zuzu::Value::BinaryString->from_utf8_string(
114 0 $yaml_class->methods->{encode}->{_native}->( $self, @args )
115 );
116 },
117 2 );
118  
119 $yaml_class->methods->{decode} = native_function(
120 name => 'decode',
121 0 ▶ native => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
122 0 my ( $self, @args ) = @_;
123 0 ▶ my $yaml = @args ? $args[0] : '';
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 0 0 0
124 0 ▶ $yaml = defined($yaml) ? "$yaml" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $yaml ? : 0 0 0
125 0 return _decode_yaml_to_zuzu( $self, $yaml );
126 },
127 2 );
128  
129 $yaml_class->methods->{decode_binarystring} = native_function(
130 name => 'decode_binarystring',
131 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
132 0 my ( $self, @args ) = @_;
133 0 ▶ my $raw = _assert_binary_string(
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 0 0 0
134 @args ? $args[0] : undef,
135 'YAML.decode_binarystring',
136 );
137 0 return _decode_yaml_to_zuzu( $self, $raw->to_utf8_string );
138 },
139 2 );
140  
141 $yaml_class->methods->{load} = native_function(
142 name => 'load',
143 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
144 0 my ( $self, $path_obj ) = @_;
145 0 $runtime->assert_capability( 'fs', "YAML.load is denied by runtime policy" );
146 0 my $path_tiny = _path_tiny_from_object( $path_obj, 'YAML.load' );
147 0 my $raw = Zuzu::Value::BinaryString->new(
148 bytes => $path_tiny->slurp_raw,
149 );
150 0 return $yaml_class->methods->{decode_binarystring}->{_native}->(
151 $self,
152 $raw,
153 );
154 },
155 2 );
156  
157 $yaml_class->methods->{dump} = native_function(
158 name => 'dump',
159 0 ▶ native => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
160 0 my ( $self, $path_obj, @args ) = @_;
161 0 $runtime->assert_capability( 'fs', "YAML.dump is denied by runtime policy" );
162 0 my $path_tiny = _path_tiny_from_object( $path_obj, 'YAML.dump' );
163 0 ▶ my $value = @args ? $args[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 0 0 0
164 my $perl = zuzu_to_perl(
165 $value,
166 0 ▶ boolean_mapper => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
167 0 ▶ return $_[0] ? JSON::PP::true() : JSON::PP::false();
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 0 0 0
168 },
169 0 );
170 0 my $yaml = $self->slots->{_yaml}->dump_string( $perl );
171 0 ▶ $yaml = '' if not defined $yaml;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $yaml 0 0 0
172 0 ▶ if ( not $self->slots->{_pretty} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->slots->{"_pretty"}) 0 0 0
173 0 $yaml =~ s/\n\z//;
174 }
175 $path_tiny->spew_raw(
176 0 Zuzu::Value::BinaryString->from_utf8_string($yaml)->bytes
177 );
178 0 return $path_obj;
179 },
180 2 );
181  
182 return {
183 2 YAML => $yaml_class,
184 };
185 }
186  
187 0 ▶sub _path_tiny_from_object {
Subroutine _path_tiny_from_object: not called CC=6 SCAR=37.4
188 0 my ( $path_obj, $method_name ) = @_;
189  
190 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($path_obj) and $path_obj->isa("Zuzu::Value::Object") and exists $path_obj->slots->{"_path_tiny"}) 0 0 0
Condition0%0/6
builtin::blessed($path_obj) and $path_obj->isa("Zuzu::Value::Object") and exists $path_obj->slots->{"_path_tiny"}
!ll&&!rl&&r
000
builtin::blessed($path_obj) and $path_obj->isa("Zuzu::Value::Object")
!ll&&!rl&&r
000
MC/DC0%0/3
builtin::blessed($path_obj) and $path_obj->isa("Zuzu::Value::Object") and exists $path_obj->slots->{"_path_tiny"}
builtin::blessed($path_obj) $path_obj->isa("Zuzu::Value::Object") exists $path_obj->slots->{"_path_tiny"}
Truth table0%0/4
A and B and C
A= builtin::blessed($path_obj)B= $path_obj->isa("Zuzu::Value::Object")C= exists $path_obj->slots->{"_path_tiny"}
ABCresult
0XX0
10X0
1100
1111
191 blessed($path_obj)
192 and $path_obj->isa('Zuzu::Value::Object')
193 and exists $path_obj->slots->{_path_tiny}
194 ) {
195 0 return $path_obj->slots->{_path_tiny};
196 }
197 0 ▶ if ( ref($path_obj) eq 'HASH' and exists $path_obj->{_path_tiny} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $path_obj eq "HASH" and exists $path_obj->{"_path_tiny"}) 0 0 0
Condition0%0/3
ref $path_obj eq "HASH" and exists $path_obj->{"_path_tiny"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $path_obj eq "HASH" and exists $path_obj->{"_path_tiny"}
ref $path_obj eq "HASH" exists $path_obj->{"_path_tiny"}
Truth table0%0/3
A and B
A= ref $path_obj eq "HASH"B= exists $path_obj->{"_path_tiny"}
ABresult
0X0
100
111
198 0 return $path_obj->{_path_tiny};
199 }
200  
201 0 die Zuzu::Error->new_runtime(
202 message => "TypeException: $method_name expects Path as first argument",
203 file => '<std/data/yaml>',
204 line => 0,
205 );
206 }
207  
208 2 1;
209  
210 =pod
211  
212 =head1 NAME
213  
214 Zuzu::Module::YAML - std/data/yaml bindings for ZuzuScript.
215  
216 =head1 DESCRIPTION
217  
218 Implements the C<std/data/yaml> module. The importer provides a single class
219 named C<YAML>.
220  
221 =head1 CLASSES
222  
223 =head2 YAML
224  
225 Constructor options:
226  
227 =over
228  
229 =item * C<pretty> (default false)
230  
231 Preserves the trailing newline generated by the YAML emitter.
232  
233 =item * C<canonical> (default false)
234  
235 Accepted for API compatibility with C<std/data/json> and C<std/data/toml>.
236  
237 =back
238  
239 Instance methods:
240  
241 =over
242  
243 =item * C<encode(value)>
244  
245 Encodes a ZuzuScript value as YAML text.
246  
247 =item * C<decode(yaml)>
248  
249 Decodes YAML text and returns the corresponding ZuzuScript value.
250  
251 =item * C<load(path)>
252  
253 Reads YAML text from a C<std/io> C<Path> and decodes it.
254 Throws if given anything other than C<Path>.
255  
256 =item * C<dump(path, value)>
257  
258 Encodes C<value> and writes YAML text to a C<std/io> C<Path>.
259 Throws if given anything other than C<Path>.
260  
261 =back
262  
263 =head1 COPYRIGHT AND LICENCE
264  
265 B<< Zuzu::Module::YAML >> is copyright Toby Inkster.
266  
267 It is free software; you may redistribute it and/or modify it under
268 the terms of either the Artistic License 1.0 or the GNU General Public
269 License version 2.
270  
271 =cut
« blib/lib/Zuzu/Value/Task.pm ↑ Summary blib/lib/Zuzu/Util/NativeHelpers.pm »