ZeroMQ/Types.pm

filter: Statementstmt 84.6% 11 / 13 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 88.8% 16 / 18 CC 1 SCAR 0.6
CC
1
Cov
85%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« ZeroMQ/Role/WithHandle.pm ↑ Summary ZeroMQ/Publish.pm »
1 package AnyEvent::ZeroMQ::Types;
2 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 $AnyEvent::ZeroMQ::Types::VERSION = '0.01';
4 }
5 # ABSTRACT: Type constraints for data passed to the ZMQ library
6 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Regexp::Common qw /net/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 my @socket_constants =
11 qw(ZMQ_REQ ZMQ_REP ZMQ_PUSH ZMQ_PULL ZMQ_PUB ZMQ_SUB);
12  
13 0 ▶use ZeroMQ::Raw::Constants (@socket_constants);
Subroutine BEGIN: called CC=1 SCAR=2.6
14 use MooseX::Types::Moose qw(Str Int ArrayRef);
15 use MooseX::Types -declare => [
16 qw/Endpoint Endpoints SocketType SocketDirection IdentityStr/
17 ];
18 use true;
19  
20 subtype Endpoint, as Str, where {
21 # if you have a trailing slash on a tcp address, the entire
22 # fucking program dies. fucking C programmers!
23  
24 my $interface = qr/[a-z]+[0-9]*/;
25 my $host = qr/[A-Za-z0-9.-]+/;
26 my $ip = qr/$RE{net}{IPv4}/;
27 my $andport = qr/:[0-9]+/;
28  
29 if(my ($proto, $rest) = m{^([a-z]+)://(.+)$}){
30 return 1 if $proto eq 'inproc';
31 return 1 if $proto eq 'ipc';
32 return 1 if $proto eq 'tcp' && $rest =~ /^(?:$host|$ip|$interface|\*)$andport$/;
33 return 1
34 if ($proto eq 'pgm' || $proto eq 'epgm') &&
35 $rest =~ /^(?:$interface|$ip);$ip$andport$/;
36 return 0;
37 }
38 return 0;
39  
40 }, message { 'An endpoint must be in the form "<transport>://<address>"' };
41  
42 subtype Endpoints, as ArrayRef[Endpoint], message {
43 'Each endpoint must be in the form "<transport>://<address>"';
44 };
45  
46 sub fixup_endpoint() {
47 s{(^[/])/$}{$1}g;
48 }
49  
50 coerce Endpoint, from Str, via { fixup_endpoint };
51  
52 coerce Endpoints, from ArrayRef[Str], via {
53 my @array = @$_;
54 fixup_endpoint for @array;
55 $_ = [@array];
56 };
57  
58 my %allowed_sockettype = map { ZeroMQ::Raw::Constants->$_ => $_ } @socket_constants;
59 subtype SocketType, as Int, where {
60 exists $allowed_sockettype{$_};
61 }, message { 'A socket type must be one of: '. join(', ', @socket_constants) };
62  
63 subtype IdentityStr, as Str, where {
64 length $_ < 256 && length $_ >= 0;
65 # it must also not start with \0, but that is technically legal
66 # and if the user wants to do it, it's between him and the man
67 # page authors. *i'm* not getting involved :)
68 }, message { 'The identity must be non-empty and no more than 255 characters.' };
69  
70 subtype SocketDirection, as Str, where {
71 /^(r|rw|wr|w|)$/;
72 }, message { "Socket direction must be r, w, rw, or the empty string; not '$_'" };
73  
74 __END__
« ZeroMQ/Role/WithHandle.pm ↑ Summary ZeroMQ/Publish.pm »