LibTmux/Operation.pm

filter: Statementstmt 94.8% 73 / 77 Branchbran 63.8% 23 / 36 Conditioncond 50.0% 16 / 32 MC/DCmcdc 25.0% 5 / 20 Subroutinesub 94.7% 18 / 19 Podpod 100% 6 / 6 totaltotal 74.2% 141 / 190 CC 32 SCAR 15.9
CC
32
Cov
77%
request
12.1
_drive
7.1
catch
6.0
CRAP 4.9
« LibTmux/Selection.pm ↑ Summary LibTmux/CommandResult.pm »
1 package LibTmux::Operation;
2 4 ▶use v5.44;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use LibTmux::X;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 64 ▶sub _copy_request ($request) {
Subroutine _copy_request: called CC=3 SCAR=11.0
7 64 my %copy = %$request;
8 64 ▶ $copy{argv} = [($request->{argv} // [])->@*];
Condition50%1/2
$request->{"argv"} // []
l!l
640
MC/DC0%0/1
$request->{"argv"} // []
$request->{"argv"}
Truth table50%1/2
A
A= $request->{"argv"}
Aresult
11
00
9 64 ▶ $copy{env} = {%{$request->{env}}} if exists $request->{env};
Branch: 1 branch
BranchTrueFalseTotal
if exists $request->{"env"} 6 58 64
10 64 return \%copy;
11 }
12  
13 32 ▶sub request ($class, %request) {
Subroutine request: called CC=7 SCAR=24.9
Pod documented
14 LibTmux::X::InvalidArgument->throw(
15 message => 'prepared request needs a nonempty argv')
16 32 ▶ unless ref($request{argv}) eq 'ARRAY' && $request{argv}->@*;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $request{"argv"} eq "ARRAY" and @{$request{"argv"}} 0 32 32
Condition33%1/3
ref $request{"argv"} eq "ARRAY" and @{$request{"argv"}}
!ll&&!rl&&r
0032
MC/DC0%0/2
ref $request{"argv"} eq "ARRAY" and @{$request{"argv"}}
ref $request{"argv"} eq "ARRAY" @{$request{"argv"}}
Truth table33%1/3
A and B
A= ref $request{"argv"} eq "ARRAY"B= @{$request{"argv"}}
ABresult
0X0
100
111
17 32 for my $arg ($request{argv}->@*) {
18 98 ▶ LibTmux::X::InvalidArgument->throw(
Branch: 1 branch
BranchTrueFalseTotal
if not defined $arg or ref $arg or index($arg, "\0") != -1 0 98 98
Condition33%2/6
not defined $arg or ref $arg or index($arg, "\0") != -1
l!l&&r!l&&!r
0098
not defined $arg or ref $arg
l!l&&r!l&&!r
0098
MC/DC0%0/3
not defined $arg or ref $arg or index($arg, "\0") != -1
not defined $arg ref $arg index($arg, "\0") != -1
Truth table25%1/4
not(A) or B or C
A= not defined $argB= ref $argC= index($arg, "\0") != -1
ABCresult
1XX1
01X1
0011
0000
19 message => 'prepared arguments must be defined NUL-free scalars')
20 if !defined($arg) || ref($arg) || index($arg, "\0") >= 0;
21 }
22 32 return bless {tag => 'request', request => _copy_request(\%request)},
23 $class;
24 }
25 34 ▶sub value ($class, $value) { bless {tag => 'value', value => $value}, $class }
Subroutine value: called CC=1 SCAR=0.0
Pod documented
26  
27 89 ▶sub then ($self, $callback) {
Subroutine then: called CC=2 SCAR=7.2
Pod documented
28 89 ▶ LibTmux::X::InvalidArgument->throw(message => 'then needs a coderef')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $callback eq "CODE" 0 89 89
29 unless ref($callback) eq 'CODE';
30 89 bless {tag => 'then', previous => $self, callback => $callback}, ref $self;
31 }
32  
33 0 ▶sub catch ($self, $callback) {
Subroutine catch: not called CC=2 SCAR=17.9
Pod documented
34 0 ▶ LibTmux::X::InvalidArgument->throw(message => 'catch needs a coderef')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $callback eq "CODE" 0 0 0
35 unless ref($callback) eq 'CODE';
36 0 bless {tag => 'catch', previous => $self, callback => $callback}, ref $self;
37 }
38  
39 2 ▶sub sequence ($class, $operations) {
Subroutine sequence: called CC=2 SCAR=6.9
Pod documented
40 2 my $op = $class->value([]);
41 2 for my $next (@$operations) {
42 $op = $op->then(
43 8 ▶ sub ($values) {
Subroutine __ANON__: called CC=1 SCAR=0.0
44 8 ▶ $next->then(sub ($value) { [@$values, $value] });
Subroutine __ANON__: called CC=1 SCAR=0.0
45 }
46 8 );
47 }
48 2 return $op;
49 }
50  
51 23 ▶sub run ($self, $executor, $done, $fail) {
Subroutine run: called CC=1 SCAR=0.3
Pod documented
52 23 my $state = {cancelled => false, finished => false, hook => undef};
53 11 ▶ my $success = sub ($value) {
Subroutine __ANON__: called CC=3 SCAR=12.0
54 11 ▶ return if $state->{cancelled} || $state->{finished};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"cancelled"} or $state->{"finished"} 0 11 11
Condition33%1/3
$state->{"cancelled"} or $state->{"finished"}
l!l&&r!l&&!r
0011
MC/DC0%0/2
$state->{"cancelled"} or $state->{"finished"}
$state->{"cancelled"} $state->{"finished"}
Truth table33%1/3
A or B
A= $state->{"cancelled"}B= $state->{"finished"}
ABresult
1X1
011
000
55 11 $state->{finished} = true;
56 11 $state->{hook} = undef;
57 11 $done->($value);
58 23 };
59 11 ▶ my $failure = sub ($error) {
Subroutine __ANON__: called CC=3 SCAR=12.0
60 11 ▶ return if $state->{cancelled} || $state->{finished};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"cancelled"} or $state->{"finished"} 0 11 11
Condition33%1/3
$state->{"cancelled"} or $state->{"finished"}
l!l&&r!l&&!r
0011
MC/DC0%0/2
$state->{"cancelled"} or $state->{"finished"}
$state->{"cancelled"} $state->{"finished"}
Truth table33%1/3
A or B
A= $state->{"cancelled"}B= $state->{"finished"}
ABresult
1X1
011
000
61 11 $state->{finished} = true;
62 11 $state->{hook} = undef;
63 11 $fail->($error);
64 23 };
65 23 _drive($self, $executor, $success, $failure, $state);
66 1 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=15.6
67 1 ▶ return if $state->{finished} || $state->{cancelled};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"finished"} or $state->{"cancelled"} 0 1 1
Condition33%1/3
$state->{"finished"} or $state->{"cancelled"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$state->{"finished"} or $state->{"cancelled"}
$state->{"finished"} $state->{"cancelled"}
Truth table33%1/3
A or B
A= $state->{"finished"}B= $state->{"cancelled"}
ABresult
1X1
011
000
68 1 $state->{cancelled} = true;
69 1 my $hook = delete $state->{hook};
70 1 ▶ $hook->() if $hook;
Branch: 1 branch
BranchTrueFalseTotal
if $hook 1 0 1
71 23 };
72 }
73  
74 155 ▶sub _drive ($op, $executor, $done, $fail, $state) {
Subroutine _drive: called CC=7 SCAR=19.7
75 155 ▶ return if $state->{cancelled};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"cancelled"} 0 155 155
76 155 ▶ if ($op->{tag} eq 'value') { $done->($op->{value}); return }
Branch: 1 branch
BranchTrueFalseTotal
if ($op->{"tag"} eq "value") 34 121 155
77 121 ▶ if ($op->{tag} eq 'request') {
Branch: 1 branch
BranchTrueFalseTotal
if ($op->{"tag"} eq "request") 32 89 121
78 32 my $settled = false;
79 31 ▶ my $success = sub ($value) {
Subroutine __ANON__: called CC=3 SCAR=11.0
80 31 ▶ return if $state->{cancelled} || $settled;
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"cancelled"} or $settled 2 29 31
Condition100%3/3
$state->{"cancelled"} or $settled
l!l&&r!l&&!r
1129
MC/DC100%2/2
$state->{"cancelled"} or $settled
$state->{"cancelled"} $settled
Truth table100%3/3
A or B
A= $state->{"cancelled"}B= $settled
ABresult
1X1
011
000
81 29 $settled = true;
82 29 $state->{hook} = undef;
83 29 $done->($value);
84 32 };
85 2 ▶ my $failure = sub ($error) {
Subroutine __ANON__: called CC=3 SCAR=12.0
86 2 ▶ return if $state->{cancelled} || $settled;
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"cancelled"} or $settled 0 2 2
Condition33%1/3
$state->{"cancelled"} or $settled
l!l&&r!l&&!r
002
MC/DC0%0/2
$state->{"cancelled"} or $settled
$state->{"cancelled"} $settled
Truth table33%1/3
A or B
A= $state->{"cancelled"}B= $settled
ABresult
1X1
011
000
87 2 $settled = true;
88 2 $state->{hook} = undef;
89 2 $fail->($error);
90 32 };
91 32 try {
92 my $hook =
93 32 $executor->(_copy_request($op->{request}), $success, $failure);
94 32 ▶ $state->{hook} = $hook unless $settled || $state->{cancelled};
Branch: 1 branch
BranchTrueFalseTotal
unless $settled or $state->{"cancelled"} 3 29 32
Condition66%2/3
$settled or $state->{"cancelled"}
l!l&&r!l&&!r
2903
MC/DC50%1/2
$settled or $state->{"cancelled"}
$settled $state->{"cancelled"}
Truth table66%2/3
A or B
A= $settledB= $state->{"cancelled"}
ABresult
1X1
011
000
95 }
96 0 catch ($error) { $failure->($error) }
97 32 return;
98 }
99 85 ▶ my $transform = sub ($value) {
Subroutine __ANON__: called CC=4 SCAR=13.9
100 85 ▶ return if $state->{cancelled};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"cancelled"} 0 85 85
101 85 try {
102 85 my $next = $op->{callback}->($value);
103 76 ▶ if (blessed($next) && $next->isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($next) and $next->isa(__PACKAGE__)) { } 43 33 76
Condition100%3/3
builtin::blessed($next) and $next->isa(__PACKAGE__)
!ll&&!rl&&r
132043
MC/DC100%2/2
builtin::blessed($next) and $next->isa(__PACKAGE__)
builtin::blessed($next) $next->isa(__PACKAGE__)
Truth table100%3/3
A and B
A= builtin::blessed($next)B= $next->isa(__PACKAGE__)
ABresult
0X0
100
111
104 43 _drive($next, $executor, $done, $fail, $state);
105 }
106 33 else { $done->($next) }
107 }
108 9 catch ($error) { $fail->($error) }
109 89 };
110 89 ▶ if ($op->{tag} eq 'then') {
Branch: 1 branch
BranchTrueFalseTotal
if ($op->{'tag'} eq 'then') { } 89 0 89
111 89 _drive($op->{previous}, $executor, $transform, $fail, $state);
112 }
113 0 else { _drive($op->{previous}, $executor, $done, $transform, $state) }
114 }
115  
116 =head1 NAME
117  
118 LibTmux::Operation - Executor-independent prepared command continuations
119  
120 =head1 METHODS
121  
122 =head2 request
123  
124 C<request(argv =E<gt> [...], stdin =E<gt> $octets, ...)> owns its input
125 arguments. Execution is explicit through L</run>; preparation performs no I/O.
126  
127 =head2 value
128  
129 Wrap one local value, including undef for a documented void operation.
130  
131 =head2 then
132  
133 Compose a local transformation returning one value or another Operation.
134 Exceptions retain their original identity.
135  
136 =head2 catch
137  
138 Recover a failure with one value or another Operation.
139  
140 =head2 sequence
141  
142 Execute an array of Operations in order, returning one arrayref of results.
143  
144 =head2 run
145  
146 C<run($executor, $on_done, $on_fail)> supplies requests and two one-argument
147 completion callbacks to the executor. The executor returns a cancellation
148 hook. C<run> returns a cancellation coderef suppressing further submissions.
149 Each request settles once; the complete operation yields one value or error.
150  
151 The synchronous L<LibTmux::Server> and optional async distribution use the
152 same preparation, codecs, and graph semantics. Core has no Future dependency.
153  
154 =cut
« LibTmux/Selection.pm ↑ Summary LibTmux/CommandResult.pm »