summaryrefslogtreecommitdiff
path: root/lib/Type/Coercion/FromMoose.pm
blob: bbab802dde87747068ea8366ab050ba4b0fc22c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package Type::Coercion::FromMoose;

use 5.006001;
use strict;
use warnings;

BEGIN {
	$Type::Coercion::FromMoose::AUTHORITY = 'cpan:TOBYINK';
	$Type::Coercion::FromMoose::VERSION   = '1.016007';
}

$Type::Coercion::FromMoose::VERSION =~ tr/_//d;

use Scalar::Util qw< blessed >;
use Types::TypeTiny ();

sub _croak ($;@) { require Error::TypeTiny; goto \&Error::TypeTiny::croak }

require Type::Coercion;
our @ISA = 'Type::Coercion';

sub type_coercion_map {
	my $self = shift;
	
	my @from;
	if ( $self->type_constraint ) {
		my $moose = $self->type_constraint->{moose_type};
		@from = @{ $moose->coercion->type_coercion_map }
			if $moose && $moose->has_coercion;
	}
	else {
		_croak
			"The type constraint attached to this coercion has been garbage collected... PANIC";
	}
	
	my @return;
	while ( @from ) {
		my ( $type, $code ) = splice( @from, 0, 2 );
		$type = Moose::Util::TypeConstraints::find_type_constraint( $type )
			unless ref $type;
		push @return, Types::TypeTiny::to_TypeTiny( $type ), $code;
	}
	
	return \@return;
} #/ sub type_coercion_map

sub add_type_coercions {
	my $self = shift;
	_croak "Adding coercions to Type::Coercion::FromMoose not currently supported"
		if @_;
}

sub _build_moose_coercion {
	my $self = shift;
	
	if ( $self->type_constraint ) {
		my $moose = $self->type_constraint->{moose_type};
		return $moose->coercion if $moose && $moose->has_coercion;
	}
	
	$self->SUPER::_build_moose_coercion( @_ );
} #/ sub _build_moose_coercion

sub can_be_inlined {
	0;
}

1;

__END__

=pod

=encoding utf-8

=head1 NAME

Type::Coercion::FromMoose - a set of coercions borrowed from Moose

=head1 STATUS

This module is considered part of Type-Tiny's internals. It is not
covered by the
L<Type-Tiny stability policy|Type::Tiny::Manual::Policies/"STABILITY">.

=head1 DESCRIPTION

This package inherits from L<Type::Coercion>; see that for most documentation.
The major differences are that C<add_type_coercions> always throws an
exception, and the C<type_coercion_map> is automatically populated from
Moose.

This is mostly for internal purposes.

=head1 BUGS

Please report any bugs to
L<https://github.com/tobyink/p5-type-tiny/issues>.

=head1 SEE ALSO

L<Type::Coercion>.

L<Moose::Meta::TypeCoercion>.

=head1 AUTHOR

Toby Inkster E<lt>tobyink@cpan.orgE<gt>.

=head1 COPYRIGHT AND LICENCE

This software is copyright (c) 2013-2014, 2017-2022 by Toby Inkster.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=head1 DISCLAIMER OF WARRANTIES

THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.