tobyink / p5-type-tiny

Perl 5 distribution Type-Tiny; see homepage for downloads and documentation.
https://typetiny.toby.ink/
54 stars 48 forks source link

Bitfield types #131

Closed tobyink closed 1 year ago

tobyink commented 1 year ago

For things like:

package MyApp::Line {
  use Moo;
  use Types::Common -types;

  use Type::Tiny::Bitfield LineStyle => {
    RED         => 1,
    GREEN       => 2,
    BLUE        => 4,
    DOTTED      => 64,
  };

  has [ 'start', 'end' ] => (
    is       => 'ro',
    isa      => InstanceOf[ 'MyApp::Point' ],
  );

  has style => (
    is       => 'ro',
    isa      => LineStyle,
    default  => 0,
  );
}

# A solid purple line from $p1 to $p2...
#
my $line = MyApp::Line->new(
  start    => $p1,
  end      => $p2,
  style    => MyApp::Line::LINESTYLE_RED | MyApp::Line::LINESTYLE_BLUE,
);

# A dotted green line:
#
my $s = MyApp::Line::LineStyle();
my $line2 = MyApp::Line->new(
  start    => $p3,
  end      => $p4,
  style    => $s->GREEN | $s->DOTTED,
);

Setting the style attribute to 8 would be a type error as there's no way to combine RED, BLUE, GREEN, and DOTTED to make 8.

codecov[bot] commented 1 year ago

Codecov Report

Merging #131 (0f21482) into master (d5248b7) will increase coverage by 0.06%. The diff coverage is 94.17%.

@@            Coverage Diff             @@
##           master     #131      +/-   ##
==========================================
+ Coverage   91.77%   91.84%   +0.06%     
==========================================
  Files          29       30       +1     
  Lines        3525     3628     +103     
  Branches      882      902      +20     
==========================================
+ Hits         3235     3332      +97     
  Misses          5        5              
- Partials      285      291       +6     
Impacted Files Coverage Δ
lib/Type/Tiny/Bitfield.pm 94.17% <94.17%> (ø)

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more