use workspace

This commit is contained in:
biluohc 2018-07-02 22:17:10 +08:00
parent 8a48d41657
commit 5287df2fe7
9 changed files with 35 additions and 25 deletions

View File

@ -1,17 +1,2 @@
[package]
name = "ip2region"
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"]
[workspace]
members = ["binding/rust"]

17
binding/rust/Cargo.toml Normal file
View File

@ -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"]

View File

@ -10,7 +10,7 @@
复制下面的 toml 添加依赖即可使用,其 api 是 `memory_search`
只是目前 DB足有3.2M,还是有些感人的。
只是目前 DB 足有3.2M,还是有些感人的。
```toml
[dependencies.ip2region]

7
binding/rust/src/db.rs Normal file
View File

@ -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"
));

View File

@ -1,4 +1,4 @@
static DB_BYTES: &'static [u8] = include_bytes!("../data/ip2region.db");
use super::DB_BYTES;
lazy_static! {
static ref OWNED_IP_2_REGION: OwnedIp2Region = {
@ -13,4 +13,4 @@ lazy_static! {
pub fn memory_search(ip_str: &str) -> Result<IpInfo> {
OWNED_IP_2_REGION.memory_search(ip_str)
}
}

View File

@ -7,10 +7,14 @@ use std::fs::File;
use std::io::{self, Read, Seek, SeekFrom};
use std::{fmt, str};
mod db;
mod error;
pub use error::{Error, Result};
mod owned;
#[cfg(feature = "lazy")]
use db::DB_BYTES;
pub use db::DB_PATH;
#[cfg(feature = "lazy")]
pub use owned::memory_search;
pub use owned::{OwnedIp2Region, OwnedIpInfo};
@ -150,7 +154,8 @@ impl Ip2Region {
let m = (l + h) >> 1;
let p = self.first_index_ptr + m * INDEX_BLOCK_LENGTH;
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);
if ip < sip {
h = m - 1;
@ -299,8 +304,6 @@ impl Ip2Region {
mod tests {
use super::*;
const DB_PATH: &str = "data/ip2region.db";
#[test]
fn it_works() {
let mut ip2 = Ip2Region::new(DB_PATH).unwrap();

View File

@ -1,7 +1,5 @@
extern crate ip2region;
const DB_PATH: &str = "data/ip2region.db";
use ip2region::*;
use std::env;
use std::io::{self, *};