rsword_chirho 0.3.0

Core SWORD module library in pure Rust
Documentation
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
// For God so loved the world that he gave his only begotten Son,
// that whoever believes in him should not perish but have eternal life.
// John 3:16

//! Cross-versification mapping between different Bible versification systems.
//!
//! Different Bible translations use different verse numbering systems. This module
//! provides functionality to map verse references between versification systems.
//!
//! # Mapping Types
//!
//! Verses can map in several ways between versification systems:
//!
//! | Type | Description | Example |
//! |------|-------------|---------|
//! | One-to-one | Direct correspondence | Ps 23:1 (KJV) = Ps 23:1 (NRSV) |
//! | Split | One verse becomes multiple | Ps 13:5 (Hebrew) = Ps 13:5-6 (English) |
//! | Merge | Multiple verses become one | - |
//! | Missing | Verse doesn't exist in target | 3 John 1:15 (some systems) |
//! | Offset | Chapter/verse numbers shifted | Psalm titles |
//!
//! # Usage
//!
//! ```rust,ignore
//! use rsword_chirho::versification_chirho::mapping_chirho::{VerseMappingChirho, VerseMappingResultChirho};
//! use rsword_chirho::versification_chirho::{kjv_chirho, catholic_chirho};
//!
//! let mapper_chirho = VerseMappingChirho::new_chirho();
//!
//! // Map a verse from KJV to Catholic versification
//! let result_chirho = mapper_chirho.map_verse_chirho(
//!     "Psalm", 13, 5,
//!     "KJV",
//!     "Catholic",
//! );
//!
//! match result_chirho {
//!     VerseMappingResultChirho::DirectChirho { book_chirho, chapter_chirho, verse_chirho } => {
//!         println!("Maps to {}:{}:{}", book_chirho, chapter_chirho, verse_chirho);
//!     }
//!     VerseMappingResultChirho::SplitChirho { verses_chirho } => {
//!         println!("Split into {} verses", verses_chirho.len());
//!     }
//!     VerseMappingResultChirho::NotFoundChirho => {
//!         println!("No mapping found");
//!     }
//! }
//! ```

use std::collections::HashMap;
use std::sync::LazyLock;

/// Result of mapping a verse between versification systems.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VerseMappingResultChirho {
    /// Direct one-to-one mapping.
    DirectChirho {
        /// Book name in target versification.
        book_chirho: String,
        /// Chapter number in target versification.
        chapter_chirho: u32,
        /// Verse number in target versification.
        verse_chirho: u32,
    },
    /// Verse splits into multiple verses in target.
    SplitChirho {
        /// List of (book, chapter, verse) tuples in target.
        verses_chirho: Vec<(String, u32, u32)>,
    },
    /// Multiple verses merge into one in target.
    MergedChirho {
        /// The merged verse reference.
        book_chirho: String,
        /// Chapter number.
        chapter_chirho: u32,
        /// Verse range (start, end).
        verse_range_chirho: (u32, u32),
    },
    /// Verse doesn't exist in target versification.
    NotFoundChirho,
}

/// A single verse mapping rule.
#[derive(Debug, Clone)]
#[allow(dead_code)]
struct MappingRuleChirho {
    /// Source book name (stored for debugging and future bidirectional mapping).
    source_book_chirho: String,
    /// Source chapter.
    source_chapter_chirho: u32,
    /// Source verse or verse range.
    source_verse_chirho: (u32, u32),
    /// Target book name (may differ).
    target_book_chirho: String,
    /// Target chapter.
    target_chapter_chirho: u32,
    /// Target verse or verse range.
    target_verse_chirho: (u32, u32),
}

/// Cross-versification mapper.
///
/// Provides verse mapping between different versification systems.
#[derive(Debug, Clone)]
pub struct VerseMappingChirho {
    /// Mapping rules indexed by source versification.
    rules_chirho: HashMap<String, HashMap<String, Vec<MappingRuleChirho>>>,
}

impl VerseMappingChirho {
    /// Create a new verse mapper with default mapping rules.
    pub fn new_chirho() -> Self {
        Self {
            rules_chirho: build_default_mappings_chirho(),
        }
    }

