blib/lib/App/FuguSeed/Words.pm

filter: Statementstmt 64.3% 83 / 129 Branchbran 38.4% 20 / 52 Conditioncond 16.6% 2 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 70.9% 22 / 31 Podpod 100% 4 / 4 totaltotal 55.5% 131 / 236 CC 33 SCAR 21.2
CC
33
Cov
54%
_build
17.2
_build
17.2
_check
14.8
CRAP 8.4
« lib/App/FuguSeed/Last.pm ↑ Summary lib/App/FuguSeed/Matrix.pm »
1 # ex:ts=8 sw=4:
2 # $OpenBSD$
3 #
4 # Copyright (c) 2026 Dick Olsson <hi@senzilla.io>
5 #
6 # Permission to use, copy, modify, and distribute this software for any
7 # purpose with or without fee is hereby granted, provided that the above
8 # copyright notice and this permission notice appear in all copies.
9 #
10 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  
18 package App::FuguSeed::Words;
19 14 our $VERSION = '0.2.0';
20  
21 14 ▶use v5.34;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 14 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 14 ▶no feature qw(indirect multidimensional bareword_filehandles);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 14 ▶use App::FuguSeed::Check ();
Subroutine BEGIN: called CC=1 SCAR=0.0
27 14 ▶use App::FuguSeed::ListFile ();
Subroutine BEGIN: called CC=1 SCAR=0.0
28 14 ▶use App::FuguSeed::Sheet ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29 14 ▶use Fugu::CLI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
30 14 ▶use Fugu::File ();
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 # App::FuguSeed::Words - the flow of fuguseed-words (WORDS-PROGRAM).
33 #
34 # The module dispatches the verbs build and check through Fugu::CLI
35 # (D-06). It reads the two share files of the sheet and the sheet
36 # under check, and it prints the result of a verb on standard output.
37 # Every diagnostic goes to standard error through the logger of the
38 # command line interface (WORDS-PROGRAM-4).
39 #
40 # The program sees no seed word, so it can run on any computer (D-01).
41 # No module that it loads maps seed words to anything, and none of them
42 # loads the mnemonic module or the check word module (SEC-TRUST-1).
43 # t/fuguseed/words-program.t proves the rule at the load, and it scans
44 # each source of the program for the names of those modules
45 # (TEST-PACK-3). The names are therefore absent from this file.
46  
47 # NAME and DIST:
48 # The name of the program in a diagnostic, and the name of the
49 # distribution that holds the share files.
50 14 ▶use constant NAME => 'fuguseed-words';
Subroutine BEGIN: called CC=1 SCAR=0.0
51 14 ▶use constant DIST => 'App-FuguSeed';
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 # SHARE:
54 # The directory of the share files, under the root of the
55 # checkout and under the share tree of an install
56 # (LIST-SHARE-2).
57 14 ▶use constant SHARE => 'share/fuguseed';
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 # SUCCESS and FAILURE:
60 # Two of the three exit codes of Fugu LIB-CLI. The third one is
61 # the usage error, and Fugu::CLI returns it
62 # (WORDS-PROGRAM-2).
63 14 ▶use constant SUCCESS => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 14 ▶use constant FAILURE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 # $class->run(@argument):
67 # Dispatch the verb and give the exit code of the program.
68 13 ▶sub run ( $class, @argument )
Subroutine run: called CC=2 SCAR=9.2
Pod documented
69 0 ▶{
Subroutine run: not called CC=2 SCAR=9.2
Pod documented
70 0 my $cli = $class->_cli;
71  
72 # Fugu::CLI prints the help for an empty argument list and it
73 # gives 0. A command line without a verb is a usage error
74 # here (WORDS-PROGRAM-3).
75 0 ▶ return $cli->usage_error unless @argument;
Branch: 2 branches
BranchTrueFalseTotal
unless @argument 1 12 13
unless @argument 0 0 0
76  
77 0 return $cli->run(@argument);
78 }
79  
80 # $class->share($name):
81 # The path of the share file $name, in a checkout and in an
82 # installed distribution, or undef when no tree holds it
83 # (LIST-SHARE-2).
84 20 ▶sub share ( $, $name )
Subroutine share: called CC=1 SCAR=1.2
Pod documented
85 0 ▶{
Subroutine share: not called CC=1 SCAR=1.2
Pod documented
86 0 return Fugu::File->share_path(
87 SHARE . "/$name",
88 from => __FILE__,
89 dist => DIST
90 );
91 }
92  
93 # $class->_cli:
94 # The command line interface: the two verbs, their arguments,
95 # and their options (WORDS-PROGRAM-2).
96 #
97 # The epilogue names the path of the shipped word list, because
98 # both verbs take a list file and a person needs that path.
99 13 ▶sub _cli ($class)
Subroutine _cli: called CC=2 SCAR=10.3
100 0 ▶{
Subroutine _cli: not called CC=2 SCAR=10.3
101 0 my $list = $class->share('english.txt');
102  
103 0 ▶ return Fugu::CLI->new(
Subroutine __ANON__: not called CC=1 SCAR=1.2
104 name => NAME,
105 usage => '<command> [arguments]',
106 epilogue => defined $list
107 ? "The shipped word list is $list\n"
108 : undef,
109 commands => {
110 build => {
111 usage => '<list> [--date YYYY-MM-DD]',
112 summary => 'write the word sheet to '
113 . 'standard output',
114 options => { 'date=s' => 1 },
115 4 ▶ run => sub ( $cli, @argument ) {
Subroutine __ANON__: called CC=1 SCAR=1.2
116 0 return $class->_build( $cli,
117 @argument );
118 },
119 },
120 0 ▶ check => {
Subroutine __ANON__: not called CC=1 SCAR=1.2
121 usage => '<sheet> <list>',
122 summary => 'prove that a word sheet is '
123 . 'correct for a list',
124 4 ▶ run => sub ( $cli, @argument ) {
Subroutine __ANON__: called CC=1 SCAR=1.2
125 0 return $class->_check( $cli,
126 @argument );
127 },
128 },
129 0 ▶ } );
Branch: 2 branches
BranchTrueFalseTotal
defined $list ? : 13 0 13
defined $list ? : 0 0 0
130 }
131  
132 # $class->_build($cli, @argument):
133 # The build verb: write the sheet of one list to standard
134 # output (WORDS-BUILD-1).
135 4 ▶sub _build ( $class, $cli, @argument )
Subroutine _build: called CC=7 SCAR=28.4
136 0 ▶{
Subroutine _build: not called CC=7 SCAR=28.4
137 0 ▶ return $cli->command_usage_error('build') unless @argument == 1;
Branch: 2 branches
BranchTrueFalseTotal
unless @argument == 1 1 3 4
unless @argument == 1 0 0 0
138  
139 0 ▶ my $date = $cli->option('date') // _today();
Condition16%1/6
$cli->option("date") // _today()
l!l&&r!l&&!r
300
$cli->option("date") // _today()
l!l&&r!l&&!r
000
MC/DC0%0/2
$cli->option("date") // _today()
$cli->option("date") _today()
MC/DC0%0/2
$cli->option("date") // _today()
$cli->option("date") _today()
Truth table33%1/3
A // B
A= $cli->option("date")B= _today()
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $cli->option("date")B= _today()
ABresult
1X1
011
000
140 0 ▶ unless ( $date =~ /\A[0-9]{4}-[0-9]{2}-[0-9]{2}\z/ ) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($date =~ /\A[0-9]{4}-[0-9]{2}-[0-9]{2}\z/u) 1 2 3
unless ($date =~ /\A[0-9]{4}-[0-9]{2}-[0-9]{2}\z/u) 0 0 0
141 0 $cli->log->error( '%s: the date must read YYYY-MM-DD', NAME );
142 0 return $cli->command_usage_error('build');
143 }
144  
145 0 my $list = App::FuguSeed::ListFile->read( $argument[0] );
146 0 ▶ return FAILURE unless defined $list;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $list 1 1 2
unless defined $list 0 0 0
147  
148 0 my $template = $class->_share_text( $cli, 'sheet.html' );
149 0 my $style = $class->_share_text( $cli, 'sheet.css' );
150 0 ▶ return FAILURE unless defined $template && defined $style;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $template and defined $style 0 1 1
unless defined $template and defined $style 0 0 0
Condition16%1/6
defined $template and defined $style
!ll&&!rl&&r
001
defined $template and defined $style
!ll&&!rl&&r
000
MC/DC0%0/2
defined $template and defined $style
defined $template defined $style
MC/DC0%0/2
defined $template and defined $style
defined $template defined $style
Truth table33%1/3
A and B
A= defined $templateB= defined $style
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $templateB= defined $style
ABresult
0X0
100
111
151  
152 # A class name after print is a bareword filehandle on perl
153 # v5.34, so the sheet reaches a variable first.
154 0 my $sheet = App::FuguSeed::Sheet->build(
155 list => $list,
156 template => $template,
157 style => $style,
158 date => $date
159 );
160 0 print $sheet;
161  
162 0 return SUCCESS;
163 }
164  
165 # $class->_check($cli, @argument):
166 # The check verb: prove one sheet against one list. The verb is
167 # silent on success, and it prints one line for each defect on
168 # standard error (WORDS-CHECK-1, WORDS-CHECK-2).
169 4 ▶sub _check ( $class, $cli, @argument )
Subroutine _check: called CC=7 SCAR=26.9
170 0 ▶{
Subroutine _check: not called CC=7 SCAR=26.9
171 0 ▶ return $cli->command_usage_error('check') unless @argument == 2;
Branch: 2 branches
BranchTrueFalseTotal
unless @argument == 2 1 3 4
unless @argument == 2 0 0 0
172  
173 0 my ( $path, $list_path ) = @argument;
174 0 my $sheet = Fugu::File->read($path);
175 0 ▶ unless ( defined $sheet ) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $sheet) 1 2 3
unless (defined $sheet) 0 0 0
176 0 $cli->log->error( '%s: cannot read the sheet %s', NAME, $path );
177 0 return FAILURE;
178 }
179  
180 0 my $list = App::FuguSeed::ListFile->read($list_path);
181 0 ▶ return FAILURE unless defined $list;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $list 0 2 2
unless defined $list 0 0 0
182  
183 0 my $style = $class->_share_text( $cli, 'sheet.css' );
184 0 ▶ return FAILURE unless defined $style;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $style 0 2 2
unless defined $style 0 0 0
185  
186 0 my @defects = App::FuguSeed::Check->defects( $sheet, $list, $style );
187 0 ▶ return SUCCESS unless @defects;
Branch: 2 branches
BranchTrueFalseTotal
unless @defects 1 1 2
unless @defects 0 0 0
188  
189 # A defect names a byte offset or a position, and it can hold
190 # a percent sign, so the message is an argument of the format.
191 0 $cli->log->error( '%s', $_ ) for @defects;
192  
193 0 return FAILURE;
194 }
195  
196 # $class->_share_text($cli, $name):
197 # The text of the share file $name, or undef with one
198 # diagnostic (LIST-SHARE-2).
199 4 ▶sub _share_text ( $class, $cli, $name )
Subroutine _share_text: called CC=3 SCAR=17.7
200 0 ▶{
Subroutine _share_text: not called CC=3 SCAR=17.7
201 0 my $path = $class->share($name);
202 0 ▶ unless ( defined $path ) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $path) 0 4 4
unless (defined $path) 0 0 0
203 0 $cli->log->error( '%s: cannot find the share file %s',
204 NAME, $name );
205 0 return;
206 }
207  
208 0 my $text = Fugu::File->read($path);
209 0 ▶ $cli->log->error( '%s: cannot read the share file %s', NAME, $path )
Branch: 2 branches
BranchTrueFalseTotal
unless defined $text 0 4 4
unless defined $text 0 0 0
210 unless defined $text;
211  
212 0 return $text;
213 }
214  
215 # _today():
216 # The date of this moment, in UTC, as YYYY-MM-DD.
217 0 ▶sub _today ()
Subroutine _today: not called CC=1 SCAR=6.9
218 {
219 0 my @now = gmtime time;
220  
221 0 return sprintf '%04d-%02d-%02d', $now[5] + 1900, $now[4] + 1, $now[3];
222 }
223  
224 14 1;
« lib/App/FuguSeed/Last.pm ↑ Summary lib/App/FuguSeed/Matrix.pm »