itihas 1.0.0

Itihas — structured world history: civilizations, eras, events, historical figures, and calendar metadata
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
//! Historical periods with date ranges and civilizational phases.
//!
//! Provides [`Era`] structs representing major historical periods, an
//! [`EraCategory`] classification enum, and pre-built eras from the Bronze Age
//! to the Information Age.

use alloc::borrow::Cow;
use alloc::string::String;
use alloc::vec;
use alloc::vec::Vec;
use core::cmp::Ordering;
use core::fmt;

use serde::{Deserialize, Serialize};

use crate::error::ItihasError;

/// Classification of historical periods.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum EraCategory {
    /// Earliest civilizations, pre-literacy to early writing (~3300 BCE - 800 BCE).
    Ancient,
    /// Greco-Roman flowering (~800 BCE - 500 CE).
    Classical,
    /// Post-Roman European period (~500 - 1500 CE).
    Medieval,
    /// Renaissance through Enlightenment (~1400 - 1800 CE).
    EarlyModern,
    /// Industrial Revolution through World Wars (~1760 - 1970 CE).
    Modern,
    /// Post-WWII to present (~1945 - present).
    Contemporary,
}

impl fmt::Display for EraCategory {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Ancient => f.write_str("Ancient"),
            Self::Classical => f.write_str("Classical"),
            Self::Medieval => f.write_str("Medieval"),
            Self::EarlyModern => f.write_str("Early Modern"),
            Self::Modern => f.write_str("Modern"),
            Self::Contemporary => f.write_str("Contemporary"),
        }
    }
}

/// Whether an era represents a global periodization or a regional tradition.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum EraScope {
    /// Standard world-history periodization (Bronze Age, Classical, etc.).
    Global,
    /// Region-specific periodization (Chinese dynasties, Indian yugas, etc.).
    Regional,
}

impl fmt::Display for EraScope {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Global => f.write_str("Global"),
            Self::Regional => f.write_str("Regional"),
        }
    }
}

/// A historical era with date range and region.
///
/// Years use astronomical year numbering: negative values represent BCE
/// (e.g., -3300 = 3300 BCE), positive values represent CE.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Era {
    /// Name of the era.
    pub name: Cow<'static, str>,
    /// Start year (negative = BCE).
    pub start_year: i32,
    /// End year (negative = BCE). `i32::MAX` means ongoing.
    pub end_year: i32,
    /// Geographic region or scope.
    pub region: Cow<'static, str>,
    /// Brief description.
    pub description: Cow<'static, str>,
    /// Category classification.
    pub category: EraCategory,
    /// Whether this is a global or regional periodization.
    pub scope: EraScope,
}

impl fmt::Display for Era {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.end_year == i32::MAX {
            write!(f, "{} ({} – present)", self.name, self.start_year)
        } else {
            write!(f, "{} ({}{})", self.name, self.start_year, self.end_year)
        }
    }
}