    /// Map a verse from one versification to another.
    ///
    /// # Arguments
    ///
    /// * `book_chirho` - Book name (e.g., "Psalm", "Genesis")
    /// * `chapter_chirho` - Chapter number
    /// * `verse_chirho` - Verse number
    /// * `from_v11n_chirho` - Source versification name (e.g., "KJV")
    /// * `to_v11n_chirho` - Target versification name (e.g., "Catholic")
    ///
    /// # Returns
    ///
    /// The mapping result indicating how the verse maps to the target system.
    pub fn map_verse_chirho(
        &self,
        book_chirho: &str,
        chapter_chirho: u32,
        verse_chirho: u32,
        from_v11n_chirho: &str,
        to_v11n_chirho: &str,
    ) -> VerseMappingResultChirho {
        // Same versification - direct mapping
        if from_v11n_chirho.eq_ignore_ascii_case(to_v11n_chirho) {
            return VerseMappingResultChirho::DirectChirho {
                book_chirho: book_chirho.to_string(),
                chapter_chirho,
                verse_chirho,
            };
        }

        // Look up mapping key
        let key_chirho = format!("{}->{}", from_v11n_chirho.to_uppercase(), to_v11n_chirho.to_uppercase());

        if let Some(book_rules_chirho) = self.rules_chirho.get(&key_chirho) {
            let book_upper_chirho = book_chirho.to_uppercase();
            if let Some(rules_chirho) = book_rules_chirho.get(&book_upper_chirho) {
                for rule_chirho in rules_chirho {
                    if rule_chirho.source_chapter_chirho == chapter_chirho
                        && verse_chirho >= rule_chirho.source_verse_chirho.0
                        && verse_chirho <= rule_chirho.source_verse_chirho.1
                    {
                        return apply_rule_chirho(rule_chirho, verse_chirho);
                    }
                }
            }
        }

        // No specific mapping - assume direct mapping if both versifications
        // have the verse (this is a simplification)
        VerseMappingResultChirho::DirectChirho {
            book_chirho: book_chirho.to_string(),
            chapter_chirho,
            verse_chirho,
        }
    }

    /// Map a verse range from one versification to another.
    pub fn map_range_chirho(
        &self,
        book_chirho: &str,
        chapter_chirho: u32,
        start_verse_chirho: u32,
        end_verse_chirho: u32,
        from_v11n_chirho: &str,
        to_v11n_chirho: &str,
    ) -> Vec<VerseMappingResultChirho> {
        let mut results_chirho = Vec::new();
        for verse_chirho in start_verse_chirho..=end_verse_chirho {
            results_chirho.push(self.map_verse_chirho(
                book_chirho,
                chapter_chirho,
                verse_chirho,
                from_v11n_chirho,
                to_v11n_chirho,
            ));
        }
        results_chirho
    }

    /// Check if a mapping exists between two versification systems.
    pub fn has_mapping_chirho(&self, from_v11n_chirho: &str, to_v11n_chirho: &str) -> bool {
        let key_chirho = format!("{}->{}", from_v11n_chirho.to_uppercase(), to_v11n_chirho.to_uppercase());
        self.rules_chirho.contains_key(&key_chirho)
    }

    /// List available versification mapping pairs.
    pub fn available_mappings_chirho(&self) -> Vec<(&str, &str)> {
        self.rules_chirho
            .keys()
            .filter_map(|k_chirho| {
                let parts_chirho: Vec<&str> = k_chirho.split("->").collect();
                if parts_chirho.len() == 2 {
                    Some((parts_chirho[0], parts_chirho[1]))
                } else {
                    None
                }
            })
            .collect()
    }
}

impl Default for VerseMappingChirho {
    fn default() -> Self {
        Self::new_chirho()
    }
}

