17 releases (10 breaking)
0.11.0 | Feb 18, 2023 |
---|---|
0.9.0 | Feb 1, 2023 |
0.8.0 | Dec 31, 2022 |
0.7.2 | Sep 23, 2022 |
0.5.3 | Mar 18, 2022 |
#10 in #steam
61 downloads per month
74KB
2K
SLoC
tf2-price
Utilities for Team Fortress 2 item pricing.
use tf2_price::{Currencies, ListingCurrencies, refined, scrap};
fn main() {
let currencies = Currencies {
keys: 5,
metal: refined!(2) + scrap!(3),
};
// 2.33 refined - metal values are counted in weapons.
assert_eq!(
currencies.metal,
42
);
// String conversions.
assert_eq!(
currencies.to_string(),
"5 keys, 2.33 ref"
);
assert_eq!(
Currencies::try_from("5 keys, 2.33 ref").unwrap(),
*¤cies
);
// Serde deserialization.
assert_eq!(
serde_json::from_str::<Currencies>(r#"{"keys":5,"metal":2.33}"#).unwrap(),
*¤cies
);
let golden_frying_pan = Currencies { keys: 3000, metal: 0 };
// Arithmetic.
assert_eq!(
&golden_frying_pan * 2,
Currencies { keys: 6000, metal: 0 },
);
assert_eq!(
&golden_frying_pan * 2.5,
Currencies { keys: 7500, metal: 0 },
);
assert_eq!(
&golden_frying_pan + Currencies { keys: 0, metal: 2 },
Currencies { keys: 3000, metal: 2 },
);
// There are also some helper methods for checking for integer overflow/underflow.
assert_eq!(
Currencies { keys: 2, metal: 0 }.checked_add(&Currencies { keys: i64::MAX, metal: 0 }),
None,
);
assert_eq!(Currencies { keys: 2, metal: 0 }.checked_mul(i64::MAX), None);
// For currencies which require floating point key values, use ListingCurrencies.
let currencies = ListingCurrencies {
keys: 1.5,
metal: 0,
};
// Arithmetic with standard Currencies objects still works.
assert_eq!(
ListingCurrencies { keys: 1.5, metal: 0 } - Currencies { keys: 1, metal: 0 },
ListingCurrencies { keys: 0.5, metal: 0 },
);
// Due to the lossy nature of converting floats to integers, arithmatic in an inverse
// manner (Currencies - ListingCurrencies) is not supported.
// Conversions to Currencies are supported.
assert!(
Currencies::try_from(ListingCurrencies { keys: 1.0, metal: 0 }).is_ok()
);
// Fails if the key value holds a fractional number.
assert!(
Currencies::try_from(ListingCurrencies { keys: 1.5, metal: 0 }).is_err()
);
}
License
MIT
Dependencies
~0.8–1.3MB
~31K SLoC