"type" sugar for when you're not subtyping anything
[gitmo/Mouse.git] / lib / Mouse / Util / TypeConstraints.pm
1 package Mouse::Util::TypeConstraints;
2 use strict;
3 use warnings;
4
5 use Carp ();
6 use Scalar::Util qw/blessed looks_like_number openhandle/;
7
8 my %SUBTYPE;
9 my %COERCE;
10 my %COERCE_KEYS;
11
12 #find_type_constraint register_type_constraint
13 sub import {
14     my $class  = shift;
15     my %args   = @_;
16     my $caller = $args{callee} || caller(0);
17
18     no strict 'refs';
19     *{"$caller\::as"}          = \&_as;
20     *{"$caller\::where"}       = \&_where;
21     *{"$caller\::message"}     = \&_message;
22     *{"$caller\::from"}        = \&_from;
23     *{"$caller\::via"}         = \&_via;
24     *{"$caller\::type"}        = \&_type;
25     *{"$caller\::subtype"}     = \&_subtype;
26     *{"$caller\::coerce"}      = \&_coerce;
27     *{"$caller\::class_type"}  = \&_class_type;
28     *{"$caller\::role_type"}   = \&_role_type;
29 }
30
31
32 sub _as ($) {
33     as => $_[0]
34 }
35 sub _where (&) {
36     where => $_[0]
37 }
38 sub _message ($) {
39     message => $_[0]
40 }
41
42 sub _from { @_ }
43 sub _via (&) {
44     $_[0]
45 }
46
47 my $optimized_constraints;
48 my $optimized_constraints_base;
49 {
50     no warnings 'uninitialized';
51     %SUBTYPE = (
52         Any        => sub { 1 },
53         Item       => sub { 1 },
54         Bool       => sub {
55             !defined($_) || $_ eq "" || "$_" eq '1' || "$_" eq '0'
56         },
57         Undef      => sub { !defined($_) },
58         Defined    => sub { defined($_) },
59         Value      => sub { defined($_) && !ref($_) },
60         Num        => sub { !ref($_) && looks_like_number($_) },
61         Int        => sub { defined($_) && !ref($_) && /^-?[0-9]+$/ },
62         Str        => sub { defined($_) && !ref($_) },
63         ClassName  => sub { Mouse::is_class_loaded($_) },
64         Ref        => sub { ref($_) },
65
66         ScalarRef  => sub { ref($_) eq 'SCALAR' },
67         ArrayRef   => sub { ref($_) eq 'ARRAY'  },
68         HashRef    => sub { ref($_) eq 'HASH'   },
69         CodeRef    => sub { ref($_) eq 'CODE'   },
70         RegexpRef  => sub { ref($_) eq 'Regexp' },
71         GlobRef    => sub { ref($_) eq 'GLOB'   },
72
73         FileHandle => sub {
74                 ref($_) eq 'GLOB'
75                 && openhandle($_)
76             or
77                 blessed($_)
78                 && $_->isa("IO::Handle")
79             },
80
81         Object     => sub { blessed($_) && blessed($_) ne 'Regexp' },
82     );
83
84     sub optimized_constraints { \%SUBTYPE }
85     my @SUBTYPE_KEYS = keys %SUBTYPE;
86     sub list_all_builtin_type_constraints { @SUBTYPE_KEYS }
87 }
88
89 sub _type {
90     my $pkg = caller(0);
91     my($name, %conf) = @_;
92     if (my $type = $SUBTYPE{$name}) {
93         Carp::croak "The type constraint '$name' has already been created, cannot be created again in $pkg";
94     };
95     my $stuff = $conf{where} || do { $SUBTYPE{delete $conf{as} || 'Any' } };
96     $SUBTYPE{$name} = $stuff;
97 }
98
99 sub _subtype {
100     my $pkg = caller(0);
101     my($name, %conf) = @_;
102     if (my $type = $SUBTYPE{$name}) {
103         Carp::croak "The type constraint '$name' has already been created, cannot be created again in $pkg";
104     };
105     my $stuff = $conf{where} || do { $SUBTYPE{delete $conf{as} || 'Any' } };
106     my $as    = $conf{as} || '';
107     if ($as = $SUBTYPE{$as}) {
108         $SUBTYPE{$name} = sub { $as->($_) && $stuff->($_) };
109     } else {
110         $SUBTYPE{$name} = $stuff;
111     }
112 }
113
114 sub _coerce {
115     my($name, %conf) = @_;
116
117     Carp::croak "Cannot find type '$name', perhaps you forgot to load it."
118         unless $SUBTYPE{$name};
119
120     unless ($COERCE{$name}) {
121         $COERCE{$name}      = {};
122         $COERCE_KEYS{$name} = [];
123     }
124     while (my($type, $code) = each %conf) {
125         Carp::croak "A coercion action already exists for '$type'"
126             if $COERCE{$name}->{$type};
127
128         Carp::croak "Could not find the type constraint ($type) to coerce from"
129             unless $SUBTYPE{$type};
130
131         push @{ $COERCE_KEYS{$name} }, $type;
132         $COERCE{$name}->{$type} = $code;
133     }
134 }
135
136 sub _class_type {
137     my $pkg = caller(0);
138     my($name, $conf) = @_;
139     my $class = $conf->{class};
140     Mouse::load_class($class);
141     _subtype(
142         $name => where => sub { $_->isa($class) }
143     );
144 }
145
146 sub _role_type {
147     my($name, $conf) = @_;
148     my $role = $conf->{role};
149     _subtype(
150         $name => where => sub {
151             return unless defined $_ && ref($_) && $_->isa('Mouse::Object');
152             $_->meta->does_role($role);
153         }
154     );
155 }
156
157 sub typecast_constraints {
158     my($class, $pkg, $type_constraint, $types, $value) = @_;
159
160     local $_;
161     for my $type (ref($types) eq 'ARRAY' ? @{ $types } : ( $types )) {
162         next unless $COERCE{$type};
163         for my $coerce_type (@{ $COERCE_KEYS{$type}}) {
164             $_ = $value;
165             next unless $SUBTYPE{$coerce_type}->();
166             $_ = $value;
167             $_ = $COERCE{$type}->{$coerce_type}->();
168             return $_ if $type_constraint->();
169         }
170     }
171     return $value;
172 }
173
174 1;
175
176 __END__
177
178 =head1 NAME
179
180 Mouse::Util::TypeConstraints - simple type constraints
181
182 =head1 METHODS
183
184 =head2 optimized_constraints -> HashRef[CODE]
185
186 Returns the simple type constraints that Mouse understands.
187
188 =cut
189
190