我决定开始学习Rust,我还没有完成他们的书,但是我正在尝试构建和运行其他项目,以便可以从源代码中学习。我现在对maxmind-rust板条箱感兴趣,尤其是我想从.mmdb文件中检索国家,城市和asn值。
[我试图将结构maxmind::geoip2::Country
转换为字符串并使用json板条箱,但导致了我无法修复的错误。
使用的代码:
use maxminddb::geoip2;
use std::net::IpAddr;
use std::str::FromStr;
fn main()
{
let mmdb_file = maxminddb::Reader::open("C:\\path\\to\\GeoLite2-City.mmdb").unwrap();
let ip_addr: IpAddr = FromStr::from_str("8.8.8.8").unwrap();
let geoip2_country: geoip2::Country = mmdb_file.lookup(ip_addr).unwrap();
println!("{:?}", geoip2_country);
}
输出为:
Country
{
continent: Some(Continent
{
code: Some("NA"),
geoname_id: Some(6255149),
names: Some(
{
"de": "Nordamerika",
"en": "North America",
"es": "Norteam?rica",
"fr": "Am?rique du Nord",
"ja": "?????",
"pt-BR": "Am?rica do Norte",
"ru": "???????? ???????",
"zh-CN": "???"
})
}),
country: Some(Country
{
geoname_id: Some(6252001),
iso_code: Some("US"),
names: Some(
{
"de": "USA",
"en": "United States",
"es": "Estados Unidos",
"fr": "?tats-Unis",
"ja": "???????",
"pt-BR": "Estados Unidos",
"ru": "???",
"zh-CN": "??"
})
}),
registered_country: Some(Country
{
geoname_id: Some(6252001),
iso_code: Some("US"),
names: Some(
{
"de": "USA",
"en": "United States",
"es": "Estados Unidos",
"fr": "?tats-Unis",
"ja": "???????",
"pt-BR": "Estados Unidos",
"ru": "???",
"zh-CN": "??"
})
}),
represented_country: None,
traits: None
}
这是maxminddb :: geoip2 :: Country结构(http://oschwald.github.io/maxminddb-rust/maxminddb/geoip2/struct.Country.html)
将代码的最后一行更改为
println!("{:?}", geoip2_country.country);
仅输出country
字段:
country: Some(Country
{
geoname_id: Some(6252001),
iso_code: Some("US"),
names: Some(
{
"de": "USA",
"en": "United States",
"es": "Estados Unidos",
"fr": "?tats-Unis",
"ja": "???????",
"pt-BR": "Estados Unidos",
"ru": "???",
"zh-CN": "??"
})
}),
但是查看maxminddb :: geoip2 :: model :: Country(http://oschwald.github.io/maxminddb-rust/maxminddb/geoip2/model/struct.Country.html)的结构,如果要获取该结构及其对(language_code,country_name),我非常困惑“ en”键的国家名称。
您可以看一下测试的编写方式,它将使您了解如何提取所需的信息:
https://github.com/oschwald/maxminddb-rust/blob/master/src/maxminddb/reader_test.rs
这应该可以实现您的期望:
let country: geoip2::model::Country = geoip2_country.country.unwrap();
println!("{:?}", country.names.unwrap()["en"]);