khanij 1.1.0

Khanij — geology and mineralogy engine for crystal structures, rock cycles, soil, and mineral properties
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
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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
//! Geologic timescale — eons, eras, periods, and epochs with absolute age
//! ranges in millions of years ago (Ma).

use serde::{Deserialize, Serialize};

/// A geologic time interval with start and end ages in Ma (millions of years ago).
///
/// # Examples
///
/// ```
/// # use khanij::*;
/// let interval = Period::Jurassic.interval();
/// assert_eq!(interval.name, "Jurassic");
/// assert!(interval.start_ma > interval.end_ma);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct TimeInterval {
    pub name: &'static str,
    pub start_ma: f64, // older boundary
    pub end_ma: f64,   // younger boundary (0.0 = present)
}

impl TimeInterval {
    /// Duration in millions of years.
    ///
    /// # Examples
    ///
    /// ```
    /// # use khanij::*;
    /// let dur = Period::Jurassic.interval().duration_ma();
    /// assert!((dur - 56.4).abs() < 0.1);
    /// ```
    #[must_use]
    pub fn duration_ma(&self) -> f64 {
        self.start_ma - self.end_ma
    }

    /// Check if an age (in Ma) falls within this interval.
    ///
    /// # Examples
    ///
    /// ```
    /// # use khanij::*;
    /// let jurassic = Period::Jurassic.interval();
    /// assert!(jurassic.contains_age(150.0));
    /// assert!(!jurassic.contains_age(50.0));
    /// ```
    #[must_use]
    pub fn contains_age(&self, age_ma: f64) -> bool {
        age_ma >= self.end_ma && age_ma < self.start_ma
    }
}

// ---------------------------------------------------------------------------
// Eons
// ---------------------------------------------------------------------------

/// Geologic eon — the largest division of geologic time.
///
/// # Examples
///
/// ```
/// # use khanij::*;
/// let interval = Eon::Phanerozoic.interval();
/// assert!(interval.contains_age(100.0));
/// assert_eq!(Eon::ALL.len(), 4);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum Eon {
    Hadean,
    Archean,
    Proterozoic,
    Phanerozoic,
}

impl Eon {
    /// Returns the time interval for this eon.
    ///
    /// # Examples
    ///
    /// ```
    /// # use khanij::*;
    /// let archean = Eon::Archean.interval();
    /// assert!((archean.start_ma - 4000.0).abs() < 0.1);
    /// ```
    #[must_use]
    pub fn interval(&self) -> TimeInterval {
        match self {
            Self::Hadean => TimeInterval {
                name: "Hadean",
                start_ma: 4600.0,
                end_ma: 4000.0,
            },
            Self::Archean => TimeInterval {
                name: "Archean",
                start_ma: 4000.0,
                end_ma: 2500.0,
            },
            Self::Proterozoic => TimeInterval {
                name: "Proterozoic",
                start_ma: 2500.0,
                end_ma: 538.8,
            },
            Self::Phanerozoic => TimeInterval {
                name: "Phanerozoic",
                start_ma: 538.8,
                end_ma: 0.0,
            },
        }
    }

    /// All eons in chronological order (oldest first).
    pub const ALL: &'static [Eon] = &[
        Self::Hadean,
        Self::Archean,
        Self::Proterozoic,
        Self::Phanerozoic,
    ];
}

// ---------------------------------------------------------------------------
// Eras
// ---------------------------------------------------------------------------

/// Geologic era — subdivision of an eon.
///
/// # Examples
///
/// ```
/// # use khanij::*;
/// let mesozoic = Era::Mesozoic.interval();
/// assert!(mesozoic.contains_age(150.0));
/// assert_eq!(Era::ALL.len(), 3);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum Era {
    // Phanerozoic eras
    Paleozoic,
    Mesozoic,
    Cenozoic,
}

