diff --git a/lib/Subtitle/SSA/Record.pm b/lib/Subtitle/SSA/Record.pm index c3543a7..c20ce08 100644 --- a/lib/Subtitle/SSA/Record.pm +++ b/lib/Subtitle/SSA/Record.pm @@ -10,6 +10,15 @@ use overload '""' => \&to_string, ; +sub error { + my ($self, $text) = @_; + if (defined $text) { + $self->{_error} = $text; + return; + } + return $self->{_error}; +} + sub set { my ($self, $field, $value) = @_; return unless $field; @@ -22,7 +31,8 @@ sub set { sub parse_format_line { my ($self, $line) = @_; - return unless $line and $line =~ m{^Format:}oi; + return $self->error("not looks like Format line") + unless $line and $line =~ m{^Format:}oi; chomp $line; $line =~ s{^Format:\s*}{}oi; @@ -30,7 +40,8 @@ sub parse_format_line { my @fields; foreach my $field (split(/,\s*/o, $line)) { $field = lc($field); - return unless exists $self->{_fields}->{$field}; + return $self->error("unknown field: $field") + unless exists $self->{_fields}->{$field}; push @fields, $field; } @@ -49,15 +60,18 @@ sub parse { $format //= $self->{_format}; my $PREFIX = $self->{_prefix}; - return unless $line and $line =~ m{^$PREFIX:}oi; - return unless $format and ref($format) eq 'ARRAY'; + return $self->error("not looks like $PREFIX line") + unless $line and $line =~ m{^$PREFIX:}oi; + return $self->error("passed custom fields order not ARRAY ref") + unless $format and ref($format) eq 'ARRAY'; chomp $line; $line =~ s{^$PREFIX:\s*}{}oi; my $fieldcnt = scalar @{ $format }; my @values = split /,\s*/o, $line, $fieldcnt; # check that values count match fields count - return if scalar @values != $fieldcnt; + return $self->error("number of fields less than expected count") + if scalar @values < $fieldcnt; foreach my $field (@{ $format }) { my $d = $self->{_fields}->{$field}; @@ -66,7 +80,8 @@ sub parse { $value = hex($1); } elsif ($d->{type} eq 't') { $value = parse_timing($value); - return if $value < 0; # parsing failure + return $self->error("can't parse timing: $value") + if $value < 0; # parsing failure } $self->set($field => $value); }