1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
use std::{
cmp::Ordering,
collections::BTreeMap,
fmt::{Debug, Display, Formatter, Result as FmtResult},
iter::FromIterator,
ops::BitOr,
};
use crate::{
generated_mods::{GameMod, GameModIntermode, GameModOrder},
intersection::{GameModsIntersection, IntersectionInner},
iter::{GameModsIter, GameModsIterMut, IntoGameModsIter},
Acronym, GameMode, GameModsIntermode, GameModsLegacy,
};
/// Combination of [`GameMod`]s.
#[derive(Clone, Default, PartialEq)]
pub struct GameMods {
pub(crate) inner: BTreeMap<GameModOrder, GameMod>,
}
impl GameMods {
/// Returns empty mods i.e. "`NoMod`"
pub const fn new() -> Self {
Self {
inner: BTreeMap::new(),
}
}
/// Return the accumulated bit values of all contained mods.
///
/// Mods that don't have bit values will be ignored.
/// See <https://github.com/ppy/osu-api/wiki#mods>
///
/// # Example
/// ```rust
/// # use rosu_mods::{GameMod, GameMods};
/// # let hdhrdtwu: GameMods = [
/// # GameMod::HiddenOsu(Default::default()),
/// # GameMod::HardRockOsu(Default::default()),
/// # GameMod::DoubleTimeOsu(Default::default()),
/// # GameMod::WindUpOsu(Default::default()),
/// # ].into_iter().collect();
/// # /*
/// let hdhrdtwu = mods!(Osu: HD HR DT WU);
/// # */
/// assert_eq!(hdhrdtwu.bits(), 8 + 16 + 64);
/// ```
pub fn bits(&self) -> u32 {
self.inner
.values()
.filter_map(GameMod::bits)
.fold(0, u32::bitor)
}
/// Return the accumulated bit values of all contained mods.
///
/// If any contained mod has no bit value `None` is returned.
/// See <https://github.com/ppy/osu-api/wiki#mods>
///
/// # Example
/// ```rust
/// # use rosu_mods::{GameMod, GameMods};
/// # let hdhrdt: GameMods = [
/// # GameMod::HiddenOsu(Default::default()),
/// # GameMod::HardRockOsu(Default::default()),
/// # GameMod::DoubleTimeOsu(Default::default()),
/// # ].into_iter().collect();
/// # let hdhrdtwu: GameMods = [
/// # GameMod::HiddenOsu(Default::default()),
/// # GameMod::HardRockOsu(Default::default()),
/// # GameMod::DoubleTimeOsu(Default::default()),
/// # GameMod::WindUpOsu(Default::default()),
/// # ].into_iter().collect();
/// # /*
/// let hdhrdt = mods!(Osu: HD HR DT);
/// # */
/// assert_eq!(hdhrdt.checked_bits(), Some(8 + 16 + 64));
///
/// # /*
/// let hdhrdtwu = mods!(Osu: HD HR DT WU);
/// # */
/// assert_eq!(hdhrdtwu.checked_bits(), None);
/// ```
pub fn checked_bits(&self) -> Option<u32> {
self.inner
.values()
.map(GameMod::bits)
.try_fold(0, |bits, next| Some(next? | bits))
}
/// Returns `true` if no mods are contained.
///
/// # Example
/// ```rust
/// use rosu_mods::{GameMod, GameMods};
///
/// let mut mods = GameMods::new();
/// assert!(mods.is_empty());
///
/// mods.insert(GameMod::HiddenOsu(Default::default()));
/// assert!(!mods.is_empty());
/// ```
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
/// Returns the amount of contained mods.
///
/// # Example
/// ```rust
/// use rosu_mods::{GameMod, GameMods};
///
/// # let hdhrdt: GameMods = [
/// # GameMod::HiddenCatch(Default::default()),
/// # GameMod::HardRockCatch(Default::default()),
/// # GameMod::DoubleTimeCatch(Default::default()),
/// # ].into_iter().collect();
/// # /*
/// let hdhrdt = mods!(Catch: HD HR DT);
/// # */
/// assert_eq!(hdhrdt.len(), 3);
///
/// let mut nm = GameMods::new();
/// assert_eq!(nm.len(), 0);
/// assert_eq!(nm.to_string(), "NM");
/// ```
pub fn len(&self) -> usize {
self.inner.len()
}
/// Add a [`GameMod`]
///
/// # Example
/// ```rust
/// use rosu_mods::{GameMod, GameMods};
///
/// let mut mods = GameMods::new();
/// assert_eq!(mods.to_string(), "NM");
///
/// mods.insert(GameMod::TraceableOsu(Default::default()));
/// assert_eq!(mods.to_string(), "TC");
///
/// mods.insert(GameMod::HardRockOsu(Default::default()));
/// assert_eq!(mods.to_string(), "HRTC");
/// ```
pub fn insert(&mut self, gamemod: GameMod) {
self.inner.insert(GameModOrder::from(&gamemod), gamemod);
}
/// Check whether a given [`GameMod`] is contained.
///
/// # Example
/// ```rust
/// use rosu_mods::GameMod;
///
/// # let hd = rosu_mods::GameMods::from(GameMod::HiddenTaiko(Default::default()));
/// # /*
/// let hd = mods!(Taiko: HD);
/// # */
/// assert!(hd.contains(&GameMod::HiddenTaiko(Default::default())));
/// assert!(!hd.contains(&GameMod::HiddenOsu(Default::default())));
/// ```
pub fn contains(&self, gamemod: &GameMod) -> bool {
self.inner.contains_key(&GameModOrder::from(gamemod))
}
/// Check whether a given [`GameModIntermode`] is contained.
///
/// # Example
/// ```rust
/// use rosu_mods::GameModIntermode;
///
/// # let hd = rosu_mods::GameMods::from(rosu_mods::GameMod::HiddenTaiko(Default::default()));
/// # /*
/// let hd = mods!(Taiko: HD);
/// # */
/// assert!(hd.contains_intermode(GameModIntermode::Hidden));
/// assert!(!hd.contains_intermode(GameModIntermode::HardRock));
/// ```
pub fn contains_intermode<M>(&self, gamemod: M) -> bool
where
GameModIntermode: From<M>,
{
self.inner.contains_key(&GameModIntermode::from(gamemod))
}
/// Check whether any of the given mods are contained.
///
/// Note that this method does not consider the mods' modes so it could
/// return `true` even if it's a different mode.
///
/// # Example
/// ```rust
/// use rosu_mods::mods;
///
/// # use rosu_mods::{GameMod, GameMods};
/// # let hd = GameMods::from(GameMod::HiddenTaiko(Default::default()));
/// # /*
/// let hd = mods!(Taiko: HD);
/// # */
///
/// assert!(hd.contains_any(mods!(HD HR)));
/// assert!(!hd.contains_any(mods!(HR DT)));
///
/// // Careful: It returns `true` even if it's a different mode
/// # assert!(hd.contains_any(GameMods::from(GameMod::HiddenOsu(Default::default()))));
/// # /*
/// assert!(hd.contains_any(mods!(Osu: HD)));
/// # */
/// ```
pub fn contains_any<I, M>(&self, mods: I) -> bool
where
I: IntoIterator<Item = M>,
GameModIntermode: From<M>,
{
mods.into_iter()
.any(|gamemod| self.contains_intermode(gamemod))
}
/// Check whether a given [`Acronym`] is contained.
///
/// # Example
/// ```rust
/// use rosu_mods::Acronym;
///
/// # use rosu_mods::{GameMod, GameMods};
/// # let mods: GameMods = [
/// # GameMod::NoFailOsu(Default::default()),
/// # GameMod::DoubleTimeOsu(Default::default()),
/// # ].into_iter().collect();
/// # /*
/// let mods = mods!(Osu: NF DT);
/// # */
///
/// let nf = "NF".parse::<Acronym>().unwrap();
/// assert!(mods.contains_acronym(nf));
///
/// let hd = "HD".parse::<Acronym>().unwrap();
/// assert!(!mods.contains_acronym(hd));
/// ```
pub fn contains_acronym(&self, acronym: Acronym) -> bool {
self.inner
.values()
.any(|gamemod| gamemod.acronym() == acronym)
|| (self.is_empty() && acronym.as_str() == "NM")
}
/// Remove a [`GameMod`] and return whether it was contained.
///
/// # Example
/// ```
/// use rosu_mods::{GameMod, GameMods};
///
/// # let mut mods: GameMods = [
/// # GameMod::DoubleTimeMania(Default::default()),
/// # GameMod::MirrorMania(Default::default())
/// # ].into_iter().collect();
/// # /*
/// let mut mods: GameMods = mods!(Mania: DT MR);
/// #*/
///
/// assert!(mods.remove(&GameMod::MirrorMania(Default::default())));
/// assert_eq!(mods.to_string(), "DT");
/// assert!(!mods.remove(&GameMod::DoubleTimeCatch(Default::default())));
/// ```
pub fn remove(&mut self, gamemod: &GameMod) -> bool {
self.inner.remove(&GameModOrder::from(gamemod)).is_some()
}
/// Remove a gamemod and return whether it was contained.
///
/// If the same gamemod is contained for multiple modes, only one of them will be removed.
///
/// # Example
/// ```
/// use rosu_mods::{mods, GameModIntermode, GameMod, GameMods};
///
/// let mut mods: GameMods = [
/// GameMod::HiddenOsu(Default::default()),
/// GameMod::HiddenTaiko(Default::default()),
/// GameMod::HardRockOsu(Default::default()),
/// ].into_iter().collect();
///
/// assert_eq!(mods.to_string(), "HDHRHD");
///
/// assert!(mods.remove_intermode(GameModIntermode::Hidden));
/// assert_eq!(mods.to_string(), "HRHD");
/// assert!(!mods.remove_intermode(GameModIntermode::DoubleTime));
/// ```
pub fn remove_intermode<M>(&mut self, gamemod: M) -> bool
where
GameModIntermode: From<M>,
{
self.inner
.remove(&GameModIntermode::from(gamemod))
.is_some()
}
/// Remove all mods contained in the iterator.
///
/// # Example
/// ```
/// use rosu_mods::{mods, GameMod, GameMods};
///
/// # let mut mods: GameMods = [
/// # GameMod::HiddenOsu(Default::default()),
/// # GameMod::HardRockOsu(Default::default()),
/// # GameMod::WiggleOsu(Default::default()),
/// # GameMod::DoubleTimeOsu(Default::default()),
/// # GameMod::BarrelRollOsu(Default::default()),
/// # ].into_iter().collect();
/// # /*
/// let mut mods: GameMods = mods!(Osu: HD HR WG DT BR);
/// # */
///
/// mods.remove_all([
/// GameMod::HiddenOsu(Default::default()),
/// GameMod::EasyOsu(Default::default())
/// ].iter());
/// assert_eq!(mods.to_string(), "HRDTBRWG");
///
/// mods.remove_all(mods!(Osu: NF WG).iter());
/// assert_eq!(mods.to_string(), "HRDTBR")
/// ```
pub fn remove_all<'m, I>(&mut self, mods: I)
where
I: Iterator<Item = &'m GameMod>,
{
for gamemod in mods {
self.remove(gamemod);
}
}
/// Remove all mods contained in the iterator.
///
/// If the same gamemod is contained for multiple modes, each occurence of the gamemod
/// in the iterator will remove only one of the contained gamemods.
///
/// # Example
/// ```
/// use rosu_mods::{mods, GameMod, GameMods};
///
/// let mut mods: GameMods = [
/// GameMod::HiddenOsu(Default::default()),
/// GameMod::HardRockOsu(Default::default()),
/// GameMod::HardRockCatch(Default::default()),
/// GameMod::WiggleOsu(Default::default()),
/// GameMod::DoubleTimeOsu(Default::default()),
/// GameMod::BarrelRollOsu(Default::default()),
/// ].into_iter().collect();
///
/// assert_eq!(mods.to_string(), "HDHRDTBRWGHR");
/// mods.remove_all_intermode(mods!(HD HR WG));
/// assert_eq!(mods.to_string(), "DTBRHR");
/// ```
pub fn remove_all_intermode<I, M>(&mut self, mods: I)
where
I: IntoIterator<Item = M>,
GameModIntermode: From<M>,
{
for gamemod in mods {
self.remove_intermode(gamemod);
}
}
/// Returns an iterator over all mods that appear in both [`GameMods`].
///
/// # Example
/// ```rust
/// use rosu_mods::GameMods;
///
/// # use rosu_mods::GameMod;
/// # let hd = GameMods::from(GameMod::HiddenCatch(Default::default()));
/// # let hdhr: GameMods = [
/// # GameMod::HiddenCatch(Default::default()),
/// # GameMod::HardRockCatch(Default::default()),
/// # ].into_iter().collect();
/// # /*
/// let hd = mods!(Catch: HD);
/// let hdhr = mods!(Catch: HD HR);
/// # */
/// let mut intersection = hd.intersection(&hdhr);
///
/// assert_eq!(intersection.next(), Some(&GameMod::HiddenCatch(Default::default())));
/// assert_eq!(intersection.next(), None);
/// ```
// https://github.com/rust-lang/rust/blob/c1d3610ac1ddd1cd605479274047fd0a3f37d220/library/alloc/src/collections/btree/set.rs#L517
pub fn intersection<'m>(&'m self, other: &'m GameMods) -> GameModsIntersection<'m> {
let (Some(self_min), Some(self_max)) =
(self.inner.first_key_value(), self.inner.last_key_value())
else {
return GameModsIntersection {
inner: IntersectionInner::Answer(None),
};
};
let (Some(other_min), Some(other_max)) =
(other.inner.first_key_value(), other.inner.last_key_value())
else {
return GameModsIntersection {
inner: IntersectionInner::Answer(None),
};
};
GameModsIntersection {
inner: match (self_min.0.cmp(other_max.0), self_max.0.cmp(other_min.0)) {
(Ordering::Greater, _) | (_, Ordering::Less) => IntersectionInner::Answer(None),
(Ordering::Equal, _) => IntersectionInner::Answer(Some(self_min.1)),
(_, Ordering::Equal) => IntersectionInner::Answer(Some(self_max.1)),
_ => IntersectionInner::new_stitch(self.inner.iter(), other.inner.iter()),
},
}
}
/// Check whether the two [`GameMods`] have any common mods.
///
/// # Example
/// ```rust
/// use rosu_mods::GameMods;
///
/// # use rosu_mods::GameMod;
/// # let hd = GameMods::from(GameMod::HiddenCatch(Default::default()));
/// # let hr = GameMods::from(GameMod::HardRockCatch(Default::default()));
/// # let hdhr: GameMods = [
/// # GameMod::HiddenCatch(Default::default()),
/// # GameMod::HardRockCatch(Default::default()),
/// # ].into_iter().collect();
/// # /*
/// let hd = mods!(Catch: HD);
/// let hr = mods!(Catch: HR);
/// # */
/// assert!(!hd.intersects(&hr));
///
/// # /*
/// let hdhr = mods!(Catch: HD HR);
/// # */
/// assert!(hd.intersects(&hdhr));
/// ```
pub fn intersects(&self, other: &Self) -> bool {
self.intersection(other).next().is_some()
}
/// The clock rate of the [`GameMods`].
///
/// Returns `None` if any contained [`GameMod`] has no single clock rate.
///
/// # Example
/// ```rust
/// use rosu_mods::GameMod;
///
/// # let hd: rosu_mods::GameMods = [GameMod::HiddenOsu(Default::default())].into_iter().collect();
/// # /*
/// let hd = mods!(Osu: HD);
/// # */
/// assert_eq!(hd.clock_rate(), Some(1.0));
///
/// let mut hddt = hd;
/// hddt.insert(GameMod::DoubleTimeOsu(Default::default()));
/// assert_eq!(hddt.clock_rate(), Some(1.5));
///
/// let mut hddtwu = hddt;
/// hddtwu.insert(GameMod::WindUpOsu(Default::default()));
/// assert_eq!(hddtwu.clock_rate(), None);
/// ```
pub fn clock_rate(&self) -> Option<f32> {
self.inner
.values()
.map(GameMod::clock_rate)
.try_fold(1.0, |clock_rate, next| next.map(|next| clock_rate * next))
}
/// Tries to create [`GameMods`] from a [`GameModsIntermode`].
///
/// Returns `None` if any contained [`GameModIntermode`] is unknown for the
/// given [`GameMode`].
///
/// # Example
/// ```rust
/// use rosu_mods::{mods, GameMods, GameModsIntermode, GameMode};
///
/// let intermode: GameModsIntermode = mods!(DT FI);
/// let mods = GameMods::try_from_intermode(&intermode, GameMode::Mania).unwrap();
///
/// // The FadeIn mod doesn't exist in Taiko
/// assert!(GameMods::try_from_intermode(&intermode, GameMode::Taiko).is_none());
/// ```
pub fn try_from_intermode(mods: &GameModsIntermode, mode: GameMode) -> Option<Self> {
mods.try_with_mode(mode)
}
/// Create [`GameMods`] from a [`GameModsIntermode`].
///
/// Any contained [`GameModIntermode`] that's unknown for the given
/// [`GameMode`] will be replaced with `GameModIntermode::Unknown`.
///
/// # Example
/// ```rust
/// use rosu_mods::{mods, GameMods, GameModsIntermode, GameMode};
///
/// let intermode: GameModsIntermode = mods!(DT FI);
/// let mods = GameMods::from_intermode(&intermode, GameMode::Mania);
///
/// // The FadeIn mod doesn't exist in Taiko
/// let dt = GameMods::from_intermode(&intermode, GameMode::Taiko);
/// ```
pub fn from_intermode(mods: &GameModsIntermode, mode: GameMode) -> Self {
mods.with_mode(mode)
}
/// Returns an iterator over all contained mods.
///
/// Note that the iterator will immediately yield `None` in case of "`NoMod`".
pub fn iter(&self) -> GameModsIter<'_> {
GameModsIter::new(self.inner.values())
}
/// Returns an iterator that allows modifying each contained mod.
///
/// Note that the iterator will immediately yield `None` in case of "`NoMod`".
pub fn iter_mut(&mut self) -> GameModsIterMut<'_> {
GameModsIterMut::new(self.inner.values_mut())
}
/// Checks whether some contained mods exclude other contained mods.
///
/// # Example
/// ```rust
/// use rosu_mods::GameMod;
///
/// # let mut mods: rosu_mods::GameMods = [
/// # GameMod::EasyOsu(Default::default()),
/// # ].into_iter().collect();
/// # /*
/// let mut mods = mods!(Osu: EZ);
/// # */
/// assert!(mods.is_valid());
///
/// mods.insert(GameMod::HardRockOsu(Default::default()));
/// assert!(!mods.is_valid());
/// ```
pub fn is_valid(&self) -> bool {
for gamemod in self.inner.values() {
for &acronym in gamemod.incompatible_mods().iter() {
if self.contains_acronym(acronym) {
return false;
}
}
}
true
}
/// Remove all mods that are excluded by other contained mods.
///
/// # Example
/// ```rust
/// # let mut mods: rosu_mods::GameMods = [
/// # rosu_mods::GameMod::EasyOsu(Default::default()),
/// # rosu_mods::GameMod::HardRockOsu(Default::default())
/// # ].into_iter().collect();
/// # /*
/// let mut mods = mods!(Osu: EZ HR);
/// # */
/// assert_eq!(mods.to_string(), "EZHR");
///
/// mods.sanitize();
/// assert_eq!(mods.to_string(), "EZ");
/// ```
pub fn sanitize(&mut self) {
'outer: loop {
let mods = self.inner.values();
for gamemod in mods {
for &excluded in gamemod.incompatible_mods().iter() {
let intermode = GameModIntermode::from_acronym(excluded);
if self.contains_intermode(intermode) {
self.inner.retain(|key, _| *key != intermode);
continue 'outer;
}
}
}
break;
}
}
/// Turns [`GameMods`] into [`GameModsLegacy`].
pub fn as_legacy(&self) -> GameModsLegacy {
GameModsLegacy::from_bits(self.bits())
}
/// Attempts to turns [`GameMods`] into [`GameModsLegacy`].
///
/// Returns `None` if any contained [`GameMod`] does not have a bit value.
pub fn try_as_legacy(&self) -> Option<GameModsLegacy> {
self.checked_bits().map(GameModsLegacy::from_bits)
}
}
impl Debug for GameMods {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
f.debug_list().entries(self.inner.values()).finish()
}
}
impl Display for GameMods {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
if self.is_empty() {
f.write_str("NM")
} else {
for gamemod in self.iter() {
f.write_str(gamemod.acronym().as_str())?;
}
Ok(())
}
}
}
impl From<GameMod> for GameMods {
fn from(gamemod: GameMod) -> Self {
let mut mods = Self::new();
mods.insert(gamemod);
mods
}
}
impl IntoIterator for GameMods {
type Item = GameMod;
type IntoIter = IntoGameModsIter;
/// Turns [`GameMods`] into an iterator over all contained mods.
///
/// Note that the iterator will immediately yield `None` in case of "`NoMod`".
fn into_iter(self) -> Self::IntoIter {
IntoGameModsIter::new(self.inner.into_values())
}
}
impl<'a> IntoIterator for &'a GameMods {
type Item = <GameModsIter<'a> as Iterator>::Item;
type IntoIter = GameModsIter<'a>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<'a> IntoIterator for &'a mut GameMods {
type Item = <GameModsIterMut<'a> as Iterator>::Item;
type IntoIter = GameModsIterMut<'a>;
fn into_iter(self) -> Self::IntoIter {
self.iter_mut()
}
}
impl FromIterator<GameMod> for GameMods {
fn from_iter<T: IntoIterator<Item = GameMod>>(iter: T) -> Self {
Self {
inner: iter
.into_iter()
.map(|gamemod| (GameModOrder::from(&gamemod), gamemod))
.collect(),
}
}
}
impl Extend<GameMod> for GameMods {
fn extend<T: IntoIterator<Item = GameMod>>(&mut self, iter: T) {
let iter = iter
.into_iter()
.map(|gamemod| (GameModOrder::from(&gamemod), gamemod));
self.inner.extend(iter);
}
}
#[cfg(feature = "serde")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
const _: () = {
use serde::ser::{Serialize, SerializeSeq, Serializer};
impl Serialize for GameMods {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
let mut s = s.serialize_seq(Some(self.inner.len()))?;
for gamemod in self.inner.values() {
s.serialize_element(gamemod)?;
}
s.end()
}
}
};
#[cfg(feature = "rkyv")]
#[cfg_attr(docsrs, doc(cfg(feature = "rkyv")))]
const _: () = {
use rkyv::{
ser::{ScratchSpace, Serializer},
vec::{ArchivedVec, VecResolver},
Archive, Archived, Deserialize, Fallible, Infallible, Serialize,
};
impl Archive for GameMods {
type Archived = Archived<Vec<GameMod>>;
type Resolver = VecResolver;
unsafe fn resolve(&self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) {
ArchivedVec::resolve_from_len(self.inner.len(), pos, resolver, out);
}
}
impl<S: Serializer + ScratchSpace + Fallible + ?Sized> Serialize<S> for GameMods {
fn serialize(&self, s: &mut S) -> Result<Self::Resolver, S::Error> {
ArchivedVec::serialize_from_iter::<GameMod, _, _, _>(self.inner.values(), s)
}
}
impl<D: Fallible + ?Sized> Deserialize<GameMods, D> for Archived<Vec<GameMod>> {
fn deserialize(&self, _: &mut D) -> Result<GameMods, D::Error> {
Ok(self
.iter()
.map(|archived| archived.deserialize(&mut Infallible).unwrap())
.collect())
}
}
};
#[cfg(test)]
mod tests {
use crate::generated_mods::DoubleTimeOsu;
use super::*;
#[test]
fn insert_valid() {
let mut mods = GameMods::new();
mods.insert(GameMod::HiddenOsu(Default::default()));
mods.insert(GameMod::HardRockOsu(Default::default()));
assert_eq!(mods.len(), 2);
assert_eq!(mods.to_string(), "HDHR");
}
#[test]
fn contains() {
let mods: GameMods = [
GameMod::HiddenOsu(Default::default()),
GameMod::HardRockOsu(Default::default()),
GameMod::NightcoreOsu(Default::default()),
]
.into_iter()
.collect();
assert!(mods.contains_intermode(GameModIntermode::Nightcore));
assert!(mods.contains_intermode(GameModIntermode::Hidden));
assert!(!mods.contains_intermode(GameModIntermode::DoubleTime));
}
#[test]
fn checked_bits() {
let mods: GameMods = [
GameMod::HiddenOsu(Default::default()),
GameMod::TraceableOsu(Default::default()),
GameMod::DoubleTimeOsu(Default::default()),
]
.into_iter()
.collect();
assert_eq!(mods.checked_bits(), None);
}
#[test]
fn unchecked_bits() {
let mods: GameMods = [
GameMod::TraceableOsu(Default::default()),
GameMod::DoubleTimeOsu(Default::default()),
GameMod::HiddenOsu(Default::default()),
]
.into_iter()
.collect();
assert_eq!(mods.bits(), 72);
}
#[test]
fn intersection() {
let a: GameMods = [
GameMod::HiddenOsu(Default::default()),
GameMod::WindUpOsu(Default::default()),
GameMod::HardRockOsu(Default::default()),
]
.into_iter()
.collect();
let b: GameMods = [
GameMod::WindUpOsu(Default::default()),
GameMod::ClassicOsu(Default::default()),
GameMod::HardRockOsu(Default::default()),
]
.into_iter()
.collect();
let mut iter = a.intersection(&b);
assert_eq!(
iter.next().map(GameMod::intermode),
Some(GameModIntermode::HardRock)
);
assert_eq!(
iter.next().map(GameMod::intermode),
Some(GameModIntermode::WindUp)
);
assert_eq!(iter.next(), None);
}
#[test]
fn clock_rate_unaffected() {
let mods: GameMods = [
GameMod::HiddenOsu(Default::default()),
GameMod::HardRockOsu(Default::default()),
GameMod::WiggleOsu(Default::default()),
]
.into_iter()
.collect();
assert_eq!(mods.clock_rate(), Some(1.0));
}
#[test]
fn clock_rate_speed_change() {
let mut mods: GameMods = [GameMod::HardRockOsu(Default::default())]
.into_iter()
.collect();
mods.insert(GameMod::DoubleTimeOsu(DoubleTimeOsu {
speed_change: Some(1.25),
adjust_pitch: Some(false),
}));
assert_eq!(mods.clock_rate(), Some(1.25));
}
#[test]
fn clock_rate_variable() {
let mods: GameMods = [
GameMod::HiddenOsu(Default::default()),
GameMod::WindUpOsu(Default::default()),
]
.into_iter()
.collect();
assert_eq!(mods.clock_rate(), None);
}
#[test]
fn sanitize() {
let mut mods: GameMods = [
GameMod::BlindsOsu(Default::default()),
GameMod::FlashlightOsu(Default::default()),
GameMod::HiddenOsu(Default::default()),
GameMod::TraceableOsu(Default::default()),
]
.into_iter()
.collect();
mods.sanitize();
assert_eq!(mods.to_string(), "HDFL");
}
}