blib/lib/Function/Interface.pm

filter: Statementstmt 100% 94 / 94 Branchbran 100% 24 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 21 / 21 Podpod 100% 1 / 1 totaltotal 100% 140 / 140 CC 13 SCAR 8.5
CC
13
Cov
100%
_assert_valid_interface_params
5.0
_assert_valid_interface
4.0
_render_src
3.0
CRAP 2.3
« blib/lib/Function/Interface/Info/Function.pm ↑ Summary Build »
1 package Function::Interface;
2  
3 10 ▶use v5.14.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 our $VERSION = "0.04";
7  
8 10 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Keyword::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use PPR;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 10 ▶use Function::Interface::Info;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use Function::Interface::Info::Function;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 ▶use Function::Interface::Info::Function::Param;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 10 ▶use Function::Interface::Info::Function::ReturnParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 sub import {
18 12 ▶ my $class = shift;
Subroutine import: called CC=2 SCAR=6.9
19 12 my %args = @_;
20  
21 12 ▶ my $pkg = $args{pkg} ? $args{pkg} : caller;
Branch: 1 branch
BranchTrueFalseTotal
$args{'pkg'} ? : 1 11 12
22  
23 12 Keyword::Simple::define 'fun' => _define_interface($pkg, 'fun');
24 12 Keyword::Simple::define 'method' => _define_interface($pkg, 'method');
25 }
26  
27 sub unimport {
28 1 ▶ Keyword::Simple::undefine 'fun';
Subroutine unimport: called CC=1 SCAR=0.0
29 1 Keyword::Simple::undefine 'method';
30 }
31  
32 sub _define_interface {
33 24 ▶ my ($pkg, $keyword) = @_;
Subroutine _define_interface: called CC=1 SCAR=0.0
34  
35 return sub {
36 33 ▶ my $ref = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
37  
38 33 my $match = _assert_valid_interface($$ref);
39 20 my $src = _render_src($pkg, $keyword, $match);
40  
41 20 substr($$ref, 0, length $match->{statement}) = $src;
42 }
43 24 }
44  
45 sub _render_src {
46 20 ▶ my ($pkg, $keyword, $match) = @_;
Subroutine _render_src: called CC=3 SCAR=11.0
47  
48 20 my $src = <<"```";
49 BEGIN {
50 Function::Interface::_register_info({
51 package => '$pkg',
52 keyword => '$keyword',
53 subname => '$match->{subname}',
54 params => [ @{[ join ',', map {
55 7 ▶ my $named = $_->{named} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_->{'named'} ? : 2 5 7
56 7 ▶ my $optional = $_->{optional} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_->{'optional'} ? : 2 5 7
57 7 qq!{ type => $_->{type}, name => '$_->{name}', named => $named, optional => $optional }!
58 20 } @{$match->{params}} ]} ],
59 20 return => [ @{[ join ',', @{$match->{return}}] } ],
60 });
61 }
62 ```
63 20 return $src;
64 }
65  
66 10 our %metadata;
67 sub _register_info {
68 20 ▶ my ($args) = @_;
Subroutine _register_info: called CC=1 SCAR=0.0
69  
70 20 push @{$metadata{$args->{package}}} => +{
71 subname => $args->{subname},
72 keyword => $args->{keyword},
73 params => $args->{params},
74 return => $args->{return},
75 20 };
76 }
77  
78 sub info {
79 18 ▶ my ($interface_package) = @_;
Subroutine info: called CC=2 SCAR=6.9
Pod documented
80 18 ▶ my $info = $metadata{$interface_package} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $info = $Function::Interface::metadata{$interface_package} or return undef 1 17 18
81  
82 Function::Interface::Info->new(
83 package => $interface_package,
84 functions => [ map {
85 Function::Interface::Info::Function->new(
86 subname => $_->{subname},
87 keyword => $_->{keyword},
88 55 params => [ map { _make_function_param($_) } @{$_->{params}} ],
89 94 return => [ map { _make_function_return_param($_) } @{$_->{return}} ],
90 )
91 17 } @{$info}],
92 );
93 }
94  
95 sub _make_function_param {
96 55 ▶ my $param = shift;
Subroutine _make_function_param: called CC=1 SCAR=0.0
97 Function::Interface::Info::Function::Param->new(
98 type => $param->{type},
99 name => $param->{name},
100 named => $param->{named},
101 optional => $param->{optional},
102 )
103 55 }
104  
105 sub _make_function_return_param {
106 28 ▶ my $type = shift;
Subroutine _make_function_return_param: called CC=1 SCAR=0.0
107 28 Function::Interface::Info::Function::ReturnParam->new(
108 type => $type,
109 )
110 }
111  
112 sub _assert_valid_interface {
113 33 ▶ my $src = shift;
Subroutine _assert_valid_interface: called CC=4 SCAR=13.9
114  
115 33 ▶ $src =~ m{
Branch: 1 branch
BranchTrueFalseTotal
$src =~ / \A (?<statement> (?&PerlOWS) (?<subname>(?&PerlIdentifier)) (?&PerlOWS) \((?<params>.*?)\) (?&PerlOWS) :Return\((?<return>.*?)\) ; ) $PPR::GRAMMAR /sux or croak("invalid interface") 7 26 33
116 \A
117 (?<statement>
118 (?&PerlOWS) (?<subname>(?&PerlIdentifier))
119 (?&PerlOWS) \((?<params>.*?)\)
120 (?&PerlOWS) :Return\((?<return>.*?)\)
121 ;
122 )
123 $PPR::GRAMMAR
124 }sx or croak "invalid interface";
125  
126 26 my %match;
127 26 $match{statement} = $+{statement};
128 26 $match{subname} = $+{subname};
129 26 ▶ $match{params} = $+{params} ? _assert_valid_interface_params($+{params}) : [];
Branch: 1 branch
BranchTrueFalseTotal
$+{'params'} ? : 10 16 26
130 22 ▶ $match{return} = $+{return} ? _assert_valid_interface_return($+{return}) : [];
Branch: 1 branch
BranchTrueFalseTotal
$+{'return'} ? : 5 17 22
131  
132 20 return \%match;
133 }
134  
135 10 $Function::Interface::GRAMMAR = qr{
136 (?(DEFINE)
137 (?<PerlType>
138 (?&PerlIdentifier)
139 (?: \s* \[
140 \s* (?&PerlTypeParameter) \s*
141 (?: , \s* (?&PerlTypeParameter) \s* )*+
142 \] )?
143 )
144  
145 (?<PerlTypeParameter>
146 (?&PerlString)|(?&PerlVariable)|(?&PerlType)
147 )
148 )
149  
150 $PPR::GRAMMAR
151 }x;
152  
153 sub _assert_valid_interface_params {
154 20 ▶ my $src = shift;
Subroutine _assert_valid_interface_params: called CC=5 SCAR=16.1
155  
156 20 my @list = grep { defined } $src =~ m{
157 ((?&PerlType)) \s*
158 (:?) # named \s*
159 ((?&PerlVariable)) \s*
160 (=?) # optional
161  
162 $Function::Interface::GRAMMAR
163 }xg;
164  
165 20 my @params;
166 20 ▶ while (my ($type, $named, $name, $optional) = splice @list, 0, 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@list, 0, 4)) 17 20 37
167 17 push @params => {
168 type => $type,
169 named => !!$named,
170 name => $name,
171 optional => !!$optional,
172 }
173 }
174  
175 my $regex = join '\s*,\s*', map {
176 20 quotemeta sprintf('%s %s%s%s',
177 $_->{type},
178 $_->{named} ? ':' : '',
179 $_->{name},
180 17 ▶ $_->{optional} ? '=' : '',
Branch: 2 branches
BranchTrueFalseTotal
$_->{'named'} ? : 4 13 17
$_->{'optional'} ? : 4 13 17
181 )
182 } @params;
183  
184 20 ▶ croak "invalid interface params: $src"
Branch: 1 branch
BranchTrueFalseTotal
unless $src =~ / \A \s* $regex \s* \z /ux 8 12 20
185 unless $src =~ m{ \A \s* $regex \s* \z }x;
186  
187 12 return \@params;
188 }
189  
190 sub _assert_valid_interface_return {
191 23 ▶ my $src = shift;
Subroutine _assert_valid_interface_return: called CC=2 SCAR=6.9
192  
193 23 my @list = grep { defined } $src =~ m{
194 ((?&PerlType))
195 $Function::Interface::GRAMMAR
196 }xg;
197  
198 23 ▶ croak "invalid interface return: $src. It should be TYPELIST."
Branch: 1 branch
BranchTrueFalseTotal
unless $src =~ / \A \s* @{[join("\\s*,\\s*", map({quotemeta $_;} @list))];} \s* \z /ux 7 16 23
199 unless $src =~ m{
200 23 \A \s* @{[join '\s*,\s*', map { quotemeta $_ } @list]} \s* \z
201 }x;
202  
203 16 return \@list;
204 }
205  
206 10 1;
207 __END__
« blib/lib/Function/Interface/Info/Function.pm ↑ Summary Build »