File Coverage

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


line stmt bran cond sub pod time code
1             package Types::Encodings;
2              
3 1     1   131271 use 5.008001;
  1         3  
4 1     1   5 use strict;
  1         2  
  1         17  
5 1     1   4 use warnings;
  1         1  
  1         33  
6              
7             BEGIN {
8 1     1   2 $Types::Encodings::AUTHORITY = 'cpan:TOBYINK';
9 1         22 $Types::Encodings::VERSION = '0.003';
10             }
11              
12 1     1   5 use Type::Library -base, -declare => qw( Str Bytes Chars );
  1         2  
  1         5  
13 1     1   901 use Type::Utils;
  1         3860  
  1         7  
14 1     1   1256 use Types::Standard;
  1         2  
  1         4  
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 { sprintf '(%s) && %s', Str->inline_check($_), "!utf8::is_utf8($_)" };
25              
26             declare Chars,
27             as Str,
28             where { utf8::is_utf8($_) or $_ =~ $Types::Encodings::SevenBitSafe },
29             inline_as { sprintf '(%s) && (%s)', Str->inline_check($_), "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__