/// Apply a mapping rule to get the result.
fn apply_rule_chirho(rule_chirho: &MappingRuleChirho, source_verse_chirho: u32) -> VerseMappingResultChirho {
    let source_range_chirho = rule_chirho.source_verse_chirho.1 - rule_chirho.source_verse_chirho.0 + 1;
    let target_range_chirho = rule_chirho.target_verse_chirho.1 - rule_chirho.target_verse_chirho.0 + 1;

    if source_range_chirho == 1 && target_range_chirho == 1 {
        // One-to-one mapping
        VerseMappingResultChirho::DirectChirho {
            book_chirho: rule_chirho.target_book_chirho.clone(),
            chapter_chirho: rule_chirho.target_chapter_chirho,
            verse_chirho: rule_chirho.target_verse_chirho.0,
        }
    } else if source_range_chirho == 1 && target_range_chirho > 1 {
        // Split: one verse maps to multiple
        let mut verses_chirho = Vec::new();
        for v_chirho in rule_chirho.target_verse_chirho.0..=rule_chirho.target_verse_chirho.1 {
            verses_chirho.push((
                rule_chirho.target_book_chirho.clone(),
                rule_chirho.target_chapter_chirho,
                v_chirho,
            ));
        }
        VerseMappingResultChirho::SplitChirho { verses_chirho }
    } else if source_range_chirho > 1 && target_range_chirho == 1 {
        // Merge: multiple verses map to one
        VerseMappingResultChirho::MergedChirho {
            book_chirho: rule_chirho.target_book_chirho.clone(),
            chapter_chirho: rule_chirho.target_chapter_chirho,
            verse_range_chirho: (source_verse_chirho, source_verse_chirho),
        }
    } else {
        // Range to range - calculate offset
        let offset_chirho = source_verse_chirho - rule_chirho.source_verse_chirho.0;
        let target_verse_chirho = rule_chirho.target_verse_chirho.0 + offset_chirho;
        VerseMappingResultChirho::DirectChirho {
            book_chirho: rule_chirho.target_book_chirho.clone(),
            chapter_chirho: rule_chirho.target_chapter_chirho,
            verse_chirho: target_verse_chirho,
        }
    }
}

/// Build default mapping rules between common versification systems.
fn build_default_mappings_chirho() -> HashMap<String, HashMap<String, Vec<MappingRuleChirho>>> {
    let mut mappings_chirho: HashMap<String, HashMap<String, Vec<MappingRuleChirho>>> = HashMap::new();

    // KJV -> Catholic mappings (Psalm numbering differences)
    let mut kjv_to_catholic_chirho: HashMap<String, Vec<MappingRuleChirho>> = HashMap::new();

    // Psalm 9-10 in KJV = Psalm 9 in Catholic/LXX
    kjv_to_catholic_chirho.insert("PSALM".to_string(), vec![
        // Psalms 10-146 are offset by -1 in Catholic numbering
        MappingRuleChirho {
            source_book_chirho: "Psalm".to_string(),
            source_chapter_chirho: 10,
            source_verse_chirho: (1, 18),
            target_book_chirho: "Psalm".to_string(),
            target_chapter_chirho: 9,
            target_verse_chirho: (22, 39),
        },
    ]);

    mappings_chirho.insert("KJV->CATHOLIC".to_string(), kjv_to_catholic_chirho);

    // KJV -> Hebrew/Leningrad mappings
    let mut kjv_to_hebrew_chirho: HashMap<String, Vec<MappingRuleChirho>> = HashMap::new();

    // Psalm titles are verse 1 in Hebrew
    kjv_to_hebrew_chirho.insert("PSALM".to_string(), vec![
        // Psalms with titles: Hebrew verse numbers are +1
        MappingRuleChirho {
            source_book_chirho: "Psalm".to_string(),
            source_chapter_chirho: 3,
            source_verse_chirho: (1, 8),
            target_book_chirho: "Psalm".to_string(),
            target_chapter_chirho: 3,
            target_verse_chirho: (2, 9),
        },
        MappingRuleChirho {
            source_book_chirho: "Psalm".to_string(),
            source_chapter_chirho: 4,
            source_verse_chirho: (1, 8),
            target_book_chirho: "Psalm".to_string(),
            target_chapter_chirho: 4,
            target_verse_chirho: (2, 9),
        },
    ]);

    mappings_chirho.insert("KJV->LENINGRAD".to_string(), kjv_to_hebrew_chirho.clone());
    mappings_chirho.insert("KJV->MT".to_string(), kjv_to_hebrew_chirho.clone());
    mappings_chirho.insert("KJV->HEBREW".to_string(), kjv_to_hebrew_chirho);

    // Catholic -> KJV (reverse mappings)
    let mut catholic_to_kjv_chirho: HashMap<String, Vec<MappingRuleChirho>> = HashMap::new();
    catholic_to_kjv_chirho.insert("PSALM".to_string(), vec![
        MappingRuleChirho {
            source_book_chirho: "Psalm".to_string(),
            source_chapter_chirho: 9,
            source_verse_chirho: (22, 39),
            target_book_chirho: "Psalm".to_string(),
            target_chapter_chirho: 10,
            target_verse_chirho: (1, 18),
        },
    ]);

    mappings_chirho.insert("CATHOLIC->KJV".to_string(), catholic_to_kjv_chirho);

    // LXX uses same numbering as Catholic for Psalms
    mappings_chirho.insert("KJV->LXX".to_string(), mappings_chirho.get("KJV->CATHOLIC").cloned().unwrap_or_default());
    mappings_chirho.insert("LXX->KJV".to_string(), mappings_chirho.get("CATHOLIC->KJV").cloned().unwrap_or_default());

    mappings_chirho
}

