| 1 | package Types::Const; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 3 | ▶ | use v5.14; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | # ABSTRACT: Types that coerce references to read-only | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | use Type::Library | ||||||||||||||||||||
| 9 | 3 | ▶ | -base, | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 3 | -declare => qw/ Const /; | |||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 3 | ▶ | use Const::Fast (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 3 | ▶ | use List::Util 1.33 (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 3 | ▶ | use Storable 3.06 (); # Regexp support | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 3 | ▶ | use Type::Coercion; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 3 | ▶ | use Type::Tiny 1.002001; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 3 | ▶ | use Type::Utils -all; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | 3 | ▶ | use Types::Standard qw/ -types is_ArrayRef is_HashRef is_ScalarRef /; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 3 | ▶ | use Types::TypeTiny (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | # RECOMMEND PREREQ: Ref::Util::XS 0.100 | ||||||||||||||||||||
| 22 | # RECOMMEND PREREQ: Type::Tiny::XS | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 3 | ▶ | use namespace::autoclean 0.28; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 3 | our $VERSION = 'v0.5.0'; | |||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | declare Const, | ||||||||||||||||||||
| 30 | as Ref, | ||||||||||||||||||||
| 31 | where \&__is_readonly, | ||||||||||||||||||||
| 32 | 1 | ▶ | message { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 33 | 1 | return "$_ is not readonly"; | |||||||||||||||||||
| 34 | }, | ||||||||||||||||||||
| 35 | 3 | constraint_generator => \&__constraint_generator, | |||||||||||||||||||
| 36 | coercion_generator => \&__coercion_generator; | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 3 | coerce Const, | |||||||||||||||||||
| 39 | from Ref, | ||||||||||||||||||||
| 40 | via \&__coerce_constant; | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 8 | ▶ | sub __coerce_constant { | ||||||||||||||||||
|
Subroutine
__coerce_constant: called CC=2 SCAR=7.1
| |||||||||||||||||||||
| 43 | 8 | ▶ | my $value = @_ ? $_[0] : $_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 44 | 8 | Const::Fast::_make_readonly( $value => 0 ); | |||||||||||||||||||
| 45 | 8 | return $value; | |||||||||||||||||||
| 46 | } | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 423 | ▶ | sub __is_readonly { | ||||||||||||||||||
|
Subroutine
__is_readonly: called CC=6 SCAR=17.9
| |||||||||||||||||||||
| 49 | 423 | ▶ | if ( is_ArrayRef( $_[0] ) ) { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 50 | 80 | return Internals::SvREADONLY( @{ $_[0] } ) | |||||||||||||||||||
| 51 | 80 | ▶ | && List::Util::all { __is_readonly($_) } @{ $_[0] }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Condition66%2/3
&Internals::SvREADONLY(\@{$_[0];}) && &List::Util::all(sub {
__is_readonly($_);
}
, @{$_[0];})
MC/DC50%1/2
&Internals::SvREADONLY(\@{$_[0];}) && &List::Util::all(sub {
__is_readonly($_);
}
, @{$_[0];})
&Internals::SvREADONLY(\@{$_[0];})
&List::Util::all(sub {
__is_readonly($_);
}
, @{$_[0];})
Truth table66%2/3
A && B
A= &Internals::SvREADONLY(\@{$_[0];})B= &List::Util::all(sub {
__is_readonly($_);
}
, @{$_[0];})
| |||||||||||||||||||||
| 52 | } | ||||||||||||||||||||
| 53 | elsif ( is_HashRef( $_[0] ) ) { | ||||||||||||||||||||
| 54 | 64 | &Internals::hv_clear_placeholders( $_[0] ); | |||||||||||||||||||
| 55 | 64 | return Internals::SvREADONLY( %{ $_[0] } ) | |||||||||||||||||||
| 56 | 64 | ▶ | && List::Util::all { __is_readonly($_) } values %{ $_[0] }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Condition66%2/3
&Internals::SvREADONLY(\%{$_[0];}) && &List::Util::all(sub {
__is_readonly($_);
}
, values %{$_[0];})
MC/DC50%1/2
&Internals::SvREADONLY(\%{$_[0];}) && &List::Util::all(sub {
__is_readonly($_);
}
, values %{$_[0];})
&Internals::SvREADONLY(\%{$_[0];})
&List::Util::all(sub {
__is_readonly($_);
}
, values %{$_[0];})
Truth table66%2/3
A && B
A= &Internals::SvREADONLY(\%{$_[0];})B= &List::Util::all(sub {
__is_readonly($_);
}
, values %{$_[0];})
| |||||||||||||||||||||
| 57 | } | ||||||||||||||||||||
| 58 | elsif ( is_ScalarRef( $_[0] ) ) { | ||||||||||||||||||||
| 59 | 9 | return Internals::SvREADONLY( ${ $_[0] } ); | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 270 | return Internals::SvREADONLY( $_[0] ); | |||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 6 | ▶ | sub __constraint_generator { | ||||||||||||||||||
|
Subroutine
__constraint_generator: called CC=4 SCAR=14.4
| |||||||||||||||||||||
| 66 | 6 | ▶ | return Const unless @_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | 5 | my $param = shift; | |||||||||||||||||||
| 69 | 5 | ▶ | Types::TypeTiny::TypeTiny->check($param) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 70 | or _croak("Parameter to Const[`a] expected to be a type constraint; got $param"); | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 0 | ▶ | _croak("Only one parameter to Const[`a] expected; got @{[ 1 + @_ ]}.") | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 73 | if @_; | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 5 | my $psub = $param->constraint; | |||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 53 | ▶ | return sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||
| 78 | 53 | ▶ | return $psub->($_) && __is_readonly($_); | ||||||||||||||||||
|
Condition66%2/3
&$psub($_) && __is_readonly($_)
MC/DC50%1/2
&$psub($_) && __is_readonly($_)
&$psub($_)
__is_readonly($_)
Truth table66%2/3
A && B
A= &$psub($_)B= __is_readonly($_)
| |||||||||||||||||||||
| 79 | 5 | }; | |||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 5 | ▶ | sub __coercion_generator { | ||||||||||||||||||
|
Subroutine
__coercion_generator: called CC=2 SCAR=14.6
| |||||||||||||||||||||
| 83 | 5 | my ( $parent, $child, $param ) = @_; | |||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 5 | ▶ | return $parent->coercion unless $param->has_coercion; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 0 | my $coercion = Type::Coercion->new( type_constraint => $child ); | |||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 0 | my $coercable_item = $param->coercion->_source_type_union; | |||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | $coercion->add_type_coercions( | ||||||||||||||||||||
| 92 | 0 | ▶ | $parent => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||
| 93 | 0 | ▶ | my $value = @_ ? $_[0] : $_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | 0 | my @new; | |||||||||||||||||||
| 95 | 0 | for my $item (@$value) { | |||||||||||||||||||
| 96 | 0 | ▶ | return $value unless $coercable_item->check($item); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | 0 | push @new, $param->coerce($item); | |||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | 0 | return __coerce_constant(\@new); | |||||||||||||||||||
| 100 | }, | ||||||||||||||||||||
| 101 | 0 | ); | |||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 0 | return $coercion; | |||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 3 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | __END__ | ||||||||||||||||||||