45 lines
1.0 KiB
Perl
45 lines
1.0 KiB
Perl
package BeastBB::Types;
|
|
|
|
use 5.30.3;
|
|
|
|
use strict;
|
|
use warnings;
|
|
|
|
use Exporter qw/import/;
|
|
use Scalar::Util qw/blessed/;
|
|
use Type::Tiny;
|
|
|
|
use Const::Fast;
|
|
|
|
our @EXPORT_OK = ( '&IsClassTypeGenerator', '$MATRIX_ADDRESS_REGEX', '$MATRIX_ADDRESS_TYPE' );
|
|
|
|
const our $MATRIX_ADDRESS_REGEX => qr/^@\w+:(\w|\.)+\.(\w+)$/;
|
|
|
|
|
|
const our $MATRIX_ADDRESS_TYPE => Type::Tiny->new(
|
|
name => "MatrixAddressChecker",
|
|
constraint => sub {
|
|
my $matrix_address = shift;
|
|
return 1 if $matrix_address =~ /$MATRIX_ADDRESS_REGEX/;
|
|
}
|
|
);
|
|
|
|
my %generated_classes;
|
|
|
|
sub IsClassTypeGenerator {
|
|
my $class = shift;
|
|
if ( !exists $generated_classes{$class} ) {
|
|
my $sanitized_class = $class =~ s/:://gr;
|
|
$generated_classes{$class} = Type::Tiny->new(
|
|
name => "Is$sanitized_class",
|
|
constraint => sub {
|
|
my $item_to_test = shift;
|
|
return 1 if blessed $item_to_test && $item_to_test->isa($class);
|
|
return 0;
|
|
},
|
|
);
|
|
}
|
|
return $generated_classes{$class};
|
|
}
|
|
1;
|