blib/lib/Net/EGTS/SubRecord.pm untested

filter: Statementstmt 0.0% 0 / 121 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 183 CC 11 SCAR 19.1
CC
11
Cov
0%
encode
20.0
decode_all
12.0
as_debug
12.0
CRAP 6.7
1 use utf8;
2  
3 package Net::EGTS::SubRecord;
4 use namespace::autoclean;
5 use Mouse;
6  
7 use Carp;
8 use List::MoreUtils qw(natatime);
9 use Module::Load qw(load);
10  
11 use Net::EGTS::Codes;
12 use Net::EGTS::Util qw(usize);
13 use Net::EGTS::Types;
14  
15 =head1 NAME
16  
17 Net::EGTS::SubRecord - SubRecord common part
18  
19 =cut
20  
21 # Packet types and classes
22 our %TYPES = (
23 EGTS_SR_RECORD_RESPONSE, 'Net::EGTS::SubRecord::Auth::RecordResponse',
24 EGTS_SR_DISPATCHER_IDENTITY, 'Net::EGTS::SubRecord::Auth::DispatcherIdentity',
25 EGTS_SR_RESULT_CODE, 'Net::EGTS::SubRecord::Auth::ResultCode',
26 EGTS_SR_POS_DATA, 'Net::EGTS::SubRecord::Teledata::PosData',
27 );
28  
29 # Subrecord Туре
30 has SRT => is => 'rw', isa => 'BYTE';
31 # Subrecord Length
32 has SRL =>
33 is => 'rw',
34 isa => 'USHORT',
35 lazy => 1,
36 builder => sub {
37 my ($self) = @_;
38 use bytes;
39 return length($self->SRD);
40 },
41 ;
42 # Subrecord Data
43 has SRD =>
44 is => 'rw',
45 isa => 'Maybe[BINARY]',
46 trigger => sub {
47 my ($self, $value, $old) = @_;
48 die 'Subrecord Data too long' if length($value) > 65495;
49 }
50 ;
51  
52 # SubRecord binary
53 has bin => is => 'rw', isa => 'Str', default => '';
54  
55 around BUILDARGS => sub {
56 my $orig = shift;
57 my $class = shift;
58  
59 # simple scalar decoding support
60 my $bin = @_ % 2 ? shift : undef;
61 my %opts = @_;
62  
63 return $class->$orig( bin => $bin, %opts ) if $bin;
64 return $class->$orig( %opts );
65 };
66 sub BUILD {
67 my $self = shift;
68 my $args = shift;
69 $self->decode( \$self->bin ) if length $self->bin;
70 }
71  
72 # Get chunk from binary and store it
73 sub take {
74 my ($self, $bin, $mask, $length) = @_;
75 use bytes;
76  
77 $length //= usize($mask);
78 confess "Can`t get chunk of length $length" if $length > length $$bin;
79  
80 my $chunk = substr $$bin, 0 => $length, '';
81 $self->bin( $self->bin . $chunk );
82  
83 return unpack $mask => $chunk;
84 }
85  
86 # Helper to get portion of data
87 sub nip {
88 my ($self, $bin, $mask, $length) = @_;
89 use bytes;
90  
91 $length //= usize($mask);
92 confess "Can`t get chunk of length $length" if $length > length $$bin;
93  
94 my $chunk = substr $$bin, 0 => $length, '';
95 return unpack $mask => $chunk;
96 }
97  
98 =head2 encode
99  
100 Build subrecord as binary
101  
102 =cut
103  
104 sub encode {
105 my ($self) = @_;
106 use bytes;
107  
108 croak 'Subrecord Туре required' unless defined $self->SRT;
109 croak 'Subrecord Data required' unless defined $self->SRD;
110 croak 'Subrecord Length roo big' unless $self->SRL <= 65495;
111  
112 my $bin = pack 'C S a*' => $self->SRT, $self->SRL, $self->SRD;
113 $self->bin( $bin );
114 return $bin;
115 }
116  
117 =head2 decode \$bin
118  
119 Decode binary I<$bin> into subrecord object.
120 The binary stream will be truncated!
121  
122 =cut
123  
124 sub decode {
125 my ($self, $bin) = @_;
126 use bytes;
127  
128 $self->SRT( $self->take($bin => 'C') );
129 $self->SRL( $self->take($bin => 'S') );
130 $self->SRD( $self->take($bin => 'a*' => $self->SRL) );
131  
132 return $self;
133 }
134  
135 =head2 decode_all \$bin
136  
137 Parse all subrecords from record Record Data
138  
139 =cut
140  
141 sub decode_all {
142 my ($class, $bin) = @_;
143 use bytes;
144  
145 my @result;
146 while( my $length = length $bin ) {
147 my $type = unpack 'C' => substr $bin, 0 => usize('C');
148  
149 my $subclass = $TYPES{ $type };
150 load $subclass;
151  
152 my $self = $subclass->new->decode( \$bin );
153 die 'Something wrong in subrecords decode' unless $self;
154  
155 push @result, $self;
156 }
157  
158 return wantarray ? @result : \@result;
159 }
160  
161 =head2 as_debug
162  
163 Return human readable string
164  
165 =cut
166  
167 sub as_debug {
168 my ($self) = @_;
169 use bytes;
170  
171 my @bytes = ((unpack('B*', $self->bin)) =~ m{.{8}}g);
172  
173 my @str;
174 push @str => sprintf('SRT: %s', splice @bytes, 0 => usize('C'));
175 push @str => sprintf('SRL: %s %s', splice @bytes, 0 => usize('S'));
176  
177 if( @bytes ) {
178 if( my @qualify = inner() ) {
179 push @str => sprintf('SRD =>');
180 push @str, @qualify;
181 push @str => sprintf('<======');
182 } else {
183 my $it = natatime 4, @bytes;
184 my @chunks;
185 while (my @vals = $it->()) {
186 push @chunks, join(' ', @vals);
187 }
188 push @str => sprintf('SRD: %s', join("\n ", @chunks));
189 }
190 }
191  
192 return join "\n", @str;
193 }
194  
195 __PACKAGE__->meta->make_immutable();