File Coverage

blib/lib/Types/Encodings.pm
Criterion Covered Total %
statement 20 20 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 27 27 100.0


line stmt bran cond sub pod time code
1             package Types::Encodings;
2              
3 1     1   198333 use 5.008001;
  1         4  
  1         45  
4 1     1   7 use strict;
  1         3  
  1         35  
5 1     1   8 use warnings;
  1         8  
  1         65  
6              
7             BEGIN {
8 1     1   2 $Types::Encodings::AUTHORITY = 'cpan:TOBYINK';
9 1         38 $Types::Encodings::VERSION = '0.002';
10             }
11              
12 1     1   6 use Type::Library -base, -declare => qw( Str Bytes Chars );
  1         1  
  1         9  
13 1     1   1593 use Type::Utils;
  1         6642  
  1         8  
14 1     1   1456 use Types::Standard;
  1         2  
  1         7  
15              
16             our $SevenBitSafe = qr{^[\x00-\x7F]*$}sm;
17              
18             declare Str,
19             as Types::Standard::Str;
20              
21             declare Bytes,
22             as Str,
23             where { !utf8::is_utf8($_) },
24             inline_as { "!utf8::is_utf8($_)" };
25              
26             declare Chars,
27             as Str,
28             where { utf8::is_utf8($_) or $_ =~ $Types::Encodings::SevenBitSafe },
29             inline_as { "utf8::is_utf8($_) or $_ =~ \$Types::Encodings::SevenBitSafe" };
30              
31             declare_coercion Decode => to_type Chars, {
32             coercion_generator => sub {
33             my ($self, $target, $encoding) = @_;
34             require Encode;
35             Encode::find_encoding($encoding)
36             or _croak("Parameter \"$encoding\" for Decode[`a] is not an encoding supported by this version of Perl");
37             require B;
38             $encoding = B::perlstring($encoding);
39             return (Bytes, qq{ Encode::decode($encoding, \$_) });
40             },
41             };
42              
43             declare_coercion Encode => to_type Bytes, {
44             coercion_generator => sub {
45             my ($self, $target, $encoding) = @_;
46             require Encode;
47             Encode::find_encoding($encoding)
48             or _croak("Parameter \"$encoding\" for Encode[`a] is not an encoding supported by this version of Perl");
49             require B;
50             $encoding = B::perlstring($encoding);
51             return (Chars, qq{ Encode::encode($encoding, \$_) });
52             },
53             };
54              
55             1;
56              
57             __END__