File Coverage

blib/lib/TOON/XS.pm
Criterion Covered Total %
statement 46 68 67.6
branch 13 38 34.2
condition 0 4 0.0
subroutine 16 22 72.7
pod 16 16 100.0
total 91 148 61.4


line stmt bran cond sub pod time code
1             # ABSTRACT: Token-Oriented Object Notation for Perl in XS
2             package TOON::XS;
3             $TOON::XS::VERSION = '0.001';
4 15     15   1481787 use 5.010;
  15         50  
5 15     15   84 use strict;
  15         29  
  15         464  
6 15     15   65 use warnings;
  15         20  
  15         844  
7              
8 15     15   78 use Carp qw(croak);
  15         53  
  15         774  
9 15     15   57 use Exporter 'import';
  15         24  
  15         428  
10 15     15   54 use XSLoader;
  15         21  
  15         12388  
11              
12             our @EXPORT_OK = qw(
13             encode_toon decode_toon
14             encode_line_toon decode_line_toon validate_line_toon
15             encode_brace_toon decode_brace_toon validate_brace_toon
16             );
17              
18             XSLoader::load(__PACKAGE__, $TOON::XS::{VERSION} ? ${ $TOON::XS::{VERSION} } : ());
19              
20             sub encode_line_toon {
21 35     35 1 1083109 my ($data, %opts) = @_;
22 35         1036 return _xs_encode_line($data, \%opts);
23             }
24              
25             sub decode_line_toon {
26 50     50 1 1239085 my ($text, %opts) = @_;
27 50         4143 return _xs_decode_line($text, \%opts);
28             }
29              
30             sub validate_line_toon {
31 3     3 1 242808 my ($text, %opts) = @_;
32 3         97 return _xs_validate_line($text, \%opts);
33             }
34              
35             sub encode_brace_toon {
36 5     5 1 218223 my ($data, %opts) = @_;
37 5         163 return _xs_encode_brace($data, \%opts);
38             }
39              
40             sub decode_brace_toon {
41 7     7 1 825 my ($text, %opts) = @_;
42 7         159 return _xs_decode_brace($text);
43             }
44              
45             sub validate_brace_toon {
46 0     0 1 0 my ($text, %opts) = @_;
47 0         0 my $ok = eval { _xs_decode_brace($text); 1 };
  0         0  
  0         0  
48 0 0       0 return $ok ? 1 : 0;
49             }
50              
51             sub encode_toon {
52 0     0 1 0 my ($data, %opts) = @_;
53 0   0     0 my $syntax = delete $opts{'syntax'} // q{};
54 0 0       0 $syntax eq 'line' ? return encode_line_toon($data, %opts)
    0          
55             : $syntax eq 'brace' ? return encode_brace_toon($data, %opts)
56             : croak q{encode_toon() requires 'syntax' => 'line'|'brace'};
57             }
58              
59             sub decode_toon {
60 0     0 1 0 my ($text, %opts) = @_;
61 0   0     0 my $syntax = delete $opts{syntax} // q{};
62 0 0       0 $syntax eq 'line' ? return decode_line_toon($text, %opts)
    0          
63             : $syntax eq 'brace' ? return decode_brace_toon($text, %opts)
64             : croak q{decode_toon() requires 'syntax' => 'line'|'brace'};
65             }
66              
67             sub new {
68 7     7 1 700288 my ($class, %opts) = @_;
69 7 50       23 defined $opts{'syntax'}
70             or croak q{'syntax' => 'line'|'brace' is required};
71              
72             return bless {
73             syntax => $opts{syntax},
74             pretty => exists $opts{pretty} ? $opts{pretty} : 0,
75             canonical => exists $opts{canonical} ? $opts{canonical} : 0,
76 7 50       54 indent => exists $opts{indent} ? $opts{indent} : 2,
    100          
    50          
77             }, $class;
78             }
79              
80             sub encode {
81 4     4 1 12 my ($self, $data, %opts) = @_;
82 4         5 my %all_opts = ( %{$self}, %opts );
  4         16  
83 4         9 my $syntax = delete $all_opts{'syntax'};
84 4 50       19 $syntax eq 'brace' ? return encode_brace_toon($data, %all_opts)
    100          
85             : $syntax eq 'line' ? return encode_line_toon($data, %all_opts)
86             : croak "Unknown syntax '$syntax' for object encode()";
87             }
88              
89             sub decode {
90 5     5 1 1206 my ($self, $text, %opts) = @_;
91 5         6 my %all_opts = (%{$self}, %opts);
  5         20  
92 5         16 my $syntax = delete $all_opts{syntax};
93 5 50       27 $syntax eq 'brace' ? return decode_brace_toon($text, %all_opts)
    100          
94             : $syntax eq 'line' ? return decode_line_toon($text, %all_opts)
95             : croak "Unknown syntax '$syntax' for object decode()";
96             }
97              
98             sub validate {
99 0     0 1 0 my ($self, $text, %opts) = @_;
100 0         0 my %all_opts = (%{$self}, %opts);
  0         0  
101 0         0 my $syntax = delete $all_opts{syntax};
102 0 0       0 $syntax eq 'brace' ? return validate_brace_toon($text, %all_opts)
    0          
103             : $syntax eq 'line' ? return validate_line_toon($text, %all_opts)
104             : croak "Unknown syntax '$syntax' for object validate()";
105             }
106              
107             sub pretty {
108 1     1 1 3 my ($self, $value) = @_;
109 1 50       5 $self->{pretty} = @_ > 1 ? $value : 1;
110 1         3 return $self;
111             }
112              
113             sub canonical {
114 1     1 1 1 my ($self, $value) = @_;
115 1 50       3 $self->{canonical} = @_ > 1 ? $value : 1;
116 1         3 return $self;
117             }
118              
119             sub indent {
120 0     0 1   my ($self, $value) = @_;
121 0 0         defined $value and $self->{indent} = $value;
122 0           return $self;
123             }
124              
125             sub syntax {
126 0     0 1   my ($self, $value) = @_;
127 0 0         defined $value and $self->{syntax} = $value;
128 0           return $self;
129             }
130              
131             1;
132              
133             __END__