1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
use std::{fs, path::PathBuf};
use topos_bible_index::files::{self, WalkOptions};
use crate::complete;
use clap::{Parser, ValueEnum, ValueHint};
use clap_complete::engine::ArgValueCompleter;
use topos_bible::{
data::{
bible_data::{BibleData, BibleDataInput},
patch::DataPatch,
},
error::AnyResult,
filter::{
bible_filter::BibleFilter,
filters::{book::BookFilter, genre::GenreFilter, testament::TestamentFilter},
},
matcher::{BibleMatcher, context::BookContext},
segments::formatter::{BookStyle, FormatOptions},
};
use topos_bible_formats::epub::{CfiOptions, LinkStyle};
/**
Find Bible references in files, directories, text, or stdin.
- Including a testament limits the search to it: `--nt -g Gospels` is the four Gospels
- Included genres and books add up: `-g Pentateuch -b Revelation` is six books
- Exclusions always win, so a book can be excluded from an included genre
- Passage filters that keep references (`-i`, `--any-overlap`, `-o`, `--exact-overlap`) are joined
with a logical OR, then `--exclude-overlap` removes references
- A passage filter can name a book alone for the whole book: `-i mat` keeps every reference in
Matthew
- Whole chapters alone (`John 3`) are left out unless `--chapters`, or unless every passage
filter names only chapters or books (`-i "John 3"`); `--translation` narrows what the other
filters keep
*/
#[derive(Parser, Debug)]
#[command(
name = "topos",
version,
about = "Find Bible references, like ripgrep",
verbatim_doc_comment,
// Options from the config file come first, so the command line overrides them
args_override_self = true
)]
pub struct Args {
/// Files or directories to search (respecting .gitignore); defaults to stdin when piped,
/// otherwise the current directory
#[arg(value_hint = ValueHint::AnyPath)]
pub paths: Vec<PathBuf>,
/// Search this text instead of files
#[arg(long, conflicts_with = "paths")]
pub text: Option<String>,
/// Include books from a testament (old/new)
#[arg(long = "testament", short = 't', add = ArgValueCompleter::new(complete::testaments))]
pub testaments: Vec<TestamentFilter>,
/// Include the New Testament (same as `-t new`)
#[arg(long)]
pub nt: bool,
/// Include the Old Testament (same as `-t old`)
#[arg(long)]
pub ot: bool,
/// Exclude books from a testament
#[arg(long = "exclude-testament", add = ArgValueCompleter::new(complete::testaments))]
pub exclude_testaments: Vec<TestamentFilter>,
/// Include books of a genre (e.g. epistles, gospels)
#[arg(long = "genre", short = 'g', add = ArgValueCompleter::new(complete::genres))]
pub genres: Vec<String>,
/// Exclude books of a genre
#[arg(long = "exclude-genre", add = ArgValueCompleter::new(complete::genres))]
pub exclude_genres: Vec<String>,
/// Include a book (e.g. John)
#[arg(long = "book", short = 'b', add = ArgValueCompleter::new(complete::books))]
pub books: Vec<String>,
/// Exclude a book
#[arg(long = "exclude-book", add = ArgValueCompleter::new(complete::books))]
pub exclude_books: Vec<String>,
/// Only keep references entirely inside this passage (e.g. "John 1" keeps John 1:2-3)
#[arg(long = "inside", short = 'i', add = ArgValueCompleter::new(complete::passages))]
pub inside: Vec<String>,
/// Only keep references that share any verse with this passage, whole chapters included
/// (e.g. "John 3:16" keeps John 3 and John 3:14-18)
#[arg(
long = "any-overlap",
alias = "overlaps",
add = ArgValueCompleter::new(complete::passages)
)]
pub any_overlap: Vec<String>,
/// Only keep references that name a verse of this passage: whole chapters don't count
/// (e.g. "John 3:16" keeps John 3:14-18 and John 2; 3:16, but not John 3)
#[arg(
long = "explicit-overlap",
short = 'o',
add = ArgValueCompleter::new(complete::passages)
)]
pub explicit_overlap: Vec<String>,
/// Only keep references that are exactly this passage, however they are written (e.g.
/// "John 3:16-18" keeps Jn 3:16-18 and John 3:16, 17-18, but not John 3:16-17)
#[arg(long = "exact-overlap", add = ArgValueCompleter::new(complete::passages))]
pub exact_overlap: Vec<String>,
/// Drop references that share any verse with this passage
#[arg(
long = "exclude-overlap",
alias = "outside",
add = ArgValueCompleter::new(complete::passages)
)]
pub exclude_overlap: Vec<String>,
/// Also keep references that name no verse, only whole chapters (e.g. John 3 or John 2-4);
/// without it, only references that name a verse are kept (John 3:16, John 2; 3:16), unless
/// every passage filter names only chapters or books (-i "John 3", -i mat)
#[arg(long)]
pub chapters: bool,
/// Also drop references in single-chapter books, whose numbers are verses without a chapter
/// (e.g. Jude 25 and Obadiah 3)
#[arg(long, conflicts_with = "chapters")]
pub skip_single_chapter: bool,
/// Only keep references followed by this translation (e.g. ESV keeps "John 3:16 ESV" and
/// "John 3:16 (ESV)"); several keep any of them. Searches again instead of using --cache
#[arg(long = "translation", value_name = "NAME")]
pub translations: Vec<String>,
/// Treat the input as being about this book, so references like 3:16 match
#[arg(long, add = ArgValueCompleter::new(complete::books))]
pub context_book: Option<String>,
/// Lines matching this pattern set the book for references after them, like '^#+ {book}$'
#[arg(long, conflicts_with = "context_book")]
pub context_heading: Option<String>,
/// A JSON file with custom books, genres, or chapter and verse counts
#[arg(long, value_hint = ValueHint::FilePath)]
pub data: Option<PathBuf>,
/// A JSON file (like --data, every field optional) whose names and values are added to the
/// data, keeping the defaults: new abbreviations, books, genres, or chapter counts
#[arg(long, value_name = "PATH", value_hint = ValueHint::FilePath)]
pub merge_data: Vec<PathBuf>,
/// A JSON file (like --data, every field optional) whose values are removed from the data: a
/// book or genre listed alone is removed entirely, otherwise just the values listed
#[arg(long, value_name = "PATH", value_hint = ValueHint::FilePath)]
pub remove_data: Vec<PathBuf>,
/// Use a named query from ~/.config/topos/queries.toml (its options go where this is)
#[arg(long, short = 'q', value_name = "NAME", add = ArgValueCompleter::new(complete::queries))]
pub query: Vec<String>,
/// List the named queries and exit
#[arg(long)]
pub list_queries: bool,
/// Read default options from this file instead of ~/.config/topos/config.toml
#[arg(long, value_name = "PATH", conflicts_with = "no_config", value_hint = ValueHint::FilePath)]
pub config: Option<PathBuf>,
/// Do not read default options from a config file
#[arg(long)]
pub no_config: bool,
/// Print only the total number of matches across all files (same as `-m total-count`)
#[arg(long)]
pub total_count: bool,
/// How to print results
#[arg(long, short = 'm', value_enum, default_value_t)]
pub mode: OutputMode,
/// How to write each reference's book (also used by --complete); overrides --psg-fmt's
/// `book` [default: name]
#[arg(long, short = 'f', value_enum)]
pub format: Option<ReferenceFormat>,
/// How to write references (results and completions), as JSON with any of: book,
/// book_separator, chapter_verse, range, verse_separator, chapter_separator,
/// omit_first_verse_of_chapter_range, join_adjacent, chapter_in_single_chapter_books. In
/// config.toml it is a table: psg-fmt = { join_adjacent = true }
#[arg(long, value_name = "JSON", value_parser = parse_format)]
pub psg_fmt: Option<FormatOptions>,
/// Between the book and its chapters [default: " "]
#[arg(long, value_name = "TEXT")]
pub fmt_book_separator: Option<String>,
/// Between a chapter and a verse [default: ":"]
#[arg(long, value_name = "TEXT")]
pub fmt_chapter_verse: Option<String>,
/// Between the ends of a range [default: "-"]
#[arg(long, value_name = "TEXT")]
pub fmt_range: Option<String>,
/// Before another verse in the same chapter [default: ","]
#[arg(long, value_name = "TEXT")]
pub fmt_verse_separator: Option<String>,
/// Before a part in another chapter [default: "; "]
#[arg(long, value_name = "TEXT")]
pub fmt_chapter_separator: Option<String>,
/// Write adjacent verses as a range: 3:16-18 instead of 3:16,17,18 [default: false]
#[arg(long, value_name = "BOOL", num_args = 0..=1, default_missing_value = "true")]
pub fmt_join_adjacent: Option<bool>,
/// Write a range from a chapter's first verse as 1-2:3 instead of 1:1-2:3 [default: false]
#[arg(long, value_name = "BOOL", num_args = 0..=1, default_missing_value = "true")]
pub fmt_omit_first_verse_of_chapter_range: Option<bool>,
/// Write the chapter in single-chapter books: Jude 1:5 instead of Jude 5 [default: true]
#[arg(long, value_name = "BOOL", num_args = 0..=1, default_missing_value = "true")]
pub fmt_chapter_in_single_chapter_books: Option<bool>,
/// Print an EPUB++ link to each reference instead (`[[Book.epub#epubcfi(...)|John 3:16]]`, or
/// with markdown, `[John 3:16](Book.epub#epubcfi%28...%29)`), labeled with the reference as
/// written by -f and --psg-fmt. Only EPUBs are searched, and the path in each link is relative
/// to the directory searched (or just the file's name, for a file named on the command line).
/// With -m json, each object gets a "link" field instead; -m count, -m total-count, -l, and
/// --files work as usual
#[arg(long, value_enum, value_name = "STYLE", conflicts_with = "text")]
pub epub_links: Option<EpubLinks>,
/// Write EPUB CFIs with `[id]` assertions, like /6/14[chapter-1]!/4/2[p3]/1:0: more robust
/// if the book changes, but `[` and `]` break wikilinks (EPUB++ leaves them out by default)
#[arg(long)]
pub cfi_assertions: bool,
/// Lines of context to show after each match (prints the matched line too, grouped by file;
/// for EPUBs, a line is a paragraph, and for PDFs, a line of a page)
#[arg(long, short = 'A')]
pub after_context: Option<usize>,
/// Lines of context to show before each match (prints the matched line too, as -A does)
#[arg(long, short = 'B')]
pub before_context: Option<usize>,
/// Lines of context to show before and after each match; -C 0 shows only the matched line
#[arg(long, short = 'C')]
pub context: Option<usize>,
/// When to use colors
#[arg(long, value_enum, default_value_t)]
pub color: ColorChoice,
/// Print results sorted by path (waits for the whole search)
#[arg(long)]
pub sort: bool,
/// Reuse results for files that have not changed (stored unfiltered, so any filters can use them)
#[arg(long, overrides_with = "no_cache")]
pub cache: bool,
/// Don't use the cache, even if the config turns it on
#[arg(long, overrides_with = "cache")]
pub no_cache: bool,
/// Print the completions for a partly typed reference, one per line, and exit: books for ""
/// (or nothing), then chapters, verses, and range ends ("John 3:" gives John 3:1, ...).
/// Uses -f for the book style and the book filters; -m json prints objects
#[arg(
long,
value_name = "TEXT",
num_args = 0..=1,
default_missing_value = "",
add = ArgValueCompleter::new(complete::passages)
)]
pub complete: Option<String>,
/// Whether --complete offers the chapter or verse already typed, first ("Matthew 1" gives
/// Matthew 1, then Matthew 10, ...) [default: true]
#[arg(long, value_name = "BOOL", num_args = 0..=1, default_missing_value = "true")]
pub complete_typed: Option<bool>,
/// Print every book, one per line, and exit (the same as `--complete ""`)
#[arg(long)]
pub list_books: bool,
/// Delete the cache (in ~/.cache/topos) and exit
#[arg(long)]
pub clear_cache: bool,
/// Only search files with these extensions when walking directories (e.g. md,txt); files
/// named on the command line are always searched
#[arg(long = "ext", value_name = "EXT", value_delimiter = ',')]
pub extensions: Vec<String>,
/// Don't search files with these extensions when walking directories (e.g. pdf,epub)
#[arg(long, value_name = "EXT", value_delimiter = ',')]
pub exclude_ext: Vec<String>,
/// Include or exclude paths matching a glob (`!` excludes), like `--glob '*.md' --glob
/// '!drafts/**'`; can be repeated. Like ripgrep, globs override ignore files and --hidden
#[arg(long, value_name = "GLOB")]
pub glob: Vec<String>,
/// Like --glob, ignoring case (when a path matches both, the --iglob wins)
#[arg(long, value_name = "GLOB")]
pub iglob: Vec<String>,
/// Only search files whose path matches a regular expression anywhere (`!` excludes), like
/// `--path-regex '20\d\d/' --path-regex '!(?i)drafts'`; paths use `/` and have no leading
/// `./`. Can be repeated: a file must match one, and none that exclude. Files named on the
/// command line are always searched
#[arg(long, value_name = "REGEX")]
pub path_regex: Vec<String>,
/// Search hidden files and directories (names starting with `.`)
#[arg(long, short = '.')]
pub hidden: bool,
/// Don't respect ignore files (.gitignore, .ignore, .toposignore, and Git's global and
/// exclude files)
#[arg(long)]
pub no_ignore: bool,
/// Don't respect Git's ignore files (.gitignore, the global gitignore, .git/info/exclude)
#[arg(long)]
pub no_ignore_vcs: bool,
/// Don't respect ignore files in parent directories
#[arg(long)]
pub no_ignore_parent: bool,
/// Respect .gitignore files outside of Git repositories too
#[arg(long)]
pub no_require_git: bool,
/// Also ignore the paths in this file (gitignore syntax); can be repeated
#[arg(long, value_name = "PATH", value_hint = ValueHint::FilePath)]
pub ignore_file: Vec<PathBuf>,
/// Search more: -u is --no-ignore, -uu adds --hidden, -uuu adds --binary
#[arg(long, short = 'u', action = clap::ArgAction::Count)]
pub unrestricted: u8,
/// Search files that look binary as text (normally they are skipped)
#[arg(long)]
pub binary: bool,
/// Follow symbolic links
#[arg(long, short = 'L')]
pub follow: bool,
/// Descend at most this many directories below the paths given (0 searches only them)
#[arg(long, short = 'd', value_name = "NUM")]
pub max_depth: Option<usize>,
/// Skip files larger than this, like 500K, 10M, or 1G
#[arg(long, value_name = "SIZE", value_parser = parse_size)]
pub max_filesize: Option<u64>,
/// Don't cross into other file systems (like mounted drives)
#[arg(long)]
pub one_file_system: bool,
/// Print the files that would be searched, without searching them
#[arg(long)]
pub files: bool,
/// Print only the paths of files with references
#[arg(long, short = 'l', conflicts_with = "files_without_match")]
pub files_with_matches: bool,
/// Print only the paths of searched files without references
#[arg(long)]
pub files_without_match: bool,
}
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, ValueEnum)]
pub enum OutputMode {
/// Grouped by file on a terminal or with context (-A, -B, -C), otherwise
/// `path:line:column: reference`
#[default]
Auto,
/// Grouped by file, with context lines with -A, -B, or -C
Grouped,
/// `path:line:column: reference` (for Vim's quickfix list)
#[value(alias = "qf", alias = "vimgrep")]
Quickfix,
/// A Markdown table
#[value(alias = "t")]
Table,
/// One JSON object per match
#[value(alias = "j")]
Json,
/// Matches per file
#[value(alias = "c")]
Count,
/// Matches across all files
TotalCount,
/// One line per searched file, for apps that keep an index (the Obsidian plugin):
/// `{"path": ..., "entry": ...}`, with the file's references as a topos-bible-index entry
/// (base64; UTF-16 offsets)
Index,
}
/// How `--epub-links` writes links
#[derive(Copy, Clone, Debug, PartialEq, Eq, ValueEnum)]
pub enum EpubLinks {
/// `[[Book.epub#epubcfi(...)|John 3:16]]`
Wiki,
/// `[John 3:16](Book.epub#epubcfi%28...%29)`
#[value(alias = "md")]
Markdown,
}
impl From<EpubLinks> for LinkStyle {
fn from(links: EpubLinks) -> Self {
match links {
EpubLinks::Wiki => LinkStyle::Wiki,
EpubLinks::Markdown => LinkStyle::Markdown,
}
}
}
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, ValueEnum)]
pub enum ReferenceFormat {
/// `Genesis 1:1`
#[default]
Name,
/// `Gn 1:1`
#[value(alias = "abbrev")]
Abbreviation,
/// `Gen.1.1`
Osis,
/// `GEN 1:1`
Usfm,
}
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, ValueEnum)]
pub enum ColorChoice {
#[default]
Auto,
Always,
Never,
}
/// `--psg-fmt`: format options as JSON, every field optional
fn parse_format(json: &str) -> Result<FormatOptions, String> {
serde_json::from_str(json).map_err(|e| format!("{e} (see --help for the fields)"))
}
/// `500`, `500K`, `10M`, or `1G` (powers of 1024)
fn parse_size(text: &str) -> Result<u64, String> {
let text = text.trim();
let invalid = || format!("`{text}` isn't a size like 500K, 10M, or 1G");
let split = text
.find(|c: char| c.is_ascii_alphabetic())
.unwrap_or(text.len());
let (number, unit) = text.split_at(split);
let number: u64 = number.trim().parse().map_err(|_| invalid())?;
let scale: u64 = match unit.to_ascii_uppercase().as_str() {
"" | "B" => 1,
"K" | "KB" => 1 << 10,
"M" | "MB" => 1 << 20,
"G" | "GB" => 1 << 30,
_ => return Err(invalid()),
};
Ok(number.saturating_mul(scale))
}
impl Args {
/// How directories are walked, with -u applied
pub fn walk_options(&self) -> Result<WalkOptions, String> {
let u = self.unrestricted;
Ok(WalkOptions {
hidden: self.hidden || u >= 2,
no_ignore: self.no_ignore || u >= 1,
no_ignore_vcs: self.no_ignore_vcs,
no_ignore_parent: self.no_ignore_parent,
no_require_git: self.no_require_git,
ignore_files: self.ignore_file.clone(),
follow: self.follow,
max_depth: self.max_depth,
max_filesize: self.max_filesize,
one_file_system: self.one_file_system,
globs: self.glob.clone(),
iglobs: self.iglob.clone(),
path_regexes: self.path_regex.clone(),
extensions: WalkOptions::extension_list(&self.extensions),
exclude_extensions: WalkOptions::extension_list(&self.exclude_ext),
only_epub: self.epub_links.is_some(),
})
}
/// How EPUB CFIs are written
pub fn cfi_options(&self) -> CfiOptions {
CfiOptions {
assertions: self.cfi_assertions,
}
}
/// Whether files that look binary are searched (`--binary` or `-uuu`)
pub fn search_binary(&self) -> bool {
self.binary || self.unrestricted >= 3
}
/// Every option that changes which references are found before filtering (for the cache,
/// which stores unfiltered results, so filters are left out)
pub fn fingerprint(&self) -> String {
let contents = |paths: &[PathBuf]| -> Vec<Option<String>> {
paths
.iter()
.map(|path| fs::read_to_string(path).ok())
.collect()
};
let data = contents(self.data.as_slice());
let merge = contents(&self.merge_data);
let remove = contents(&self.remove_data);
let config = format!(
"{:?}",
(
&self.context_book,
&self.context_heading,
data,
merge,
remove
)
);
// Binary files are cached as having no references unless they are searched, and EPUB
// results are stored with their CFIs
files::fingerprint(&config, self.search_binary(), self.cfi_options())
}
/// Lines before and after each match, when any of -A, -B, or -C is given (even 0, which shows
/// the matched line alone)
pub fn context_lines(&self) -> Option<(usize, usize)> {
let (before, after, both) = (self.before_context, self.after_context, self.context);
if before.is_none() && after.is_none() && both.is_none() {
return None;
}
let both = both.unwrap_or(0);
Some((both.max(before.unwrap_or(0)), both.max(after.unwrap_or(0))))
}
/// How references are written: the defaults, then --psg-fmt, then each --fmt-* option, then -f
/// (OSIS references are written by [`Passage::to_osis`] instead)
pub fn format_options(&self) -> FormatOptions {
let mut options = self.psg_fmt.clone().unwrap_or_default();
let text = |value: &Option<String>, field: &mut String| {
if let Some(value) = value {
*field = value.clone();
}
};
text(&self.fmt_book_separator, &mut options.book_separator);
text(&self.fmt_chapter_verse, &mut options.chapter_verse);
text(&self.fmt_range, &mut options.range);
text(&self.fmt_verse_separator, &mut options.verse_separator);
text(&self.fmt_chapter_separator, &mut options.chapter_separator);
if let Some(join) = self.fmt_join_adjacent {
options.join_adjacent = join;
}
if let Some(omit) = self.fmt_omit_first_verse_of_chapter_range {
options.omit_first_verse_of_chapter_range = omit;
}
if let Some(chapter) = self.fmt_chapter_in_single_chapter_books {
options.chapter_in_single_chapter_books = chapter;
}
if let Some(format) = self.format {
options.book = match format {
ReferenceFormat::Name => BookStyle::Name,
ReferenceFormat::Abbreviation => BookStyle::Abbreviation,
ReferenceFormat::Osis => BookStyle::Osis,
ReferenceFormat::Usfm => BookStyle::Usfm,
};
}
options
}
/// `--data` (or the defaults), then each `--merge-data`, then each `--remove-data`
fn data(&self) -> AnyResult<BibleData> {
fn read<T: serde::de::DeserializeOwned>(path: &PathBuf) -> AnyResult<T> {
let text = fs::read_to_string(path).map_err(|e| format!("{}: {e}", path.display()))?;
Ok(serde_json::from_str(&text).map_err(|e| format!("{}: {e}", path.display()))?)
}
if self.data.is_none() && self.merge_data.is_empty() && self.remove_data.is_empty() {
return Ok(BibleData::default());
}
let mut input = match &self.data {
Some(path) => read::<BibleDataInput>(path)?,
None => BibleDataInput::defaults(),
};
for path in &self.merge_data {
input
.merge(read::<DataPatch>(path)?)
.map_err(|e| format!("{}: {e}", path.display()))?;
}
for path in &self.remove_data {
input
.remove(read::<DataPatch>(path)?)
.map_err(|e| format!("{}: {e}", path.display()))?;
}
Ok(BibleData::new(input)?)
}
pub fn matcher(&self) -> AnyResult<BibleMatcher> {
let data = self.data()?;
let mut filter = BibleFilter::new(data);
filter.include_many(self.testaments.iter().copied())?;
if self.nt {
filter.include(TestamentFilter::New)?;
}
if self.ot {
filter.include(TestamentFilter::Old)?;
}
filter.include_many(self.genres.iter().map(GenreFilter::new))?;
filter.include_many(self.books.iter().map(BookFilter::new))?;
filter.exclude_many(self.exclude_testaments.iter().copied())?;
filter.exclude_many(self.exclude_genres.iter().map(GenreFilter::new))?;
filter.exclude_many(self.exclude_books.iter().map(BookFilter::new))?;
for passage in &self.inside {
filter.filter_inside(passage)?;
}
for passage in &self.any_overlap {
filter.filter_any_overlap(passage)?;
}
for passage in &self.explicit_overlap {
filter.filter_explicit_overlap(passage)?;
}
for passage in &self.exact_overlap {
filter.filter_exact_overlap(passage)?;
}
for passage in &self.exclude_overlap {
filter.filter_exclude_overlap(passage)?;
}
// Completions and the book list suggest chapters too, and passages that name no verse
// (`-i "John 3"`) keep them
if !self.chapters
&& self.complete.is_none()
&& !self.list_books
&& !filter.includes_only_chapters()
{
filter.filter_verses(!self.skip_single_chapter);
}
for name in &self.translations {
filter.filter_translation(name);
}
if let Some(reason) = filter.contradiction() {
eprintln!("topos: warning: {reason}, so nothing can match");
}
let matcher = filter.create_matcher();
let books = matcher.data().books();
let context = match (&self.context_book, &self.context_heading) {
(Some(book), _) => Some(BookContext::Book(
books
.search(book)
.ok_or_else(|| format!("unknown book {book:?}"))?,
)),
(None, Some(pattern)) => Some(BookContext::headings(books, pattern)?),
(None, None) => None,
};
Ok(match context {
Some(context) => matcher.with_context(context),
None => matcher,
})
}
}