impl PartialOrd for Era {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Era {
    fn cmp(&self, other: &Self) -> Ordering {
        self.start_year
            .cmp(&other.start_year)
            .then_with(|| self.end_year.cmp(&other.end_year))
    }
}

fn build_eras() -> Vec<Era> {
    vec![
        // ---- Global periodization ----
        Era {
            name: Cow::Borrowed("Bronze Age"),
            start_year: -3500,
            end_year: -1100,
            region: Cow::Borrowed("Near East, Mediterranean, South Asia"),
            description: Cow::Borrowed(
                "Emergence of bronze metallurgy, early writing systems, and first cities",
            ),
            category: EraCategory::Ancient,
            scope: EraScope::Global,
        },
        Era {
            name: Cow::Borrowed("Iron Age"),
            start_year: -1200,
            end_year: -550,
            region: Cow::Borrowed("Near East, Mediterranean, South Asia"),
            description: Cow::Borrowed(
                "Widespread iron smelting, alphabetic writing, rise of empires",
            ),
            category: EraCategory::Ancient,
            scope: EraScope::Global,
        },
        Era {
            name: Cow::Borrowed("Classical Antiquity"),
            start_year: -800,
            end_year: 476,
            region: Cow::Borrowed("Mediterranean, Europe"),
            description: Cow::Borrowed(
                "Greek and Roman civilization, philosophy, democracy, republic",
            ),
            category: EraCategory::Classical,
            scope: EraScope::Global,
        },
        Era {
            name: Cow::Borrowed("Middle Ages"),
            start_year: 476,
            end_year: 1453,
            region: Cow::Borrowed("Europe, Near East"),
            description: Cow::Borrowed("Feudalism, monasticism, Islamic Golden Age, Crusades"),
            category: EraCategory::Medieval,
            scope: EraScope::Global,
        },
        Era {
            name: Cow::Borrowed("Renaissance"),
            start_year: 1400,
            end_year: 1600,
            region: Cow::Borrowed("Europe"),
            description: Cow::Borrowed(
                "Revival of classical learning, art, science, and exploration",
            ),
            category: EraCategory::EarlyModern,
            scope: EraScope::Global,
        },
        Era {
            name: Cow::Borrowed("Age of Enlightenment"),
            start_year: 1600,
            end_year: 1789,
            region: Cow::Borrowed("Europe"),
            description: Cow::Borrowed(
                "Scientific Revolution, rationalism, empiricism, social contract theory",
            ),
            category: EraCategory::EarlyModern,
            scope: EraScope::Global,
        },
        Era {
            name: Cow::Borrowed("Industrial Age"),
            start_year: 1760,
            end_year: 1970,
            region: Cow::Borrowed("Global"),
            description: Cow::Borrowed("Mechanization, mass production, urbanization, world wars"),
            category: EraCategory::Modern,
            scope: EraScope::Global,
        },
        Era {
            name: Cow::Borrowed("Information Age"),
            start_year: 1970,
            end_year: i32::MAX,
            region: Cow::Borrowed("Global"),
            description: Cow::Borrowed("Digital revolution, internet, AI, globalization"),
            category: EraCategory::Contemporary,
            scope: EraScope::Global,
        },
        // ---- Chinese dynasties ----
        Era {
            name: Cow::Borrowed("Xia Dynasty"),
            start_year: -2070,
            end_year: -1600,
            region: Cow::Borrowed("East Asia"),
            description: Cow::Borrowed(
                "Semi-legendary first dynasty of China, Yellow River valley",
            ),
            category: EraCategory::Ancient,
            scope: EraScope::Regional,
        },
        Era {
            name: Cow::Borrowed("Shang Dynasty"),
            start_year: -1600,
            end_year: -1046,
            region: Cow::Borrowed("East Asia"),
            description: Cow::Borrowed("Oracle bones, bronze casting, earliest Chinese writing"),
            category: EraCategory::Ancient,
            scope: EraScope::Regional,
        },
        Era {
            name: Cow::Borrowed("Zhou Dynasty"),
            start_year: -1046,
            end_year: -256,
            region: Cow::Borrowed("East Asia"),
            description: Cow::Borrowed(
                "Mandate of Heaven, Confucius, Laozi, Hundred Schools of Thought",
            ),
            category: EraCategory::Ancient,
            scope: EraScope::Regional,
        },
        Era {
            name: Cow::Borrowed("Qin Dynasty"),
            start_year: -221,
            end_year: -206,
            region: Cow::Borrowed("East Asia"),
            description: Cow::Borrowed(
                "First unification of China, Great Wall begun, Legalism, terracotta army",
            ),
            category: EraCategory::Classical,
            scope: EraScope::Regional,
        },
        Era {
            name: Cow::Borrowed("Han Dynasty"),
            start_year: -206,
            end_year: 220,
            region: Cow::Borrowed("East Asia"),
            description: Cow::Borrowed(
                "Silk Road, Confucian state, paper invention, civil service exams",
            ),
            category: EraCategory::Classical,
            scope: EraScope::Regional,
        },
        Era {
            name: Cow::Borrowed("Tang Dynasty"),
            start_year: 618,
            end_year: 907,
            region: Cow::Borrowed("East Asia"),
            description: Cow::Borrowed(
                "Golden age of Chinese poetry, cosmopolitan culture, woodblock printing",
            ),
            category: EraCategory::Medieval,
            scope: EraScope::Regional,
        },
        Era {
            name: Cow::Borrowed("Song Dynasty"),
            start_year: 960,
            end_year: 1279,
            region: Cow::Borrowed("East Asia"),
            description: Cow::Borrowed(
                "Movable type, gunpowder weapons, compass navigation, Neo-Confucianism",
            ),
            category: EraCategory::Medieval,
            scope: EraScope::Regional,
        },
        Era {
            name: Cow::Borrowed("Ming Dynasty"),
            start_year: 1368,
            end_year: 1644,
            region: Cow::Borrowed("East Asia"),
            description: Cow::Borrowed(
                "Great Wall reconstruction, Forbidden City, Zheng He voyages, porcelain",
            ),
            category: EraCategory::EarlyModern,
            scope: EraScope::Regional,
        },
        Era {
            name: Cow::Borrowed("Qing Dynasty"),
            start_year: 1644,
            end_year: 1912,
            region: Cow::Borrowed("East Asia"),
            description: Cow::Borrowed(
                "Last imperial dynasty, Manchu rule, territorial expansion, Opium Wars",
            ),
            category: EraCategory::EarlyModern,
            scope: EraScope::Regional,
        },
        // ---- Indian periods ----
        Era {
            name: Cow::Borrowed("Vedic Period"),
            start_year: -1500,
            end_year: -500,
            region: Cow::Borrowed("South Asia"),
            description: Cow::Borrowed(
                "Composition of the Vedas, varna system, early Hinduism, Sanskrit literature",
            ),
            category: EraCategory::Ancient,
            scope: EraScope::Regional,
        },
        Era {
            name: Cow::Borrowed("Maurya Period"),
            start_year: -322,
            end_year: -185,
            region: Cow::Borrowed("South Asia"),
            description: Cow::Borrowed(
                "First pan-Indian empire under Chandragupta and Ashoka, spread of Buddhism",
            ),
            category: EraCategory::Classical,
            scope: EraScope::Regional,
        },
        Era {
            name: Cow::Borrowed("Gupta Period"),
            start_year: 320,
            end_year: 550,
            region: Cow::Borrowed("South Asia"),
            description: Cow::Borrowed(
                "Golden age of India: zero, decimal system, Kalidasa, Aryabhata, Nalanda",
            ),
            category: EraCategory::Classical,
            scope: EraScope::Regional,
        },
        Era {
            name: Cow::Borrowed("Delhi Sultanate"),
            start_year: 1206,
            end_year: 1526,
            region: Cow::Borrowed("South Asia"),
            description: Cow::Borrowed(
                "Turkic and Afghan Muslim rule in northern India, Indo-Islamic architecture",
            ),
            category: EraCategory::Medieval,
            scope: EraScope::Regional,
        },
        Era {
            name: Cow::Borrowed("Mughal Period"),
            start_year: 1526,
            end_year: 1857,
            region: Cow::Borrowed("South Asia"),
            description: Cow::Borrowed(
                "Babur to Bahadur Shah, Taj Mahal, Akbar's religious tolerance, Urdu synthesis",
            ),
            category: EraCategory::EarlyModern,
            scope: EraScope::Regional,
        },
        // ---- Mesoamerican periods ----
        Era {
            name: Cow::Borrowed("Mesoamerican Preclassic"),
            start_year: -2000,
            end_year: 250,
            region: Cow::Borrowed("Mesoamerica"),
            description: Cow::Borrowed(
                "Olmec civilization, early Maya settlements, development of writing and calendars",
            ),
            category: EraCategory::Ancient,
            scope: EraScope::Regional,
        },
        Era {
            name: Cow::Borrowed("Mesoamerican Classic"),
            start_year: 250,
            end_year: 900,
            region: Cow::Borrowed("Mesoamerica"),
            description: Cow::Borrowed(
                "Maya golden age, Teotihuacan, Zapotec, monumental architecture and astronomy",
            ),
            category: EraCategory::Classical,
            scope: EraScope::Regional,
        },
        Era {
            name: Cow::Borrowed("Mesoamerican Postclassic"),
            start_year: 900,
            end_year: 1521,
            region: Cow::Borrowed("Mesoamerica"),
            description: Cow::Borrowed(
                "Toltec and Aztec empires, Chichen Itza, Tenochtitlan, Spanish contact",
            ),
            category: EraCategory::Medieval,
            scope: EraScope::Regional,
        },
    ]
}

/// Returns all pre-built eras.
///
/// Data is computed once and cached for the lifetime of the process.
#[cfg(feature = "std")]
#[must_use]
#[inline]
pub fn all_eras() -> &'static [Era] {
    static DATA: std::sync::LazyLock<Vec<Era>> = std::sync::LazyLock::new(build_eras);
    &DATA
}

