diff options
author | dakkar <dakkar@thenautilus.net> | 2016-11-10 14:26:47 +0000 |
---|---|---|
committer | dakkar <dakkar@thenautilus.net> | 2016-11-10 14:26:47 +0000 |
commit | 3db58a4fe8a121093bc5c5287eb808f71de87441 (patch) | |
tree | b6ecd45739d931ae4a096ac2876674e16386a001 /lib/Data/QRCode/Types.pm | |
parent | move example script into examples/ (diff) | |
download | Data-QRCode-3db58a4fe8a121093bc5c5287eb808f71de87441.tar.gz Data-QRCode-3db58a4fe8a121093bc5c5287eb808f71de87441.tar.bz2 Data-QRCode-3db58a4fe8a121093bc5c5287eb808f71de87441.zip |
move QRcode* to ::Result, wrap in Moo
Diffstat (limited to 'lib/Data/QRCode/Types.pm')
-rw-r--r-- | lib/Data/QRCode/Types.pm | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/lib/Data/QRCode/Types.pm b/lib/Data/QRCode/Types.pm new file mode 100644 index 0000000..3c5eec7 --- /dev/null +++ b/lib/Data/QRCode/Types.pm @@ -0,0 +1,57 @@ +package Data::QRCode::Types; +use Type::Library + -base, + -declare => qw( QRCodeEC QRCodeMode ); +use Type::Utils -all; +use Types::Standard qw(Int Str); +use Data::QRCode::Input; +use Carp; + +my %letter_for_ec = ( + Data::QRCode::Input::ECLEVEL_L() => 'L', + Data::QRCode::Input::ECLEVEL_M() => 'M', + Data::QRCode::Input::ECLEVEL_Q() => 'Q', + Data::QRCode::Input::ECLEVEL_H() => 'H', +); +my %ec_for_letter = reverse %letter_for_ec; + +declare QRCodeEC, + as Int, + where { $letter_for_ec{$_} }, + message { Int->validate($_) or "$_ is not a valid raw QR error correction level, acceptable values are @{[ sort keys %letter_for_ec ]}" }; + +coerce QRCodeEC, + from Str, + via { + return $_ if /^[0-9]+$/; + $ec_for_letter{uc($_)} + || croak "$_ is not a valid simbolic QR error correction level, acceptable values are @{[ sort keys %ec_for_letter ]}"; + }; + +my %name_for_mode = ( + Data::QRCode::Input::MODE_NUM() => 'NUM', + Data::QRCode::Input::MODE_AN() => 'AN', + Data::QRCode::Input::MODE_8() => '8BIT', + Data::QRCode::Input::MODE_KANJI() => 'KANJI', + Data::QRCode::Input::MODE_STRUCTURE() => 'STRUCTURE', + Data::QRCode::Input::MODE_ECI() => 'ECI', + Data::QRCode::Input::MODE_FNC1FIRST() => 'FNC1FIRST', + Data::QRCode::Input::MODE_FNC1SECOND() => 'FNC1SECOND', +); +my %mode_for_name = reverse %name_for_mode; + +declare QRCodeMode, + as Int, + where { $name_for_mode{$_} }, + message { Int->validate($_) or "$_ is not a valid raw QR error data mode, acceptable values are @{[ sort keys %name_for_mode ]}" }; + +coerce QRCodeMode, + from Str, + via { + return $_ if /^[0-9]+$/; + $mode_for_name{uc($_)} + || croak "$_ is not a valid simbolic QR data mode, acceptable values are @{[ sort keys %mode_for_name ]}"; + }; + +1; + |