gdlib 0.4.0

Rust library for editing Geometry Dash savefiles
Documentation
#![warn(clippy::all)]
#![deny(missing_docs)]
#![doc = include_str!("../README.md")]

pub mod ccgamemanager;
pub mod cclocallevels;
pub mod core;

#[cfg(test)]
pub mod tests;

/// Enum definition helper
macro_rules! repr_t {
    ($(#[$meta:meta])* $name:ident : $t:ty { $( $(#[$vmeta:meta])* $variant:ident = $val:expr),* $(,)? }) => {
        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
        #[allow(missing_docs)]
        $(#[$meta])*
        pub enum $name {
            $(
                $(#[$vmeta])*
                $variant,
            )*
            /// Value that is unaccounted for (unknown case or corrupt value)
            Unrecognized($t)
        }

        impl From<$t> for $name {
            fn from(value: $t) -> Self {
                match value {
                    $($val => Self::$variant,)*
                    _ => Self::Unrecognized(value),
                }
            }
        }
        #[allow(missing_docs)]

        impl $name {
            /// Converts `self` to its numeric type. Autogenerated by the `crate::repr_t` macro.
            ///
            /// Normally, `#[repr(t)]` would be used here as that would serve as the same thing. However, for cases where a value may be parsed that isn't accounted for, an `Unrecognized` variant is necessary, which allows us to catch "other" values.
            pub fn to_num(&self) -> $t {
                match self {
                    Self::Unrecognized(n) => *n,
                    $(Self::$variant => $val,)*
                }
            }
        }
    };

    ($(#[$meta:meta])* $name:ident : $t:ty { $( $(#[$vmeta:meta])* $variant:ident = $val:expr),* $(,)? } default $default:ident) => {
        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
        #[allow(missing_docs)]
        $(#[$meta])*
        pub enum $name {
            $(
                $(#[$vmeta])*
                $variant,
            )*
            /// Value that is unaccounted for (unknown case or corrupt value)
            Unrecognized($t)
        }

        impl From<$t> for $name {
            fn from(value: $t) -> Self {
                match value {
                    $($val => Self::$variant,)*
                    _ => Self::Unrecognized(value),
                }
            }
        }

        impl $name {
            /// Converts `self` to its numeric type. Autogenerated by the `crate::repr_t` macro.
            pub fn to_num(&self) -> $t {
                match self {
                    Self::Unrecognized(n) => *n,
                    $(Self::$variant => $val,)*
                }
            }
        }

        impl Default for $name {
            fn default() -> Self {
                Self::$default
            }
        }
    };

    // next two macros are for enums which have a known and fixed set of values where an unrecognized value would be irregular. e.g. speed (0.5x, 1x, 2x, 3x, 4x)

    ($(#[$meta:meta])* strict $name:ident : $t:ty { $( $(#[$vmeta:meta])* $variant:ident = $val:expr),* $(,)? }) => {
        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
        #[allow(missing_docs)]
        #[repr($t)]
        $(#[$meta])*
        pub enum $name {
            $(
                $(#[$vmeta])*
                $variant,
            )*
        }

        impl TryFrom<$t> for $name {
            type Error = $t;
            fn try_from(value: $t) -> Result<Self, Self::Error> {
                match value {
                    $($val => Ok(Self::$variant),)*
                    _ => Err(value),
                }
            }
        }
    };

    ($(#[$meta:meta])* strict $name:ident : $t:ty { $( $(#[$vmeta:meta])* $variant:ident = $val:expr),* $(,)? } default $default:ident) => {
        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
        #[allow(missing_docs)]
        #[repr($t)]
        $(#[$meta])*
        pub enum $name {
            $(
                $(#[$vmeta])*
                $variant,
            )*
        }

        impl TryFrom<$t> for $name {
            type Error = $t;
            fn try_from(value: $t) -> Result<Self, Self::Error> {
                match value {
                    $($val => Ok(Self::$variant),)*
                    _ => Err(value),
                }
            }
        }

        impl Default for $name {
            fn default() -> Self {
                Self::$default
            }
        }
    };
}
pub(crate) use repr_t;