line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MouseX::Types::Data::Monad::Either; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
633
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
30
|
|
4
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
29
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
2
|
use Carp qw( croak ); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
51
|
|
7
|
1
|
|
|
1
|
|
3
|
use Mouse::Util::TypeConstraints; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
8
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
subtype 'Either', |
10
|
|
|
|
|
|
|
as 'Data::Monad::Either', |
11
|
|
|
|
|
|
|
( |
12
|
|
|
|
|
|
|
constraint_generator => sub { |
13
|
|
|
|
|
|
|
my ($type_parameter) = @_; |
14
|
|
|
|
|
|
|
my $check = $type_parameter->_compiled_type_constraint; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
my ($left_t, $right_t) = @{ $type_parameter->{type_constraints} // [] }; |
19
|
|
|
|
|
|
|
croak 'Either must have Left and Right type constraints' |
20
|
|
|
|
|
|
|
unless defined($left_t) && ($left_t =~ m/\ALeft\[?/) && defined($right_t) && ($right_t =~ m/\ARight\[?/); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
return sub { |
23
|
|
|
|
|
|
|
my ($either) = @_; |
24
|
|
|
|
|
|
|
return $either->is_right ? $right_t->check($either) : $left_t->check($either); |
25
|
|
|
|
|
|
|
}; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
subtype 'Left', |
30
|
|
|
|
|
|
|
as 'Data::Monad::Either::Left', |
31
|
|
|
|
|
|
|
( |
32
|
|
|
|
|
|
|
constraint_generator => sub { |
33
|
|
|
|
|
|
|
my ($type_parameter) = @_; |
34
|
|
|
|
|
|
|
my $check = $type_parameter->_compiled_type_constraint; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
return sub { |
37
|
|
|
|
|
|
|
my ($left) = @_; |
38
|
|
|
|
|
|
|
my ($result) = $check->($left->value); |
39
|
|
|
|
|
|
|
return $result; |
40
|
|
|
|
|
|
|
}; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
); |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
subtype 'Right', |
45
|
|
|
|
|
|
|
as 'Data::Monad::Either::Right', |
46
|
|
|
|
|
|
|
( |
47
|
|
|
|
|
|
|
constraint_generator => sub { |
48
|
|
|
|
|
|
|
my ($type_parameter) = @_; |
49
|
|
|
|
|
|
|
my $check = $type_parameter->_compiled_type_constraint; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
return sub { |
52
|
|
|
|
|
|
|
my ($right) = @_; |
53
|
|
|
|
|
|
|
my ($result) = $check->($right->value); |
54
|
|
|
|
|
|
|
return $result; |
55
|
|
|
|
|
|
|
}; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
); |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
1; |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
__END__ |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=encoding utf-8 |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=head1 NAME |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
MouseX::Types::Data::Monad::Either - Type constraints for Data::Monad::Either |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=head1 SYNOPSIS |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
use Data::Monad::Either qw( right left ); |
72
|
|
|
|
|
|
|
use MouseX::Types::Data::Monad::Either; |
73
|
|
|
|
|
|
|
use Smart::Args qw( args ); |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub from_api { |
76
|
|
|
|
|
|
|
args my $json => 'Either[Left[Str] | Right[Int]]'; |
77
|
|
|
|
|
|
|
$json->flat_map(sub { |
78
|
|
|
|
|
|
|
# ... |
79
|
|
|
|
|
|
|
}); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
from_api(right(1)); |
83
|
|
|
|
|
|
|
from_api(left('some error')); |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head1 DESCRIPTION |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
MouseX::Types::Data::Monad::Either defines a type constraint for Data::Monad::Either. |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
C<Either> type requires a union type that consists of C<Left> and C<Right> types. |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
The reason for this strange requirement is that L<Mouse::Meta::TypeConstraint> cannot have multiple type parameters. |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head1 SEE ALSO |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
L<Mouse>, L<Data::Monad::Either> |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=head1 LICENSE |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
Copyright (C) aereal. |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
102
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head1 AUTHOR |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
aereal E<lt>aereal@aereal.orgE<gt> |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=cut |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
|