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   148903 use strict;
  2         4  
  2         119  
2 2     2   11 use warnings;
  2         3  
  2         189  
3             package YAML::PP::Schema::Binary;
4              
5             our $VERSION = 'v0.41.1'; # TRIAL VERSION
6              
7 2     2   1039 use MIME::Base64 qw/ decode_base64 encode_base64 /;
  2         2056  
  2         179  
8 2     2   592 use YAML::PP::Common qw/ YAML_ANY_SCALAR_STYLE /;
  2         5  
  2         563  
9              
10             sub register {
11 1     1 1 4 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   20 my ($constructor, $event) = @_;
18 15         18 my $base64 = $event->{value};
19 15         43 my $binary = decode_base64($base64);
20 15         66 return $binary;
21 1         7 }],
22             implicit => 0,
23             );
24              
25             $schema->add_representer(
26             regex => qr{.*},
27             code => sub {
28 20     20   26 my ($rep, $node) = @_;
29 20         26 my $binary = $node->{value};
30 20 100       80 unless ($binary =~ m/[\x{7F}-\x{10FFFF}]/) {
31             # ASCII
32 2         5 return;
33             }
34 18 100       48 if (utf8::is_utf8($binary)) {
35             # utf8
36 4         11 return;
37             }
38             # everything else must be base64 encoded
39 14         45 my $base64 = encode_base64($binary);
40 14         21 $node->{style} = YAML_ANY_SCALAR_STYLE;
41 14         24 $node->{data} = $base64;
42 14         19 $node->{tag} = "tag:yaml.org,2002:binary";
43 14         42 return 1;
44             },
45 1         24 );
46             }
47              
48             1;
49              
50             __END__