impl Era {
    /// Returns the time interval for this era.
    ///
    /// # Examples
    ///
    /// ```
    /// # use khanij::*;
    /// let cenozoic = Era::Cenozoic.interval();
    /// assert!((cenozoic.start_ma - 66.0).abs() < 0.1);
    /// ```
    #[must_use]
    pub fn interval(&self) -> TimeInterval {
        match self {
            Self::Paleozoic => TimeInterval {
                name: "Paleozoic",
                start_ma: 538.8,
                end_ma: 251.9,
            },
            Self::Mesozoic => TimeInterval {
                name: "Mesozoic",
                start_ma: 251.9,
                end_ma: 66.0,
            },
            Self::Cenozoic => TimeInterval {
                name: "Cenozoic",
                start_ma: 66.0,
                end_ma: 0.0,
            },
        }
    }

    /// Parent eon.
    ///
    /// # Examples
    ///
    /// ```
    /// # use khanij::*;
    /// assert_eq!(Era::Mesozoic.eon(), Eon::Phanerozoic);
    /// ```
    #[must_use]
    pub fn eon(&self) -> Eon {
        Eon::Phanerozoic
    }

    /// All eras in chronological order.
    pub const ALL: &'static [Era] = &[Self::Paleozoic, Self::Mesozoic, Self::Cenozoic];
}

// ---------------------------------------------------------------------------
// Periods
// ---------------------------------------------------------------------------

/// Geologic period — subdivision of an era.
///
/// # Examples
///
/// ```
/// # use khanij::*;
/// let j = Period::Jurassic.interval();
/// assert!(j.contains_age(160.0));
/// assert_eq!(Period::Jurassic.era(), Era::Mesozoic);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum Period {
    // Paleozoic
    Cambrian,
    Ordovician,
    Silurian,
    Devonian,
    Carboniferous,
    Permian,
    // Mesozoic
    Triassic,
    Jurassic,
    Cretaceous,
    // Cenozoic
    Paleogene,
    Neogene,
    Quaternary,
}

impl Period {
    /// Returns the time interval for this period.
    ///
    /// # Examples
    ///
    /// ```
    /// # use khanij::*;
    /// let cretaceous = Period::Cretaceous.interval();
    /// assert!((cretaceous.start_ma - 145.0).abs() < 0.1);
    /// ```
    #[must_use]
    pub fn interval(&self) -> TimeInterval {
        match self {
            Self::Cambrian => TimeInterval {
                name: "Cambrian",
                start_ma: 538.8,
                end_ma: 485.4,
            },
            Self::Ordovician => TimeInterval {
                name: "Ordovician",
                start_ma: 485.4,
                end_ma: 443.8,
            },
            Self::Silurian => TimeInterval {
                name: "Silurian",
                start_ma: 443.8,
                end_ma: 419.2,
            },
            Self::Devonian => TimeInterval {
                name: "Devonian",
                start_ma: 419.2,
                end_ma: 358.9,
            },
            Self::Carboniferous => TimeInterval {
                name: "Carboniferous",
                start_ma: 358.9,
                end_ma: 298.9,
            },
            Self::Permian => TimeInterval {
                name: "Permian",
                start_ma: 298.9,
                end_ma: 251.9,
            },
            Self::Triassic => TimeInterval {
                name: "Triassic",
                start_ma: 251.9,
                end_ma: 201.4,
            },
            Self::Jurassic => TimeInterval {
                name: "Jurassic",
                start_ma: 201.4,
                end_ma: 145.0,
            },
            Self::Cretaceous => TimeInterval {
                name: "Cretaceous",
                start_ma: 145.0,
                end_ma: 66.0,
            },
            Self::Paleogene => TimeInterval {
                name: "Paleogene",
                start_ma: 66.0,
                end_ma: 23.03,
            },
            Self::Neogene => TimeInterval {
                name: "Neogene",
                start_ma: 23.03,
                end_ma: 2.58,
            },
            Self::Quaternary => TimeInterval {
                name: "Quaternary",
                start_ma: 2.58,
                end_ma: 0.0,
            },
        }
    }

