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 (always 0).
16 pub binary_format_minor_version: u16,
17 /// Unix timestamp when the database was built.
18 pub build_epoch: u64,
19 /// Database type (e.g., "GeoIP2-City", "GeoLite2-Country").
20 pub database_type: String,
21 /// Map of language codes to database descriptions.
22 pub description: BTreeMap<String, String>,
23 /// IP version supported (4 or 6).
24 pub ip_version: u16,
25 /// Languages available in the database.
26 pub languages: Vec<String>,
27 /// Number of nodes in the search tree.
28 pub node_count: u32,
29 /// Size of each record in bits (24, 28, or 32).
30 pub record_size: u16,
31}
32
33impl Metadata {
34 /// Returns the database build time as a `SystemTime`.
35 ///
36 /// This converts the `build_epoch` Unix timestamp to a `SystemTime`.
37 /// If `build_epoch` is too large to represent on this platform, this
38 /// returns an [`InvalidDatabase`](MaxMindDbError::InvalidDatabase) error.
39 ///
40 /// # Example
41 ///
42 /// ```
43 /// use maxminddb::Reader;
44 ///
45 /// let reader = Reader::open_readfile("test-data/test-data/GeoIP2-City-Test.mmdb").unwrap();
46 /// let build_time = reader.metadata().build_time().unwrap();
47 /// println!("Database built: {:?}", build_time);
48 /// ```
49 #[inline]
50 pub fn build_time(&self) -> Result<SystemTime, MaxMindDbError> {
51 UNIX_EPOCH
52 .checked_add(Duration::from_secs(self.build_epoch))
53 .ok_or_else(|| {
54 MaxMindDbError::invalid_database(format!(
55 "build_epoch - Unix timestamp is too large to represent as SystemTime: {}",
56 self.build_epoch
57 ))
58 })
59 }
60}