place asset_tweak behind a feature

This commit is contained in:
juliancoffee 2021-06-08 00:09:29 +03:00
parent 3c34301947
commit 8f86f474e8
3 changed files with 131 additions and 134 deletions

View File

@ -6,7 +6,7 @@ code-quality:
script:
- ln -s /dockercache/target target
- rm -r target/debug/incremental/* || echo "all good" # TMP FIX FOR 2021-03-22-nightly
- cargo clippy --all-targets --locked --features="bin_csv,bin_bot" -- -D warnings
- cargo clippy --all-targets --locked --features="bin_csv,bin_bot,asset_tweak" -- -D warnings
- cargo fmt --all -- --check
security:

View File

@ -14,5 +14,7 @@ dot_vox = "4.0"
image = { version = "0.23.12", default-features = false, features = ["png"] }
tracing = "0.1"
[dev-dependencies]
serial_test = "0.5"
serial_test = {version = "0.5", optional = true}
[features]
asset_tweak = ["serial_test"]

View File

@ -261,6 +261,7 @@ impl Compound for Directory {
}
#[warn(clippy::pedantic)]
#[cfg(feature = "asset_tweak")]
pub mod asset_tweak {
// Return path to repository by searching 10 directories back
use super::{find_root, fs, Asset, AssetExt, Path, RonLoader};
@ -280,8 +281,6 @@ pub mod asset_tweak {
}
#[must_use]
/// NOTE: Don't use it in production code, it's debug only
///
/// # Usage
/// Create file with content which represent tweaked value
///
@ -299,13 +298,11 @@ pub mod asset_tweak {
///
/// # Panics
/// 1) If given `asset_specifier` does not exists
/// 2) If asseet is broken
pub fn tweak_expect<T>(specifier: &str) -> T
where
T: Clone + Sized + Send + Sync + 'static + DeserializeOwned,
{
if cfg!(not(any(debug_assertions, test))) {
tracing::warn!("AssetTweaker used in release build!");
}
let asset_specifier: &str = &format!("tweak.{}", specifier);
let handle = <AssetTweakWrapper<T> as AssetExt>::load_expect(asset_specifier);
let AssetTweakWrapper(value) = handle.read().clone();
@ -313,11 +310,13 @@ pub mod asset_tweak {
}
#[must_use]
/// NOTE: Don't use it in production code, it's debug only
///
/// # Usage
/// Will create file "assets/tweak/{specifier}.ron" if not exists.
/// If exists will read a value from such file.
/// Will create file "assets/tweak/{specifier}.ron" if not exists
/// and return passed `value`.
/// If file exists will read a value from such file.
///
/// In release builds (if `debug_assertions` == false) just returns passed
/// `value`
///
/// Example if you want to tweak integer value
/// ```no_run
@ -338,8 +337,7 @@ pub mod asset_tweak {
where
T: Clone + Sized + Send + Sync + 'static + DeserializeOwned + Serialize,
{
if cfg!(not(any(debug_assertions, test))) {
tracing::warn!("AssetTweaker used in release build!");
if cfg!(not(debug_assertions)) {
return value;
}
@ -365,14 +363,10 @@ pub mod asset_tweak {
value
}
}
}
#[cfg(test)]
mod tests {
use super::{
asset_tweak::{tweak_expect, tweak_expect_or_create},
find_root,
};
use super::{find_root, tweak_expect, tweak_expect_or_create};
use serial_test::serial;
use std::{
convert::AsRef,
@ -418,8 +412,8 @@ mod tests {
P: AsRef<Path> + Debug,
{
fn create(file: P) -> (Self, File) {
let f =
File::create(&file).unwrap_or_else(|_| panic!("failed to create file {:?}", &file));
let f = File::create(&file)
.unwrap_or_else(|_| panic!("failed to create file {:?}", &file));
(Self { file }, f)
}
@ -505,3 +499,4 @@ mod tests {
assert_eq!(x, 10);
}
}
}