File Coverage

blib/lib/YAML/PP/Schema/Binary.pm
Criterion Covered Total %
statement 31 31 100.0
branch 4 4 100.0
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 43 43 100.0


line stmt bran cond sub pod time code
1 2     2   151852 use strict;
  2         4  
  2         66  
2 2     2   8 use warnings;
  2         4  
  2         170  
3             package YAML::PP::Schema::Binary;
4              
5             our $VERSION = 'v0.40.1'; # TRIAL VERSION
6              
7 2     2   871 use MIME::Base64 qw/ decode_base64 encode_base64 /;
  2         1597  
  2         144  
8 2     2   434 use YAML::PP::Common qw/ YAML_ANY_SCALAR_STYLE /;
  2         3  
  2         529  
9              
10             sub register {
11 1     1 1 3 my ($self, %args) = @_;
12 1         2 my $schema = $args{schema};
13              
14             $schema->add_resolver(
15             tag => 'tag:yaml.org,2002:binary',
16             match => [ all => sub {
17 15     15   30 my ($constructor, $event) = @_;
18 15         27 my $base64 = $event->{value};
19 15         52 my $binary = decode_base64($base64);
20 15         44 return $binary;
21 1         6 }],
22             implicit => 0,
23             );
24              
25             $schema->add_representer(
26             regex => qr{.*},
27             code => sub {
28 20     20   38 my ($rep, $node) = @_;
29 20         35 my $binary = $node->{value};
30 20 100       66 unless ($binary =~ m/[\x{7F}-\x{10FFFF}]/) {
31             # ASCII
32 2         6 return;
33             }
34 18 100       60 if (utf8::is_utf8($binary)) {
35             # utf8
36 4         12 return;
37             }
38             # everything else must be base64 encoded
39 14         49 my $base64 = encode_base64($binary);
40 14         30 $node->{style} = YAML_ANY_SCALAR_STYLE;
41 14         29 $node->{data} = $base64;
42 14         24 $node->{tag} = "tag:yaml.org,2002:binary";
43 14         79 return 1;
44             },
45 1         9 );
46             }
47              
48             1;
49              
50             __END__