    /// Parent era.
    ///
    /// # Examples
    ///
    /// ```
    /// # use khanij::*;
    /// assert_eq!(Period::Cambrian.era(), Era::Paleozoic);
    /// assert_eq!(Period::Jurassic.era(), Era::Mesozoic);
    /// ```
    #[must_use]
    pub fn era(&self) -> Era {
        match self {
            Self::Cambrian
            | Self::Ordovician
            | Self::Silurian
            | Self::Devonian
            | Self::Carboniferous
            | Self::Permian => Era::Paleozoic,
            Self::Triassic | Self::Jurassic | Self::Cretaceous => Era::Mesozoic,
            Self::Paleogene | Self::Neogene | Self::Quaternary => Era::Cenozoic,
        }
    }

    /// All periods in chronological order.
    pub const ALL: &'static [Period] = &[
        Self::Cambrian,
        Self::Ordovician,
        Self::Silurian,
        Self::Devonian,
        Self::Carboniferous,
        Self::Permian,
        Self::Triassic,
        Self::Jurassic,
        Self::Cretaceous,
        Self::Paleogene,
        Self::Neogene,
        Self::Quaternary,
    ];
}

// ---------------------------------------------------------------------------
// Epochs (Cenozoic detail)
// ---------------------------------------------------------------------------

/// Geologic epoch — subdivision of a period (Cenozoic detail).
///
/// # Examples
///
/// ```
/// # use khanij::*;
/// let eocene = Epoch::Eocene.interval();
/// assert!(eocene.contains_age(45.0));
/// assert_eq!(Epoch::Eocene.period(), Period::Paleogene);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum Epoch {
    // Paleogene
    Paleocene,
    Eocene,
    Oligocene,
    // Neogene
    Miocene,
    Pliocene,
    // Quaternary
    Pleistocene,
    Holocene,
}

impl Epoch {
    /// Returns the time interval for this epoch.
    ///
    /// # Examples
    ///
    /// ```
    /// # use khanij::*;
    /// let holocene = Epoch::Holocene.interval();
    /// assert!((holocene.end_ma).abs() < 0.001);
    /// ```
    #[must_use]
    pub fn interval(&self) -> TimeInterval {
        match self {
            Self::Paleocene => TimeInterval {
                name: "Paleocene",
                start_ma: 66.0,
                end_ma: 56.0,
            },
            Self::Eocene => TimeInterval {
                name: "Eocene",
                start_ma: 56.0,
                end_ma: 33.9,
            },
            Self::Oligocene => TimeInterval {
                name: "Oligocene",
                start_ma: 33.9,
                end_ma: 23.03,
            },
            Self::Miocene => TimeInterval {
                name: "Miocene",
                start_ma: 23.03,
                end_ma: 5.33,
            },
            Self::Pliocene => TimeInterval {
                name: "Pliocene",
                start_ma: 5.33,
                end_ma: 2.58,
            },
            Self::Pleistocene => TimeInterval {
                name: "Pleistocene",
                start_ma: 2.58,
                end_ma: 0.0117,
            },
            Self::Holocene => TimeInterval {
                name: "Holocene",
                start_ma: 0.0117,
                end_ma: 0.0,
            },
        }
    }

    /// Parent period.
    ///
    /// # Examples
    ///
    /// ```
    /// # use khanij::*;
    /// assert_eq!(Epoch::Miocene.period(), Period::Neogene);
    /// assert_eq!(Epoch::Holocene.period(), Period::Quaternary);
    /// ```
    #[must_use]
    pub fn period(&self) -> Period {
        match self {
            Self::Paleocene | Self::Eocene | Self::Oligocene => Period::Paleogene,
            Self::Miocene | Self::Pliocene => Period::Neogene,
            Self::Pleistocene | Self::Holocene => Period::Quaternary,
        }
    }

    /// All epochs in chronological order.
    pub const ALL: &'static [Epoch] = &[
        Self::Paleocene,
        Self::Eocene,
        Self::Oligocene,
        Self::Miocene,
        Self::Pliocene,
        Self::Pleistocene,
        Self::Holocene,
    ];
}