/// Returns all pre-built eras.
#[cfg(not(feature = "std"))]
#[must_use]
#[inline]
pub fn all_eras() -> Vec<Era> {
    build_eras()
}

/// Returns all eras that contain the given year.
///
/// Years use astronomical year numbering: negative = BCE, positive = CE.
#[must_use]
#[inline]
pub fn eras_containing(year: i32) -> Vec<Era> {
    tracing::debug!(year, "looking up eras containing year");
    all_eras()
        .iter()
        .filter(|e| year >= e.start_year && year <= e.end_year)
        .cloned()
        .collect()
}

/// Returns eras matching the given scope (Global or Regional).
#[must_use]
#[inline]
pub fn by_scope(scope: &EraScope) -> Vec<Era> {
    tracing::debug!(?scope, "looking up eras by scope");
    all_eras()
        .iter()
        .filter(|e| e.scope == *scope)
        .cloned()
        .collect()
}

/// Returns eras whose region contains the given substring (case-insensitive).
#[must_use]
#[inline]
pub fn by_region(region: &str) -> Vec<Era> {
    tracing::debug!(region, "looking up eras by region");
    let lower = region.to_lowercase();
    all_eras()
        .iter()
        .filter(|e| e.region.to_lowercase().contains(&lower))
        .cloned()
        .collect()
}

