From c63cd1f11a4b1cc830b46e0a9d0d28f96400d17b Mon Sep 17 00:00:00 2001 From: Wyatt Herkamp Date: Tue, 21 Jun 2022 07:56:21 -0400 Subject: [PATCH 1/6] Allows you to remove the minecraft-data at compile time --- Cargo.toml | 11 ++++++++--- README.md | 5 +++++ src/lib.rs | 3 +++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ff562a8..6a5c01a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,11 @@ thiserror = "1.0.31" serde_json = "1.0.81" serde_derive = "1.0.137" serde = "1.0.137" -include_dir = "0.7.2" -itertools = "0.10.3" -lazy_static = "1.4.0" +include_dir = { version = "0.7.2", optional = true } +itertools = { version = "0.10.3", optional = true } +lazy_static = { version = "1.4.0", optional = true } + +[features] +default = ["include_data"] +include_data = ["include_dir", "itertools", "lazy_static"] + diff --git a/README.md b/README.md index c556095..bdace3a 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,11 @@ This repository is a rust library to access minecraft data. The data itself hosted in the [minecraft-data](https://github.com/PrismarineJS/minecraft-data) repository and included into the library at compile time. + +### Excluding the minecraft-data at compile time +By adding `default-features=false` to the dependency in your `Cargo.toml` file, you can exclude the minecraft-data from the library. + + ## Usage ```rust diff --git a/src/lib.rs b/src/lib.rs index 10bc226..c28e616 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,12 +4,15 @@ extern crate serde_derive; /// Provides data access methods +#[cfg(feature="include_data")] pub mod api; +#[cfg(feature="include_data")] pub(crate) mod data; /// Contains the type definitions for the data pub mod models; pub(crate) mod utils; +#[cfg(feature="include_data")] pub use api::Api; pub use utils::error::DataError; pub use utils::error::DataResult; From 47e95167ef29b0cf0c4034da6daddf675b1ef590 Mon Sep 17 00:00:00 2001 From: Wyatt Herkamp Date: Tue, 21 Jun 2022 09:59:16 -0400 Subject: [PATCH 2/6] Rename the feature --- Cargo.toml | 4 ++-- src/lib.rs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6a5c01a..6be4001 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,6 @@ itertools = { version = "0.10.3", optional = true } lazy_static = { version = "1.4.0", optional = true } [features] -default = ["include_data"] -include_data = ["include_dir", "itertools", "lazy_static"] +default = ["include-data"] +include-data = ["include_dir", "itertools", "lazy_static"] diff --git a/src/lib.rs b/src/lib.rs index c28e616..4fa7420 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,15 +4,15 @@ extern crate serde_derive; /// Provides data access methods -#[cfg(feature="include_data")] +#[cfg(feature="include-data")] pub mod api; -#[cfg(feature="include_data")] +#[cfg(feature="include-data")] pub(crate) mod data; /// Contains the type definitions for the data pub mod models; pub(crate) mod utils; -#[cfg(feature="include_data")] +#[cfg(feature="include-data")] pub use api::Api; pub use utils::error::DataError; pub use utils::error::DataResult; From 180dd339ab5d6428cc8856cac16840a80f524edc Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 21 Jun 2022 19:42:19 +0200 Subject: [PATCH 3/6] Add api feature Signed-off-by: trivernis --- Cargo.toml | 4 ++-- src/api/tests/mod.rs | 1 + src/data/mod.rs | 1 + src/lib.rs | 7 ++++--- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6be4001..047f517 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,6 @@ itertools = { version = "0.10.3", optional = true } lazy_static = { version = "1.4.0", optional = true } [features] -default = ["include-data"] +default = ["include-data", "api"] include-data = ["include_dir", "itertools", "lazy_static"] - +api = ["include-data"] diff --git a/src/api/tests/mod.rs b/src/api/tests/mod.rs index e6b8478..25646b0 100644 --- a/src/api/tests/mod.rs +++ b/src/api/tests/mod.rs @@ -1,3 +1,4 @@ +#![cfg(feature = "api")] use crate::api::versions::{available_versions, versions}; use crate::api::Api; use crate::models::version::Version; diff --git a/src/data/mod.rs b/src/data/mod.rs index b686921..fa6730c 100644 --- a/src/data/mod.rs +++ b/src/data/mod.rs @@ -1,3 +1,4 @@ +#![cfg(feature = "api")] mod datapaths; use crate::data::datapaths::Datapaths; diff --git a/src/lib.rs b/src/lib.rs index 4fa7420..e8f1d29 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,15 +4,16 @@ extern crate serde_derive; /// Provides data access methods -#[cfg(feature="include-data")] +#[cfg(feature = "api")] pub mod api; -#[cfg(feature="include-data")] +#[cfg(feature = "include-data")] pub(crate) mod data; /// Contains the type definitions for the data pub mod models; pub(crate) mod utils; -#[cfg(feature="include-data")] +#[cfg(feature = "api")] pub use api::Api; + pub use utils::error::DataError; pub use utils::error::DataResult; From 1695f3f477d6a818df902213681c5bef56c1bf5b Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 21 Jun 2022 19:47:38 +0200 Subject: [PATCH 4/6] Increment version, update readme and add workflow Signed-off-by: trivernis --- .github/workflows/test.yml | 39 ++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 +- README.md | 7 +++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/test.yml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..9092b17 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,39 @@ +name: Run checks and tests +on: + workflow_dispatch: + push: + branches: [ main, develop, feature/gh-actions ] + pull_request: + branches: [ main, develop ] + +env: + CARGO_TERM_COLOR: always + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + if: ${{ !env.ACT }} + + - name: Cache build data + if: ${{ !env.ACT }} + uses: actions/cache@v2 + with: + path: | + target + ~/.cargo/ + key: ${{ runner.os }}-cargo-${{ hashFiles('Cargo.lock') }} + restore-keys: | + ${{ runner.os }}-cargo- + - name: Fetch + run: cargo fetch + + - name: Check + run: cargo check --all-features + + - name: Lint + run: cargo clippy -- -D warnings + + - name: Test + run : cargo test --all-features \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 047f517..562a826 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "minecraft-data-rs" -version = "0.4.6" +version = "0.4.7" authors = ["trivernis "] edition = "2018" readme = "README.md" diff --git a/README.md b/README.md index bdace3a..0bc97b2 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,13 @@ for food in food { } ``` +## Features + +| Feature | Description | +| -------------|------------------------------------------------------------| +| include-data | includes the whole minecraft-data repository in the binary | +| api | enables the api to query minecraft data | + # License This project is Licensed under MIT. \ No newline at end of file From fd218f5419c9a39051b3d842ea399a11b15895c6 Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 21 Jun 2022 19:51:30 +0200 Subject: [PATCH 5/6] Add submodules handling to test workflow Signed-off-by: trivernis --- .github/workflows/test.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9092b17..ec79f21 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -15,6 +15,8 @@ jobs: steps: - uses: actions/checkout@v2 if: ${{ !env.ACT }} + with: + submodules: recursive - name: Cache build data if: ${{ !env.ACT }} From 0b32ffbf302fe3a8c585cb78013d0b75dc73d8ea Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 21 Jun 2022 19:56:52 +0200 Subject: [PATCH 6/6] Fix style issues Signed-off-by: trivernis --- src/api/items.rs | 6 +----- src/api/versions.rs | 2 +- src/data/mod.rs | 23 ++++++++++------------- 3 files changed, 12 insertions(+), 19 deletions(-) diff --git a/src/api/items.rs b/src/api/items.rs index 411df08..9c62989 100644 --- a/src/api/items.rs +++ b/src/api/items.rs @@ -33,10 +33,6 @@ impl Items { /// Returns the items indexed by ID pub fn items(&self) -> DataResult> { - Ok(self - .items_array()? - .into_iter() - .map(|i| (i.id.clone(), i)) - .collect()) + Ok(self.items_array()?.into_iter().map(|i| (i.id, i)).collect()) } } diff --git a/src/api/versions.rs b/src/api/versions.rs index 516ce10..4a1fe57 100644 --- a/src/api/versions.rs +++ b/src/api/versions.rs @@ -29,7 +29,7 @@ pub fn latest_stable() -> DataResult { .into_iter() .filter_map(|v| { let version_string = v.clone(); - let mut parts = version_string.split("."); + let mut parts = version_string.split('.'); Some(( v, diff --git a/src/data/mod.rs b/src/data/mod.rs index fa6730c..c5dfa08 100644 --- a/src/data/mod.rs +++ b/src/data/mod.rs @@ -41,9 +41,9 @@ pub static VERSIONS_FILE: &str = "versions"; pub fn get_common_file(filename: &str) -> DataResult { MINECRAFT_DATA .get_file(format!("pc/common/{}.json", filename)) - .ok_or(DataError::NotFoundError(filename.to_string()))? + .ok_or_else(|| DataError::NotFoundError(filename.to_string()))? .contents_utf8() - .ok_or(DataError::InvalidEncodingError(filename.to_string())) + .ok_or_else(|| DataError::InvalidEncodingError(filename.to_string())) .map(|d| d.to_string()) } @@ -52,12 +52,11 @@ pub fn get_version_specific_file(version: &Version, filename: &str) -> DataResul let path = get_path(version, filename)?; MINECRAFT_DATA .get_file(format!("{}/{}.json", path, filename)) - .ok_or(DataError::NotFoundError(format!( - "{}/{}", - version.minecraft_version, filename - )))? + .ok_or_else(|| { + DataError::NotFoundError(format!("{}/{}", version.minecraft_version, filename)) + })? .contents_utf8() - .ok_or(DataError::InvalidEncodingError(filename.to_string())) + .ok_or_else(|| DataError::InvalidEncodingError(filename.to_string())) .map(|d| d.to_string()) } @@ -69,20 +68,18 @@ pub fn get_path(version: &Version, filename: &str) -> DataResult { PATHS .pc .get(&version.minecraft_version) - .ok_or(DataError::NotFoundError(version.minecraft_version.clone()))? + .ok_or_else(|| DataError::NotFoundError(version.minecraft_version.clone()))? .get(filename) .cloned() - .ok_or(DataError::NotFoundError(filename.to_string())) + .ok_or_else(|| DataError::NotFoundError(filename.to_string())) } /// Returns the parsed data paths fn get_datapaths() -> DataResult { let content = MINECRAFT_DATA .get_file("dataPaths.json") - .ok_or(DataError::NotFoundError("dataPaths.json".to_string()))? + .ok_or_else(|| DataError::NotFoundError("dataPaths.json".to_string()))? .contents_utf8() - .ok_or(DataError::InvalidEncodingError( - "dataPaths.json".to_string(), - ))?; + .ok_or_else(|| DataError::InvalidEncodingError("dataPaths.json".to_string()))?; serde_json::from_str::(content).map_err(DataError::from) }