// ---------------------------------------------------------------------------
// Lookup functions
// ---------------------------------------------------------------------------

/// Look up the geologic period for a given age in Ma.
///
/// # Examples
///
/// ```
/// # use khanij::*;
/// assert_eq!(period_at_age(70.0), Some(Period::Cretaceous));
/// assert_eq!(period_at_age(3000.0), None);
/// ```
#[must_use]
pub fn period_at_age(age_ma: f64) -> Option<Period> {
    Period::ALL
        .iter()
        .find(|p| p.interval().contains_age(age_ma))
        .copied()
}

/// Look up the geologic era for a given age in Ma.
///
/// # Examples
///
/// ```
/// # use khanij::*;
/// assert_eq!(era_at_age(100.0), Some(Era::Mesozoic));
/// assert_eq!(era_at_age(3000.0), None);
/// ```
#[must_use]
pub fn era_at_age(age_ma: f64) -> Option<Era> {
    Era::ALL
        .iter()
        .find(|e| e.interval().contains_age(age_ma))
        .copied()
}

/// Look up the geologic eon for a given age in Ma.
///
/// # Examples
///
/// ```
/// # use khanij::*;
/// assert_eq!(eon_at_age(3000.0), Some(Eon::Archean));
/// assert_eq!(eon_at_age(5000.0), None);
/// ```
#[must_use]
pub fn eon_at_age(age_ma: f64) -> Option<Eon> {
    Eon::ALL
        .iter()
        .find(|e| e.interval().contains_age(age_ma))
        .copied()
}

/// Look up the Cenozoic epoch for a given age in Ma.
/// Returns `None` for ages outside the Cenozoic.
///
/// # Examples
///
/// ```
/// # use khanij::*;
/// assert_eq!(epoch_at_age(45.0), Some(Epoch::Eocene));
/// assert_eq!(epoch_at_age(100.0), None); // Cretaceous
/// ```
#[must_use]
pub fn epoch_at_age(age_ma: f64) -> Option<Epoch> {
    Epoch::ALL
        .iter()
        .find(|e| e.interval().contains_age(age_ma))
        .copied()
}

/// Full stratigraphic classification for a given age.
///
/// # Examples
///
/// ```
/// # use khanij::*;
/// let pos = classify_age(150.0);
/// assert_eq!(pos.period, Some(Period::Jurassic));
/// assert_eq!(pos.epoch, None); // pre-Cenozoic
/// ```
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct StratigraphicPosition {
    pub age_ma: f64,
    pub eon: Option<Eon>,
    pub era: Option<Era>,
    pub period: Option<Period>,
    pub epoch: Option<Epoch>,
}

