ip2location/ip2location/
record.rs

1#![allow(clippy::derive_partial_eq_without_eq)]
2
3use serde::Serialize;
4use serde_with::skip_serializing_none;
5use std::{
6    borrow::Cow,
7    net::{IpAddr, Ipv6Addr},
8};
9
10#[derive(PartialEq, Debug, Clone, Serialize)]
11pub struct Country<'a> {
12    pub short_name: Cow<'a, str>,
13    pub long_name: Cow<'a, str>,
14}
15
16#[skip_serializing_none]
17#[derive(PartialEq, Debug, Clone, Serialize)]
18pub struct LocationRecord<'a> {
19    pub ip: IpAddr,
20    pub latitude: Option<f32>,
21    pub longitude: Option<f32>,
22    pub country: Option<Country<'a>>,
23    pub region: Option<Cow<'a, str>>,
24    pub city: Option<Cow<'a, str>>,
25    pub isp: Option<Cow<'a, str>>,
26    pub domain: Option<Cow<'a, str>>,
27    pub zip_code: Option<Cow<'a, str>>,
28    pub time_zone: Option<Cow<'a, str>>,
29    pub net_speed: Option<Cow<'a, str>>,
30    pub idd_code: Option<Cow<'a, str>>,
31    pub area_code: Option<Cow<'a, str>>,
32    pub weather_station_code: Option<Cow<'a, str>>,
33    pub weather_station_name: Option<Cow<'a, str>>,
34    pub mcc: Option<Cow<'a, str>>,
35    pub mnc: Option<Cow<'a, str>>,
36    pub mobile_brand: Option<Cow<'a, str>>,
37    pub elevation: Option<Cow<'a, str>>,
38    pub usage_type: Option<Cow<'a, str>>,
39    pub address_type: Option<Cow<'a, str>>,
40    pub category: Option<Cow<'a, str>>,
41    pub district: Option<Cow<'a, str>>,
42    pub asn: Option<Cow<'a, str>>,
43    pub as_name: Option<Cow<'a, str>>,
44    pub as_domain: Option<Cow<'a, str>>,
45    pub as_usage_type: Option<Cow<'a, str>>,
46    pub as_cidr: Option<Cow<'a, str>>,
47}
48
49impl LocationRecord<'_> {
50    pub fn to_json(&self) -> String {
51        serde_json::to_string(&self).unwrap()
52    }
53}
54
55impl Default for LocationRecord<'_> {
56    fn default() -> Self {
57        LocationRecord {
58            ip: IpAddr::V6(Ipv6Addr::UNSPECIFIED),
59            latitude: None,
60            longitude: None,
61            country: None,
62            region: None,
63            city: None,
64            isp: None,
65            domain: None,
66            zip_code: None,
67            time_zone: None,
68            net_speed: None,
69            idd_code: None,
70            area_code: None,
71            weather_station_code: None,
72            weather_station_name: None,
73            mcc: None,
74            mnc: None,
75            mobile_brand: None,
76            elevation: None,
77            usage_type: None,
78            address_type: None,
79            category: None,
80            district: None,
81            asn: None,
82            as_name: None,
83            as_domain: None,
84            as_usage_type: None,
85            as_cidr: None,
86        }
87    }
88}