implement Maybe
[gitmo/Mouse.git] / lib / Mouse / Util / TypeConstraints.pm
CommitLineData
3b46bd49 1package Mouse::Util::TypeConstraints;
d60c78b9 2use strict;
3use warnings;
139d92d2 4use base 'Exporter';
9baf5d6b 5
61a02a3a 6use Carp ();
6c169c50 7use Scalar::Util qw/blessed looks_like_number openhandle/;
d60c78b9 8
139d92d2 9our @EXPORT = qw(
d44f0d03 10 as where message from via type subtype coerce class_type role_type enum
139d92d2 11);
12
cceb0e25 13my %TYPE;
7dbebb1b 14my %TYPE_SOURCE;
8a7f2a8a 15my %COERCE;
16my %COERCE_KEYS;
4188b837 17
139d92d2 18sub as ($) {
61a02a3a 19 as => $_[0]
20}
139d92d2 21sub where (&) {
61a02a3a 22 where => $_[0]
23}
0f1dae9a 24sub message (&) {
61a02a3a 25 message => $_[0]
26}
27
139d92d2 28sub from { @_ }
29sub via (&) {
61a02a3a 30 $_[0]
31}
32
381f326a 33my $optimized_constraints;
34my $optimized_constraints_base;
35{
36 no warnings 'uninitialized';
cceb0e25 37 %TYPE = (
381f326a 38 Any => sub { 1 },
39 Item => sub { 1 },
40 Bool => sub {
c91d12e0 41 !defined($_[0]) || $_[0] eq "" || "$_[0]" eq '1' || "$_[0]" eq '0'
381f326a 42 },
c91d12e0 43 Undef => sub { !defined($_[0]) },
44 Defined => sub { defined($_[0]) },
45 Value => sub { defined($_[0]) && !ref($_[0]) },
46 Num => sub { !ref($_[0]) && looks_like_number($_[0]) },
47 Int => sub { defined($_[0]) && !ref($_[0]) && $_[0] =~ /^-?[0-9]+$/ },
48 Str => sub { defined($_[0]) && !ref($_[0]) },
49 ClassName => sub { Mouse::is_class_loaded($_[0]) },
50 Ref => sub { ref($_[0]) },
51
52 ScalarRef => sub { ref($_[0]) eq 'SCALAR' },
53 ArrayRef => sub { ref($_[0]) eq 'ARRAY' },
54 HashRef => sub { ref($_[0]) eq 'HASH' },
55 CodeRef => sub { ref($_[0]) eq 'CODE' },
56 RegexpRef => sub { ref($_[0]) eq 'Regexp' },
57 GlobRef => sub { ref($_[0]) eq 'GLOB' },
381f326a 58
59 FileHandle => sub {
c91d12e0 60 ref($_[0]) eq 'GLOB' && openhandle($_[0])
381f326a 61 or
c91d12e0 62 blessed($_[0]) && $_[0]->isa("IO::Handle")
abe4e887 63 },
381f326a 64
c91d12e0 65 Object => sub { blessed($_[0]) && blessed($_[0]) ne 'Regexp' },
8a7f2a8a 66 );
d3982c7e 67
cceb0e25 68 sub optimized_constraints { \%TYPE }
69 my @TYPE_KEYS = keys %TYPE;
70 sub list_all_builtin_type_constraints { @TYPE_KEYS }
7dbebb1b 71
72 @TYPE_SOURCE{@TYPE_KEYS} = (__PACKAGE__) x @TYPE_KEYS;
381f326a 73}
d3982c7e 74
139d92d2 75sub type {
0d9fea22 76 my $pkg = caller(0);
77 my($name, %conf) = @_;
0d062abb 78 if ($TYPE{$name} && $TYPE_SOURCE{$name} ne $pkg) {
7dbebb1b 79 Carp::croak "The type constraint '$name' has already been created in $TYPE_SOURCE{$name} and cannot be created again in $pkg";
0d9fea22 80 };
7dbebb1b 81 my $constraint = $conf{where} || do { $TYPE{delete $conf{as} || 'Any' } };
82
83 $TYPE_SOURCE{$name} = $pkg;
c91d12e0 84 $TYPE{$name} = sub { local $_=$_[0]; $constraint->($_) };
0d9fea22 85}
86
139d92d2 87sub subtype {
4188b837 88 my $pkg = caller(0);
61a02a3a 89 my($name, %conf) = @_;
0d062abb 90 if ($TYPE{$name} && $TYPE_SOURCE{$name} ne $pkg) {
7dbebb1b 91 Carp::croak "The type constraint '$name' has already been created in $TYPE_SOURCE{$name} and cannot be created again in $pkg";
61a02a3a 92 };
310ad28b 93 my $constraint = $conf{where} || do {
94 my $as = delete $conf{as} || 'Any';
95 if (! exists $TYPE{$as}) { # Perhaps it's a parameterized source?
96 Mouse::Meta::Attribute::_build_type_constraint($as);
97 }
98 $TYPE{$as};
99 };
7dbebb1b 100 my $as = $conf{as} || '';
101
102 $TYPE_SOURCE{$name} = $pkg;
103
cceb0e25 104 if ($as = $TYPE{$as}) {
c91d12e0 105 $TYPE{$name} = sub { local $_=$_[0]; $as->($_) && $constraint->($_) };
1fbefea5 106 } else {
c91d12e0 107 $TYPE{$name} = sub { local $_=$_[0]; $constraint->($_) };
1fbefea5 108 }
4188b837 109}
110
139d92d2 111sub coerce {
61a02a3a 112 my($name, %conf) = @_;
113
114 Carp::croak "Cannot find type '$name', perhaps you forgot to load it."
cceb0e25 115 unless $TYPE{$name};
61a02a3a 116
8a7f2a8a 117 unless ($COERCE{$name}) {
118 $COERCE{$name} = {};
119 $COERCE_KEYS{$name} = [];
120 }
61a02a3a 121 while (my($type, $code) = each %conf) {
122 Carp::croak "A coercion action already exists for '$type'"
8a7f2a8a 123 if $COERCE{$name}->{$type};
61a02a3a 124
310ad28b 125 if (! $TYPE{$type}) {
126 # looks parameterized
127 if ($type =~ /^[^\[]+\[.+\]$/) {
128 Mouse::Meta::Attribute::_build_type_constraint($type);
129 } else {
130 Carp::croak "Could not find the type constraint ($type) to coerce from"
131 }
132 }
61a02a3a 133
8a7f2a8a 134 push @{ $COERCE_KEYS{$name} }, $type;
135 $COERCE{$name}->{$type} = $code;
61a02a3a 136 }
4188b837 137}
138
139d92d2 139sub class_type {
ecc6e3b1 140 my $pkg = caller(0);
ecc6e3b1 141 my($name, $conf) = @_;
142 my $class = $conf->{class};
139d92d2 143 subtype(
4a957f05 144 $name => where => sub { $_->isa($class) }
61a02a3a 145 );
ecc6e3b1 146}
147
139d92d2 148sub role_type {
47f36c05 149 my($name, $conf) = @_;
150 my $role = $conf->{role};
139d92d2 151 subtype(
61a02a3a 152 $name => where => sub {
153 return unless defined $_ && ref($_) && $_->isa('Mouse::Object');
154 $_->meta->does_role($role);
155 }
156 );
47f36c05 157}
158
4188b837 159sub typecast_constraints {
eec1bb49 160 my($class, $pkg, $type_constraint, $types, $value) = @_;
eec1bb49 161
b3b74cc6 162 local $_;
eec1bb49 163 for my $type (ref($types) eq 'ARRAY' ? @{ $types } : ( $types )) {
8a7f2a8a 164 next unless $COERCE{$type};
8a7f2a8a 165 for my $coerce_type (@{ $COERCE_KEYS{$type}}) {
b3b74cc6 166 $_ = $value;
c91d12e0 167 next unless $TYPE{$coerce_type}->($value);
b3b74cc6 168 $_ = $value;
c91d12e0 169 $_ = $COERCE{$type}->{$coerce_type}->($value);
170 return $_ if $type_constraint->($_);
4188b837 171 }
172 }
4188b837 173 return $value;
174}
175
01904723 176my $serial_enum = 0;
d44f0d03 177sub enum {
01904723 178 # enum ['small', 'medium', 'large']
179 if (ref($_[0]) eq 'ARRAY') {
180 my @elements = @{ shift @_ };
181
182 my $name = 'Mouse::Util::TypeConstaints::Enum::Serial::'
183 . ++$serial_enum;
184 enum($name, @elements);
185 return $name;
186 }
187
188 # enum size => 'small', 'medium', 'large'
d44f0d03 189 my $name = shift;
190 my %is_valid = map { $_ => 1 } @_;
191
192 subtype(
193 $name => where => sub { $is_valid{$_} }
194 );
195}
196
d60c78b9 1971;
198
6feb83f1 199__END__
200
201=head1 NAME
202
3b46bd49 203Mouse::Util::TypeConstraints - simple type constraints
6feb83f1 204
205=head1 METHODS
206
207=head2 optimized_constraints -> HashRef[CODE]
208
209Returns the simple type constraints that Mouse understands.
210
c91d12e0 211=head1 FUNCTIONS
212
213=over 4
214
215=item B<subtype 'Name' => as 'Parent' => where { } ...>
216
217=item B<subtype as 'Parent' => where { } ...>
218
219=item B<class_type ($class, ?$options)>
220
221=item B<role_type ($role, ?$options)>
222
223=item B<enum (\@values)>
224
225=back
226
6feb83f1 227=cut
228
229