/// Look up an era by exact name (case-insensitive).
///
/// Returns `Err(ItihasError::UnknownEra)` if no era matches.
#[inline]
pub fn by_name(name: &str) -> Result<Era, ItihasError> {
    tracing::debug!(name, "looking up era by name");
    let lower = name.to_lowercase();
    all_eras()
        .iter()
        .find(|e| e.name.to_lowercase() == lower)
        .cloned()
        .ok_or_else(|| ItihasError::UnknownEra(String::from(name)))
}

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

    #[test]
    fn test_all_eras_count() {
        assert_eq!(all_eras().len(), 25);
    }

    #[test]
    fn test_eras_containing_bronze_age() {
        let eras = eras_containing(-2000);
        assert!(eras.iter().any(|e| e.name == "Bronze Age"));
    }

    #[test]
    fn test_eras_containing_classical() {
        let eras = eras_containing(100);
        assert!(eras.iter().any(|e| e.name == "Classical Antiquity"));
    }

    #[test]
    fn test_eras_containing_overlap() {
        // 1500 CE is in both Middle Ages (ends 1453) and Renaissance (starts 1400)
        let eras = eras_containing(1450);
        let names: Vec<_> = eras.iter().map(|e| e.name.as_ref()).collect();
        assert!(names.contains(&"Middle Ages"));
        assert!(names.contains(&"Renaissance"));
    }

    #[test]
    fn test_eras_containing_modern() {
        let eras = eras_containing(2025);
        assert!(eras.iter().any(|e| e.name == "Information Age"));
    }

    #[test]
    fn test_eras_containing_none() {
        // Year far in the past
        let eras = eras_containing(-50000);
        assert!(eras.is_empty());
    }

    #[test]
    fn test_era_serde_roundtrip() {
        let eras = all_eras();
        for era in eras.iter() {
            let json = serde_json::to_string(era).unwrap();
            let back: Era = serde_json::from_str(&json).unwrap();
            assert_eq!(era, &back);
        }
    }

    #[test]
    fn test_era_category_serde_roundtrip() {
        let categories = [
            EraCategory::Ancient,
            EraCategory::Classical,
            EraCategory::Medieval,
            EraCategory::EarlyModern,
            EraCategory::Modern,
            EraCategory::Contemporary,
        ];
        for cat in &categories {
            let json = serde_json::to_string(cat).unwrap();
            let back: EraCategory = serde_json::from_str(&json).unwrap();
            assert_eq!(*cat, back);
        }
    }
}