From 5287df2fe7a428d92784911c3bd5562c19de71bc Mon Sep 17 00:00:00 2001 From: biluohc Date: Mon, 2 Jul 2018 22:17:10 +0800 Subject: [PATCH] use workspace --- Cargo.toml | 19 ++----------------- binding/rust/Cargo.toml | 17 +++++++++++++++++ binding/{rust.md => rust/readme.md} | 2 +- binding/rust/src/db.rs | 7 +++++++ {src => binding/rust/src}/error.rs | 0 {src => binding/rust/src}/lazy.rs | 4 ++-- {src => binding/rust/src}/lib.rs | 9 ++++++--- {src => binding/rust/src}/main.rs | 2 -- {src => binding/rust/src}/owned.rs | 0 9 files changed, 35 insertions(+), 25 deletions(-) create mode 100644 binding/rust/Cargo.toml rename binding/{rust.md => rust/readme.md} (89%) create mode 100644 binding/rust/src/db.rs rename {src => binding/rust/src}/error.rs (100%) rename {src => binding/rust/src}/lazy.rs (85%) rename {src => binding/rust/src}/lib.rs (98%) rename {src => binding/rust/src}/main.rs (98%) rename {src => binding/rust/src}/owned.rs (100%) diff --git a/Cargo.toml b/Cargo.toml index 6a21a71..a0427e3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,17 +1,2 @@ -[package] -name = "ip2region" -version = "0.1.0" -authors = ["biluohc "] - -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"] diff --git a/binding/rust/Cargo.toml b/binding/rust/Cargo.toml new file mode 100644 index 0000000..03239da --- /dev/null +++ b/binding/rust/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "ip2region" +version = "0.1.0" +authors = ["biluohc "] + +include = ["./*", "../../data/ip2region.db", "../../Cargo.toml"] + +[[bin]] +name="ip2region" +path="src/main.rs" + +[dependencies] +lazy_static ={ version = "^1", optional = true } + +[features] +lazy = ["lazy_static"] + diff --git a/binding/rust.md b/binding/rust/readme.md similarity index 89% rename from binding/rust.md rename to binding/rust/readme.md index 74299e7..4b6d8dd 100644 --- a/binding/rust.md +++ b/binding/rust/readme.md @@ -10,7 +10,7 @@ 复制下面的 toml 添加依赖即可使用,其 api 是 `memory_search`。 -只是目前 DB足有3.2M,还是有些感人的。 +只是目前 DB 足有3.2M,还是有些感人的。 ```toml [dependencies.ip2region] diff --git a/binding/rust/src/db.rs b/binding/rust/src/db.rs new file mode 100644 index 0000000..284475d --- /dev/null +++ b/binding/rust/src/db.rs @@ -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" +)); diff --git a/src/error.rs b/binding/rust/src/error.rs similarity index 100% rename from src/error.rs rename to binding/rust/src/error.rs diff --git a/src/lazy.rs b/binding/rust/src/lazy.rs similarity index 85% rename from src/lazy.rs rename to binding/rust/src/lazy.rs index eaed09c..7a576fd 100644 --- a/src/lazy.rs +++ b/binding/rust/src/lazy.rs @@ -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 { OWNED_IP_2_REGION.memory_search(ip_str) -} \ No newline at end of file +} diff --git a/src/lib.rs b/binding/rust/src/lib.rs similarity index 98% rename from src/lib.rs rename to binding/rust/src/lib.rs index 8919887..7d198bb 100644 --- a/src/lib.rs +++ b/binding/rust/src/lib.rs @@ -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(); diff --git a/src/main.rs b/binding/rust/src/main.rs similarity index 98% rename from src/main.rs rename to binding/rust/src/main.rs index 15ac37c..27b7505 100644 --- a/src/main.rs +++ b/binding/rust/src/main.rs @@ -1,7 +1,5 @@ extern crate ip2region; -const DB_PATH: &str = "data/ip2region.db"; - use ip2region::*; use std::env; use std::io::{self, *}; diff --git a/src/owned.rs b/binding/rust/src/owned.rs similarity index 100% rename from src/owned.rs rename to binding/rust/src/owned.rs