/// Global verse mapper instance.
static GLOBAL_MAPPER_CHIRHO: LazyLock<VerseMappingChirho> = LazyLock::new(VerseMappingChirho::new_chirho);

/// Get the global verse mapper.
pub fn global_mapper_chirho() -> &'static VerseMappingChirho {
    &GLOBAL_MAPPER_CHIRHO
}

#[cfg(test)]
mod tests_chirho {
    use super::*;

    #[test]
    fn test_same_versification_chirho() {
        let mapper_chirho = VerseMappingChirho::new_chirho();
        let result_chirho = mapper_chirho.map_verse_chirho("Genesis", 1, 1, "KJV", "KJV");

        assert_eq!(result_chirho, VerseMappingResultChirho::DirectChirho {
            book_chirho: "Genesis".to_string(),
            chapter_chirho: 1,
            verse_chirho: 1,
        });
    }

    #[test]
    fn test_default_mapping_chirho() {
        let mapper_chirho = VerseMappingChirho::new_chirho();

        // Test unmapped verse - should return direct mapping
        let result_chirho = mapper_chirho.map_verse_chirho("Genesis", 1, 1, "KJV", "Catholic");

        match result_chirho {
            VerseMappingResultChirho::DirectChirho { book_chirho, chapter_chirho, verse_chirho } => {
                assert_eq!(book_chirho, "Genesis");
                assert_eq!(chapter_chirho, 1);
                assert_eq!(verse_chirho, 1);
            }
            _ => panic!("Expected direct mapping"),
        }
    }

    #[test]
    fn test_psalm_kjv_to_hebrew_chirho() {
        let mapper_chirho = VerseMappingChirho::new_chirho();

        // Psalm 3:1 (KJV) should map to Psalm 3:2 (Hebrew) because of title
        let result_chirho = mapper_chirho.map_verse_chirho("Psalm", 3, 1, "KJV", "Leningrad");

        match result_chirho {
            VerseMappingResultChirho::DirectChirho { chapter_chirho, verse_chirho, .. } => {
                assert_eq!(chapter_chirho, 3);
                assert_eq!(verse_chirho, 2);
            }
            _ => panic!("Expected direct mapping with offset"),
        }
    }

    #[test]
    fn test_has_mapping_chirho() {
        let mapper_chirho = VerseMappingChirho::new_chirho();

        assert!(mapper_chirho.has_mapping_chirho("KJV", "Catholic"));
        assert!(mapper_chirho.has_mapping_chirho("KJV", "LXX"));
        assert!(mapper_chirho.has_mapping_chirho("KJV", "Leningrad"));
    }

    #[test]
    fn test_map_range_chirho() {
        let mapper_chirho = VerseMappingChirho::new_chirho();

        let results_chirho = mapper_chirho.map_range_chirho("Genesis", 1, 1, 3, "KJV", "Catholic");

        assert_eq!(results_chirho.len(), 3);
    }

    #[test]
    fn test_global_mapper_chirho() {
        let mapper_chirho = global_mapper_chirho();
        assert!(mapper_chirho.has_mapping_chirho("KJV", "Catholic"));
    }
}