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
//! The geoipsed library for IP address extraction and geolocation tagging.
//!
//! This library provides functionality for finding and decorating IP addresses
//! with geolocation information from various MMDB databases.
//!
//! # Examples
//!
//! Creating and registering a custom MMDB provider:
//!
//! ```rust,ignore
//! use geoipsed::{MmdbProvider, ProviderRegistry, TemplateField, template::Template};
//! use std::path::{Path, PathBuf};
//! use anyhow::Result;
//!
//! // Implement the MmdbProvider trait for your custom provider
//! #[derive(Debug)]
//! struct CustomProvider {
//! name: String,
//! initialized: bool,
//! }
//!
//! impl MmdbProvider for CustomProvider {
//! fn name(&self) -> &str {
//! &self.name
//! }
//!
//! fn default_path(&self) -> PathBuf {
//! PathBuf::from("/path/to/mmdb")
//! }
//!
//! fn required_files(&self) -> Vec<String> {
//! vec!["custom.mmdb".to_string()]
//! }
//!
//! fn available_fields(&self) -> Vec<TemplateField> {
//! vec![
//! TemplateField {
//! name: "ip".to_string(),
//! description: "The IP address".to_string(),
//! example: "93.184.216.34".to_string(),
//! },
//! ]
//! }
//!
//! fn initialize(&mut self, _path: &Path) -> Result<()> {
//! self.initialized = true;
//! Ok(())
//! }
//!
//! fn lookup(
//! &self,
//! _ip: std::net::IpAddr,
//! ip_str: &str,
//! template: &Template,
//! ) -> Result<String> {
//! Ok(template.render(|field| match field {
//! "ip" => ip_str,
//! _ => "",
//! }))
//! }
//!
//! fn lookup_and_write(
//! &self,
//! wtr: &mut dyn std::io::Write,
//! _ip: std::net::IpAddr,
//! ip_str: &str,
//! template: &Template,
//! ) -> Result<()> {
//! template.write(wtr, |out, field| match field {
//! "ip" => out.write_all(ip_str.as_bytes()),
//! _ => Ok(()),
//! })?;
//! Ok(())
//! }
//!
//! fn has_asn(&self, _ip: std::net::IpAddr) -> bool {
//! false
//! }
//! }
//!
//! // Register with a registry
//! let mut registry = ProviderRegistry::default();
//! registry.register(
//! "custom".to_string(),
//! Box::new(CustomProvider {
//! name: "My Custom Provider".to_string(),
//! initialized: false,
//! })
//! );
//! ```
/// Re-export key traits for custom MMDB format support
pub use crateError;
pub use crate;
/// Re-export IP extraction types from the sub-crate
pub use ;