Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json schema added through opt schemars crate #72

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ edition = "2021"
default = []
mmap = ["memmap2"]
unsafe-str-decode = []
schema = ["schemars"]

[lib]
name ="maxminddb"
Expand All @@ -28,6 +29,7 @@ log = "0.4"
serde = { version = "1.0", features = ["derive"] }
memchr = "2.4"
memmap2 = { version = "0.9.0", optional = true }
schemars = { version = "0.8.16",optional = true }

[dev-dependencies]
env_logger = "0.10"
Expand Down
31 changes: 31 additions & 0 deletions src/maxminddb/geoip2.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#[cfg(feature = "schema")]
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

/// GeoIP2 Country record
#[derive(Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct Country<'a> {
#[serde(borrow)]
pub continent: Option<country::Continent<'a>>,
Expand All @@ -13,6 +16,7 @@ pub struct Country<'a> {

/// GeoIP2 City record
#[derive(Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct City<'a> {
#[serde(borrow)]
pub city: Option<city::City<'a>>,
Expand All @@ -28,6 +32,7 @@ pub struct City<'a> {

/// GeoIP2 Enterprise record
#[derive(Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct Enterprise<'a> {
#[serde(borrow)]
pub city: Option<enterprise::City<'a>>,
Expand All @@ -43,6 +48,7 @@ pub struct Enterprise<'a> {

/// GeoIP2 ISP record
#[derive(Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct Isp<'a> {
pub autonomous_system_number: Option<u32>,
pub autonomous_system_organization: Option<&'a str>,
Expand All @@ -54,12 +60,14 @@ pub struct Isp<'a> {

/// GeoIP2 Connection-Type record
#[derive(Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct ConnectionType<'a> {
pub connection_type: Option<&'a str>,
}

/// GeoIP2 Anonymous Ip record
#[derive(Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct AnonymousIp {
pub is_anonymous: Option<bool>,
pub is_anonymous_vpn: Option<bool>,
Expand All @@ -71,37 +79,44 @@ pub struct AnonymousIp {

/// GeoIP2 DensityIncome record
#[derive(Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct DensityIncome {
pub average_income: Option<u32>,
pub population_density: Option<u32>,
}

/// GeoIP2 Domain record
#[derive(Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct Domain<'a> {
pub domain: Option<&'a str>,
}

/// GeoIP2 Asn record
#[derive(Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct Asn<'a> {
pub autonomous_system_number: Option<u32>,
pub autonomous_system_organization: Option<&'a str>,
}

/// Country model structs
pub mod country {
#[cfg(feature = "schema")]
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

#[derive(Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct Continent<'a> {
pub code: Option<&'a str>,
pub geoname_id: Option<u32>,
pub names: Option<BTreeMap<&'a str, &'a str>>,
}

#[derive(Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct Country<'a> {
pub geoname_id: Option<u32>,
pub is_in_european_union: Option<bool>,
Expand All @@ -110,6 +125,7 @@ pub mod country {
}

#[derive(Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct RepresentedCountry<'a> {
pub geoname_id: Option<u32>,
pub is_in_european_union: Option<bool>,
Expand All @@ -120,6 +136,7 @@ pub mod country {
}

#[derive(Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct Traits {
pub is_anonymous_proxy: Option<bool>,
pub is_satellite_provider: Option<bool>,
Expand All @@ -128,19 +145,23 @@ pub mod country {

/// Country model structs
pub mod city {
#[cfg(feature = "schema")]
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

pub use super::country::{Continent, Country, RepresentedCountry, Traits};

#[derive(Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct City<'a> {
pub geoname_id: Option<u32>,
#[serde(borrow)]
pub names: Option<BTreeMap<&'a str, &'a str>>,
}

#[derive(Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct Location<'a> {
pub accuracy_radius: Option<u16>,
pub latitude: Option<f64>,
Expand All @@ -150,11 +171,13 @@ pub mod city {
}

#[derive(Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct Postal<'a> {
pub code: Option<&'a str>,
}

#[derive(Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct Subdivision<'a> {
pub geoname_id: Option<u32>,
pub iso_code: Option<&'a str>,
Expand All @@ -164,12 +187,15 @@ pub mod city {

/// Enterprise model structs
pub mod enterprise {
#[cfg(feature = "schema")]
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

pub use super::country::{Continent, RepresentedCountry};

#[derive(Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct City<'a> {
pub confidence: Option<u8>,
pub geoname_id: Option<u32>,
Expand All @@ -178,6 +204,7 @@ pub mod enterprise {
}

#[derive(Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct Country<'a> {
pub confidence: Option<u8>,
pub geoname_id: Option<u32>,
Expand All @@ -187,6 +214,7 @@ pub mod enterprise {
}

#[derive(Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct Location<'a> {
pub accuracy_radius: Option<u16>,
pub latitude: Option<f64>,
Expand All @@ -196,12 +224,14 @@ pub mod enterprise {
}

#[derive(Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct Postal<'a> {
pub code: Option<&'a str>,
pub confidence: Option<u8>,
}

#[derive(Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct Subdivision<'a> {
pub confidence: Option<u8>,
pub geoname_id: Option<u32>,
Expand All @@ -210,6 +240,7 @@ pub mod enterprise {
}

#[derive(Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct Traits<'a> {
pub autonomous_system_number: Option<u32>,
pub autonomous_system_organization: Option<&'a str>,
Expand Down
Loading