chiark / gitweb /
Add 'TOML-Tiny/' from commit 'dab92c92905923c50929c14cc59adadff5b9eedf'
[nailing-cargo.git] / TOML-Tiny / lib / TOML / Tiny / Util.pm
1 package TOML::Tiny::Util;
2 # ABSTRACT: utility functions used by TOML::Tiny
3
4 use strict;
5 use warnings;
6 no warnings 'experimental';
7 use v5.18;
8
9 use TOML::Tiny::Grammar;
10
11 use parent 'Exporter';
12
13 our @EXPORT_OK = qw(
14   is_strict_array
15 );
16
17 sub is_strict_array {
18   my $arr = shift;
19
20   my @types = map{
21     my $value = $_;
22     my $type;
23
24     for (ref $value) {
25       $type = 'array'   when 'ARRAY';
26       $type = 'table'   when 'HASH';
27
28       # Do a little heuristic guess-work
29       $type = 'float'   when /Float/;
30       $type = 'integer' when /Int/;
31       $type = 'bool'    when /Boolean/;
32
33       when ('') {
34         for ($value) {
35           $type = 'bool'     when /$Boolean/;
36           $type = 'float'    when /$Float/;
37           $type = 'integer'  when /$Integer/;
38           $type = 'datetime' when /$DateTime/;
39           default{ $type = 'string' };
40         }
41       }
42
43       default{
44         $type = $_;
45       }
46     }
47
48     $type;
49   } @$arr;
50
51   my $t = shift @types;
52
53   for (@types) {
54     return (undef, "expected value of type $t, but found $_")
55       if $_ ne $t;
56   }
57
58   return (1, undef);
59 }
60
61 1;