Skip to main content

ipwhois/
response.rs

1//! Deserialized response types for the ipwhois.io API.
2//!
3//! Every field is an [`Option`] because the API only returns the fields you
4//! ask for when the `fields` parameter is set, and several blocks
5//! (`security`, `rate`) are gated on plan tier. An [`extra`](LookupResponse::extra)
6//! map captures any future fields not yet known to this crate, so a server-
7//! side schema bump never breaks existing callers.
8
9use serde::{Deserialize, Serialize};
10use std::collections::HashMap;
11
12/// Successful lookup response, or a per-IP entry inside a bulk response.
13///
14/// In the bulk endpoint each array entry is a `LookupResponse`; entries with
15/// `success = false` carry the `message` and `error_type` fields populated by
16/// the API instead of the geolocation fields.
17#[derive(Debug, Clone, Default, Serialize, Deserialize)]
18#[non_exhaustive]
19pub struct LookupResponse {
20    /// `true` for successful lookups; `false` for per-IP errors inside a
21    /// bulk response (e.g. "Invalid IP address", "Reserved range").
22    #[serde(default)]
23    pub success: bool,
24
25    /// The IP address that was looked up. Echoed back by the API.
26    pub ip: Option<String>,
27
28    /// `"IPv4"` or `"IPv6"`.
29    #[serde(rename = "type")]
30    pub ip_type: Option<String>,
31
32    pub continent: Option<String>,
33    pub continent_code: Option<String>,
34    pub country: Option<String>,
35    pub country_code: Option<String>,
36    pub region: Option<String>,
37    pub region_code: Option<String>,
38    pub city: Option<String>,
39
40    pub latitude: Option<f64>,
41    pub longitude: Option<f64>,
42
43    pub is_eu: Option<bool>,
44    pub postal: Option<String>,
45    pub calling_code: Option<String>,
46    pub capital: Option<String>,
47    /// Comma-separated list of bordering country codes (e.g. `"CA,MX"`).
48    pub borders: Option<String>,
49
50    pub flag: Option<Flag>,
51    pub connection: Option<Connection>,
52    pub timezone: Option<Timezone>,
53    pub currency: Option<Currency>,
54    pub security: Option<Security>,
55    pub rate: Option<Rate>,
56
57    /// Populated when `success = false` (per-IP error inside a bulk response).
58    pub message: Option<String>,
59    /// Populated alongside `message` when the API tags the error type.
60    pub error_type: Option<String>,
61
62    /// Forward-compatibility bucket for any fields not yet modelled by this
63    /// crate. New API fields land here without requiring a release.
64    #[serde(flatten)]
65    pub extra: HashMap<String, serde_json::Value>,
66}
67
68#[derive(Debug, Clone, Default, Serialize, Deserialize)]
69#[non_exhaustive]
70pub struct Flag {
71    pub img: Option<String>,
72    pub emoji: Option<String>,
73    pub emoji_unicode: Option<String>,
74}
75
76#[derive(Debug, Clone, Default, Serialize, Deserialize)]
77#[non_exhaustive]
78pub struct Connection {
79    pub asn: Option<u64>,
80    pub org: Option<String>,
81    pub isp: Option<String>,
82    pub domain: Option<String>,
83}
84
85#[derive(Debug, Clone, Default, Serialize, Deserialize)]
86#[non_exhaustive]
87pub struct Timezone {
88    pub id: Option<String>,
89    pub abbr: Option<String>,
90    pub is_dst: Option<bool>,
91    pub offset: Option<i64>,
92    pub utc: Option<String>,
93    pub current_time: Option<String>,
94}
95
96#[derive(Debug, Clone, Default, Serialize, Deserialize)]
97#[non_exhaustive]
98pub struct Currency {
99    pub name: Option<String>,
100    pub code: Option<String>,
101    pub symbol: Option<String>,
102    pub plural: Option<String>,
103    pub exchange_rate: Option<f64>,
104}
105
106/// Threat-detection block. Returned only when the `security` option is
107/// enabled and the API key has Business+ access.
108#[derive(Debug, Clone, Default, Serialize, Deserialize)]
109#[non_exhaustive]
110pub struct Security {
111    pub anonymous: Option<bool>,
112    pub proxy: Option<bool>,
113    pub vpn: Option<bool>,
114    pub tor: Option<bool>,
115    pub hosting: Option<bool>,
116}
117
118/// Rate-limit block. Returned only when the `rate` option is enabled and
119/// the API key has Basic+ access.
120#[derive(Debug, Clone, Default, Serialize, Deserialize)]
121#[non_exhaustive]
122pub struct Rate {
123    pub limit: Option<u64>,
124    pub remaining: Option<u64>,
125}