/// Classify an age into its full stratigraphic position.
///
/// # Examples
///
/// ```
/// # use khanij::*;
/// let pos = classify_age(10.0);
/// assert_eq!(pos.eon, Some(Eon::Phanerozoic));
/// assert_eq!(pos.era, Some(Era::Cenozoic));
/// assert_eq!(pos.epoch, Some(Epoch::Miocene));
/// ```
#[must_use]
pub fn classify_age(age_ma: f64) -> StratigraphicPosition {
    StratigraphicPosition {
        age_ma,
        eon: eon_at_age(age_ma),
        era: era_at_age(age_ma),
        period: period_at_age(age_ma),
        epoch: epoch_at_age(age_ma),
    }
}

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

    #[test]
    fn present_day_is_quaternary() {
        assert_eq!(period_at_age(0.001), Some(Period::Quaternary));
    }

    #[test]
    fn present_is_holocene() {
        assert_eq!(epoch_at_age(0.005), Some(Epoch::Holocene));
    }

    #[test]
    fn dinosaur_age_is_cretaceous() {
        assert_eq!(period_at_age(70.0), Some(Period::Cretaceous));
    }

    #[test]
    fn jurassic_boundaries() {
        let j = Period::Jurassic.interval();
        assert!(j.contains_age(150.0));
        assert!(!j.contains_age(210.0)); // Triassic
        assert!(!j.contains_age(140.0)); // Cretaceous
    }

    #[test]
    fn cambrian_explosion() {
        assert_eq!(period_at_age(520.0), Some(Period::Cambrian));
        assert_eq!(era_at_age(520.0), Some(Era::Paleozoic));
        assert_eq!(eon_at_age(520.0), Some(Eon::Phanerozoic));
    }

    #[test]
    fn precambrian() {
        assert_eq!(eon_at_age(3000.0), Some(Eon::Archean));
        assert_eq!(era_at_age(3000.0), None); // eras only defined for Phanerozoic
        assert_eq!(period_at_age(3000.0), None);
    }

    #[test]
    fn hadean() {
        assert_eq!(eon_at_age(4500.0), Some(Eon::Hadean));
    }

    #[test]
    fn proterozoic() {
        assert_eq!(eon_at_age(1000.0), Some(Eon::Proterozoic));
    }

    #[test]
    fn beyond_earth_returns_none() {
        assert_eq!(eon_at_age(5000.0), None);
    }

    #[test]
    fn epoch_outside_cenozoic_is_none() {
        assert_eq!(epoch_at_age(100.0), None); // Cretaceous — no epoch detail
    }

    #[test]
    fn eocene_epoch() {
        assert_eq!(epoch_at_age(45.0), Some(Epoch::Eocene));
        assert_eq!(epoch_at_age(45.0).unwrap().period(), Period::Paleogene);
    }

    #[test]
    fn pleistocene_ice_age() {
        assert_eq!(epoch_at_age(1.0), Some(Epoch::Pleistocene));
    }

    #[test]
    fn period_era_relationship() {
        assert_eq!(Period::Jurassic.era(), Era::Mesozoic);
        assert_eq!(Period::Cambrian.era(), Era::Paleozoic);
        assert_eq!(Period::Quaternary.era(), Era::Cenozoic);
    }

    #[test]
    fn all_periods_cover_phanerozoic() {
        // Ensure periods span continuously from 538.8 to 0.0
        let first = Period::ALL.first().unwrap().interval();
        let last = Period::ALL.last().unwrap().interval();
        assert!((first.start_ma - 538.8).abs() < 0.1);
        assert!(last.end_ma.abs() < 0.01);
    }

    #[test]
    fn periods_are_contiguous() {
        for pair in Period::ALL.windows(2) {
            let older = pair[0].interval();
            let younger = pair[1].interval();
            assert!(
                (older.end_ma - younger.start_ma).abs() < 0.01,
                "{} end ({}) should equal {} start ({})",
                older.name,
                older.end_ma,
                younger.name,
                younger.start_ma
            );
        }
    }

    #[test]
    fn duration_positive() {
        for p in Period::ALL {
            assert!(
                p.interval().duration_ma() > 0.0,
                "{} should have positive duration",
                p.interval().name
            );
        }
    }

    #[test]
    fn classify_age_full() {
        let pos = classify_age(150.0);
        assert_eq!(pos.eon, Some(Eon::Phanerozoic));
        assert_eq!(pos.era, Some(Era::Mesozoic));
        assert_eq!(pos.period, Some(Period::Jurassic));
        assert_eq!(pos.epoch, None); // pre-Cenozoic
    }

    #[test]
    fn classify_age_cenozoic() {
        let pos = classify_age(10.0);
        assert_eq!(pos.eon, Some(Eon::Phanerozoic));
        assert_eq!(pos.era, Some(Era::Cenozoic));
        assert_eq!(pos.period, Some(Period::Neogene));
        assert_eq!(pos.epoch, Some(Epoch::Miocene));
    }

    #[test]
    fn twelve_periods() {
        assert_eq!(Period::ALL.len(), 12);
    }

    #[test]
    fn four_eons() {
        assert_eq!(Eon::ALL.len(), 4);
    }

    #[test]
    fn seven_epochs() {
        assert_eq!(Epoch::ALL.len(), 7);
    }
}