diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..ec79f21 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,41 @@ +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 }} + with: + submodules: recursive + + - 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 ff562a8..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" @@ -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", "api"] +include-data = ["include_dir", "itertools", "lazy_static"] +api = ["include-data"] diff --git a/README.md b/README.md index c556095..0bc97b2 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 @@ -21,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 diff --git a/src/api/items.rs b/src/api/items.rs index 400f758..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, i)) - .collect()) + Ok(self.items_array()?.into_iter().map(|i| (i.id, i)).collect()) } } diff --git a/src/api/tests/mod.rs b/src/api/tests/mod.rs index 89437cc..55b4e36 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 014a878..c5dfa08 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; @@ -21,6 +22,7 @@ pub static ITEMS_FILE: &str = "items"; pub static LOGIN_PACKET_FILE: &str = "loginPacket"; #[allow(unused)] pub static MATERIALS_FILE: &str = "materials"; +#[allow(unused)] pub static PROTOCOL_FILE: &str = "protocol"; pub static RECIPES_FILE: &str = "recipes"; #[allow(unused)] @@ -39,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_else(||DataError::NotFoundError(filename.to_string()))? + .ok_or_else(|| DataError::NotFoundError(filename.to_string()))? .contents_utf8() - .ok_or_else(||DataError::InvalidEncodingError(filename.to_string())) + .ok_or_else(|| DataError::InvalidEncodingError(filename.to_string())) .map(|d| d.to_string()) } @@ -50,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_else(||DataError::NotFoundError(format!( - "{}/{}", - version.minecraft_version, filename - )))? + .ok_or_else(|| { + DataError::NotFoundError(format!("{}/{}", version.minecraft_version, filename)) + })? .contents_utf8() - .ok_or_else(||DataError::InvalidEncodingError(filename.to_string())) + .ok_or_else(|| DataError::InvalidEncodingError(filename.to_string())) .map(|d| d.to_string()) } @@ -67,20 +68,18 @@ pub fn get_path(version: &Version, filename: &str) -> DataResult { PATHS .pc .get(&version.minecraft_version) - .ok_or_else(||DataError::NotFoundError(version.minecraft_version.clone()))? + .ok_or_else(|| DataError::NotFoundError(version.minecraft_version.clone()))? .get(filename) .cloned() - .ok_or_else(||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_else(||DataError::NotFoundError("dataPaths.json".to_string()))? + .ok_or_else(|| DataError::NotFoundError("dataPaths.json".to_string()))? .contents_utf8() - .ok_or_else(||DataError::InvalidEncodingError( - "dataPaths.json".to_string(), - ))?; + .ok_or_else(|| DataError::InvalidEncodingError("dataPaths.json".to_string()))?; serde_json::from_str::(content).map_err(DataError::from) } diff --git a/src/lib.rs b/src/lib.rs index 10bc226..e8f1d29 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,12 +4,16 @@ extern crate serde_derive; /// Provides data access methods +#[cfg(feature = "api")] 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 = "api")] pub use api::Api; + pub use utils::error::DataError; pub use utils::error::DataResult;