use workspace
This commit is contained in:
parent
8a48d41657
commit
5287df2fe7
19
Cargo.toml
19
Cargo.toml
|
|
@ -1,17 +1,2 @@
|
||||||
[package]
|
[workspace]
|
||||||
name = "ip2region"
|
members = ["binding/rust"]
|
||||||
version = "0.1.0"
|
|
||||||
authors = ["biluohc <biluohc@qq.com>"]
|
|
||||||
|
|
||||||
include = ["src/**/*", "Cargo.toml", "data/ip2region.db", "binding/rust.md"]
|
|
||||||
|
|
||||||
[[bin]]
|
|
||||||
name="ip2region"
|
|
||||||
path="src/main.rs"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
lazy_static ={ version = "^1", optional = true }
|
|
||||||
|
|
||||||
[features]
|
|
||||||
lazy = ["lazy_static"]
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,17 @@
|
||||||
|
[package]
|
||||||
|
name = "ip2region"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["biluohc <biluohc@qq.com>"]
|
||||||
|
|
||||||
|
include = ["./*", "../../data/ip2region.db", "../../Cargo.toml"]
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name="ip2region"
|
||||||
|
path="src/main.rs"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
lazy_static ={ version = "^1", optional = true }
|
||||||
|
|
||||||
|
[features]
|
||||||
|
lazy = ["lazy_static"]
|
||||||
|
|
||||||
|
|
@ -10,7 +10,7 @@
|
||||||
|
|
||||||
复制下面的 toml 添加依赖即可使用,其 api 是 `memory_search`。
|
复制下面的 toml 添加依赖即可使用,其 api 是 `memory_search`。
|
||||||
|
|
||||||
只是目前 DB足有3.2M,还是有些感人的。
|
只是目前 DB 足有3.2M,还是有些感人的。
|
||||||
|
|
||||||
```toml
|
```toml
|
||||||
[dependencies.ip2region]
|
[dependencies.ip2region]
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
pub const DB_PATH: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/../../data/ip2region.db");
|
||||||
|
|
||||||
|
#[cfg(feature = "lazy")]
|
||||||
|
pub static DB_BYTES: &'static [u8] = include_bytes!(concat!(
|
||||||
|
env!("CARGO_MANIFEST_DIR"),
|
||||||
|
"/../../data/ip2region.db"
|
||||||
|
));
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
static DB_BYTES: &'static [u8] = include_bytes!("../data/ip2region.db");
|
use super::DB_BYTES;
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref OWNED_IP_2_REGION: OwnedIp2Region = {
|
static ref OWNED_IP_2_REGION: OwnedIp2Region = {
|
||||||
|
|
@ -13,4 +13,4 @@ lazy_static! {
|
||||||
|
|
||||||
pub fn memory_search(ip_str: &str) -> Result<IpInfo> {
|
pub fn memory_search(ip_str: &str) -> Result<IpInfo> {
|
||||||
OWNED_IP_2_REGION.memory_search(ip_str)
|
OWNED_IP_2_REGION.memory_search(ip_str)
|
||||||
}
|
}
|
||||||
|
|
@ -7,10 +7,14 @@ use std::fs::File;
|
||||||
use std::io::{self, Read, Seek, SeekFrom};
|
use std::io::{self, Read, Seek, SeekFrom};
|
||||||
use std::{fmt, str};
|
use std::{fmt, str};
|
||||||
|
|
||||||
|
mod db;
|
||||||
mod error;
|
mod error;
|
||||||
pub use error::{Error, Result};
|
pub use error::{Error, Result};
|
||||||
mod owned;
|
mod owned;
|
||||||
#[cfg(feature = "lazy")]
|
#[cfg(feature = "lazy")]
|
||||||
|
use db::DB_BYTES;
|
||||||
|
pub use db::DB_PATH;
|
||||||
|
#[cfg(feature = "lazy")]
|
||||||
pub use owned::memory_search;
|
pub use owned::memory_search;
|
||||||
pub use owned::{OwnedIp2Region, OwnedIpInfo};
|
pub use owned::{OwnedIp2Region, OwnedIpInfo};
|
||||||
|
|
||||||
|
|
@ -150,7 +154,8 @@ impl Ip2Region {
|
||||||
let m = (l + h) >> 1;
|
let m = (l + h) >> 1;
|
||||||
let p = self.first_index_ptr + m * INDEX_BLOCK_LENGTH;
|
let p = self.first_index_ptr + m * INDEX_BLOCK_LENGTH;
|
||||||
self.db_file.seek(SeekFrom::Start(p as u64))?;
|
self.db_file.seek(SeekFrom::Start(p as u64))?;
|
||||||
self.db_file.read_exact(&mut buf[0..INDEX_BLOCK_LENGTH as usize])?;
|
self.db_file
|
||||||
|
.read_exact(&mut buf[0..INDEX_BLOCK_LENGTH as usize])?;
|
||||||
let sip = get_u32(&buf[..INDEX_BLOCK_LENGTH as usize], 0);
|
let sip = get_u32(&buf[..INDEX_BLOCK_LENGTH as usize], 0);
|
||||||
if ip < sip {
|
if ip < sip {
|
||||||
h = m - 1;
|
h = m - 1;
|
||||||
|
|
@ -299,8 +304,6 @@ impl Ip2Region {
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
const DB_PATH: &str = "data/ip2region.db";
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_works() {
|
fn it_works() {
|
||||||
let mut ip2 = Ip2Region::new(DB_PATH).unwrap();
|
let mut ip2 = Ip2Region::new(DB_PATH).unwrap();
|
||||||
|
|
@ -1,7 +1,5 @@
|
||||||
extern crate ip2region;
|
extern crate ip2region;
|
||||||
|
|
||||||
const DB_PATH: &str = "data/ip2region.db";
|
|
||||||
|
|
||||||
use ip2region::*;
|
use ip2region::*;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::io::{self, *};
|
use std::io::{self, *};
|
||||||
Loading…
Reference in New Issue