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
use std::{
borrow::Cow,
fmt::{Debug, Formatter, Result as FmtResult},
marker::PhantomData,
ops::Deref,
};
use crate::{
model::mode::{ConvertStatus, IGameMode},
util::generic_fmt::GenericFormatter,
Difficulty,
};
use super::Beatmap;
const INCOMPATIBLE_MODES: &str = "the gamemodes were incompatible";
/// A [`Beatmap`] that is attached to a mode.
///
/// # Incompatibility
///
/// The following conversions are compatible:
/// - `Osu` → `Osu`
/// - `Taiko` → `Taiko`
/// - `Catch` → `Catch`
/// - `Mania` → `Mania`
/// - `Osu` → `Taiko`
/// - `Osu` → `Catch`
/// - `Osu` → `Mania`
///
/// All other conversions are incompatible.
pub struct Converted<'a, M> {
map: Cow<'a, Beatmap>,
mode: PhantomData<M>,
}
impl<'a, M> Converted<'a, M> {
/// Initialize a [`Converted`] beatmap by promising the given map's mode
/// matches the generic type `M`.
pub(crate) const fn new(map: Cow<'a, Beatmap>) -> Self {
Self {
map,
mode: PhantomData,
}
}
/// Returns the internal [`Beatmap`].
pub fn into_inner(self) -> Cow<'a, Beatmap> {
self.map
}
/// Borrow the contained [`Beatmap`] to cheaply create a new owned
/// [`Converted`].
///
/// This is the same as `.clone()` except cheap - but its lifetime might be
/// shorter.
#[must_use]
pub fn as_owned(&'a self) -> Self {
Self::new(Cow::Borrowed(self.map.as_ref()))
}
}
impl<M: IGameMode> Converted<'_, M> {
/// Attempt to convert a [`Beatmap`] to the specified mode.
///
/// If the conversion is incompatible the [`Beatmap`] will be returned
/// unchanged as `Err`.
#[allow(clippy::result_large_err)]
pub fn try_from_owned(mut map: Beatmap) -> Result<Self, Beatmap> {
match M::try_convert(&mut map) {
ConvertStatus::Noop => Ok(Self::new(Cow::Owned(map))),
ConvertStatus::Conversion => Ok(Self::new(Cow::Owned(map))),
ConvertStatus::Incompatible => Err(map),
}
}
/// Convert a [`Beatmap`] to the specified mode.
///
/// # Panics
///
/// Panics if the conversion is incompatible.
pub fn unchecked_from_owned(map: Beatmap) -> Self {
Self::try_from_owned(map).unwrap_or_else(|_| panic!("{}", INCOMPATIBLE_MODES))
}
/// Create a gradual difficulty calculator for the map.
pub fn gradual_difficulty(&self, difficulty: Difficulty) -> M::GradualDifficulty {
M::gradual_difficulty(difficulty, self)
}
/// Create a gradual performance calculator for the map.
pub fn gradual_performance(&self, difficulty: Difficulty) -> M::GradualPerformance {
M::gradual_performance(difficulty, self)
}
}
impl<'a, M: IGameMode> Converted<'a, M> {
/// Create a performance calculator for the map.
pub fn performance(self) -> M::Performance<'a> {
M::performance(self)
}
/// Attempt to convert a [`&Beatmap`] to the specified mode.
///
/// If the conversion is incompatible, `None` is returned.
///
/// [`&Beatmap`]: Beatmap
pub fn try_from_ref(map: &'a Beatmap) -> Option<Self> {
let mut map = match M::check_convert(map) {
ConvertStatus::Noop => return Some(Self::new(Cow::Borrowed(map))),
ConvertStatus::Conversion => map.to_owned(),
ConvertStatus::Incompatible => return None,
};
match M::try_convert(&mut map) {
ConvertStatus::Conversion => Some(Self::new(Cow::Owned(map))),
ConvertStatus::Noop => Some(Self::new(Cow::Owned(map))),
ConvertStatus::Incompatible => None,
}
}
/// Convert a [`&Beatmap`] to the specified mode.
///
/// # Panics
///
/// Panics if the conversion is incompatible.
///
/// [`&Beatmap`]: Beatmap
pub fn unchecked_from_ref(map: &'a Beatmap) -> Self {
Self::try_from_ref(map).expect(INCOMPATIBLE_MODES)
}
/// Attempt to convert a [`&mut Beatmap`] to the specified mode.
///
/// If the conversion is incompatible, `None` is returned.
///
/// [`&mut Beatmap`]: Beatmap
pub fn try_from_mut(map: &'a mut Beatmap) -> Option<Self> {
match M::try_convert(map) {
ConvertStatus::Conversion => Some(Self::new(Cow::Borrowed(map))),
ConvertStatus::Noop => Some(Self::new(Cow::Borrowed(map))),
ConvertStatus::Incompatible => None,
}
}
/// Convert a [`&mut Beatmap`] to the specified mode.
///
/// # Panics
///
/// Panics if the conversion is incompatible.
///
/// [`&mut Beatmap`]: Beatmap
pub fn unchecked_from_mut(map: &'a mut Beatmap) -> Self {
Self::try_from_mut(map).expect(INCOMPATIBLE_MODES)
}
/// Attempt to convert a [`Converted`] from mode `M` to mode `N`.
///
/// If the conversion is incompatible the [`Converted`] will be returned
/// unchanged as `Err`.
#[allow(clippy::result_large_err)]
pub fn try_convert<N: IGameMode>(self) -> Result<Converted<'a, N>, Self> {
match self.map {
Cow::Borrowed(map) => Converted::<N>::try_from_ref(map).ok_or(self),
Cow::Owned(map) => {
Converted::<N>::try_from_owned(map).map_err(|map| Self::new(Cow::Owned(map)))
}
}
}
/// Convert a [`Converted`] from mode `M` to mode `N`.
///
/// # Panics
///
/// Panics if the conversion is incompatible.
pub fn unchecked_convert<N: IGameMode>(self) -> Converted<'a, N> {
match self.map {
Cow::Borrowed(map) => Converted::<N>::unchecked_from_ref(map),
Cow::Owned(map) => Converted::<N>::unchecked_from_owned(map),
}
}
}
impl<M> Clone for Converted<'_, M> {
fn clone(&self) -> Self {
Self::new(self.map.clone())
}
}
impl<M> Debug for Converted<'_, M> {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
f.debug_struct("Converted")
.field("map", &self.map)
.field("mode", &GenericFormatter::<M>::new())
.finish()
}
}
impl<M> PartialEq for Converted<'_, M> {
fn eq(&self, other: &Self) -> bool {
self.map == other.map
}
}
impl<M> Deref for Converted<'_, M> {
type Target = Beatmap;
fn deref(&self) -> &Self::Target {
self.map.as_ref()
}
}