maxminddb/metadata.rs
1//! Database metadata types.
2
3use std::collections::BTreeMap;
4use std::time::{Duration, SystemTime, UNIX_EPOCH};
5
6use serde::{Deserialize, Serialize};
7
8use crate::error::MaxMindDbError;
9
10/// Metadata about the MaxMind DB file.
11#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Eq)]
12pub struct Metadata {
13 /// Major version of the binary format (always 2).
14 pub binary_format_major_version: u16,
15 /// Minor version of the binary format.
16 /// Minor versions are accepted for forward compatibility.
17 pub binary_format_minor_version: u16,
18 /// Unix timestamp when the database was built.
19 pub build_epoch: u64,
20 /// Database type (e.g., "GeoIP2-City", "GeoLite2-Country").
21 pub database_type: String,
22 /// Map of language codes to database descriptions.
23 pub description: BTreeMap<String, String>,
24 /// IP version supported (4 or 6).
25 pub ip_version: u16,
26 /// Languages available in the database.
27 pub languages: Vec<String>,
28 /// Number of nodes in the search tree.
29 pub node_count: u32,
30 /// Size of each record in bits (24, 28, or 32).
31 pub record_size: u16,
32}
33
34impl Metadata {
35 /// Returns the database build time as a `SystemTime`.
36 ///
37 /// This converts the `build_epoch` Unix timestamp to a `SystemTime`.
38 /// If `build_epoch` is too large to represent on this platform, this
39 /// returns an [`InvalidDatabase`](MaxMindDbError::InvalidDatabase) error.
40 ///
41 /// # Example
42 ///
43 /// ```
44 /// use maxminddb::Reader;
45 ///
46 /// let reader = Reader::open_readfile("test-data/test-data/GeoIP2-City-Test.mmdb").unwrap();
47 /// let build_time = reader.metadata().build_time().unwrap();
48 /// println!("Database built: {:?}", build_time);
49 /// ```
50 #[inline]
51 pub fn build_time(&self) -> Result<SystemTime, MaxMindDbError> {
52 UNIX_EPOCH
53 .checked_add(Duration::from_secs(self.build_epoch))
54 .ok_or_else(|| {
55 MaxMindDbError::invalid_database(format!(
56 "build_epoch - Unix timestamp is too large to represent as SystemTime: {}",
57 self.build_epoch
58 ))
59 })
60 }
61}