From 556f1e542cdeadb7dc2f549ec7915fe28f5bb112 Mon Sep 17 00:00:00 2001 From: dakkar Date: Mon, 29 Aug 2011 13:06:32 +0100 Subject: static rotors --- lib/Enigmatic/Types.pm | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 lib/Enigmatic/Types.pm (limited to 'lib/Enigmatic/Types.pm') diff --git a/lib/Enigmatic/Types.pm b/lib/Enigmatic/Types.pm new file mode 100644 index 0000000..12cb414 --- /dev/null +++ b/lib/Enigmatic/Types.pm @@ -0,0 +1,32 @@ +package Enigmatic::Types; +use DAKKAR::p; +use MooseX::Types -declare => [qw(Letter WiringMap RotorPos)]; +use MooseX::Types::Moose qw(Str Int); +use MooseX::Types::Structured qw(Map); + +subtype Letter, + as Str, + where { /[A-Z]/ }; + +subtype WiringMap, + as Map[Letter,Letter], + where { + my $in = $_->keys->sort->join; + my $out = $_->values->sort->join; + $in eq $out and $in eq ['A'..'Z']->join; + }; + +coerce WiringMap, + from Str, + via { + my @out = $_->uc->split(qr//)->flatten; + croak "invalid wiring string <$_>" + unless @out == 26; + my %ret; + @ret{'A'..'Z'}=@out; + \%ret; + }; + +subtype RotorPos, + as Int, + where { $_ >=0 and $_ <= 26 }; -- cgit v1.2.3