File Coverage

blib/lib/AnyEvent/ZeroMQ/Types.pm
Criterion Covered Total %
statement 11 13 84.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 16 18 88.8


line stmt bran cond sub pod time code
1             package AnyEvent::ZeroMQ::Types;
2             BEGIN {
3 1     1   650 $AnyEvent::ZeroMQ::Types::VERSION = '0.01';
4             }
5             # ABSTRACT: Type constraints for data passed to the ZMQ library
6 1     1   6 use strict;
  1         3  
  1         28  
7 1     1   4 use warnings;
  1         3  
  1         26  
8 1     1   340209 use Regexp::Common qw /net/;
  1         7069  
  1         7  
9              
10             my @socket_constants =
11             qw(ZMQ_REQ ZMQ_REP ZMQ_PUSH ZMQ_PULL ZMQ_PUB ZMQ_SUB);
12              
13 1     1   269657 use ZeroMQ::Raw::Constants (@socket_constants);
  0            
  0            
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__
75             =pod
76              
77             =head1 NAME
78              
79             AnyEvent::ZeroMQ::Types - Type constraints for data passed to the ZMQ library
80              
81             =head1 VERSION
82              
83             version 0.01
84              
85             =head1 AUTHOR
86              
87             Jonathan Rockway <jrockway@cpan.org>
88              
89             =head1 COPYRIGHT AND LICENSE
90              
91             This software is copyright (c) 2011 by Jonathan Rockway.
92              
93             This is free software; you can redistribute it and/or modify it under
94             the same terms as the Perl 5 programming language system itself.
95              
96             =cut
97