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
//! This module contains macros needed to work with a [`NbtValue`]
/// creates a Compound for data inserted
#[macro_export]
macro_rules! create_compound_map {
($($name:ident : $data:expr),*) => {
[
$(
(stringify!($name).to_string(), $data),
)*
].into_iter().collect::<HashMap<std::string::String, NbtValue>>()
};
}
/// Unwraps an [`Option<NbtValue>`] and converts it to a specific type using a method named `as_<type>`.
///
/// # Examples
///
/// ```
/// use nbt_lib::unwrap_to_empty;
/// use nbt_lib::NbtValue;
///
/// fn test() -> Result<i32, ()> {
/// let data: Option<NbtValue> = Some(NbtValue::Int(42));
/// let result: i32 = unwrap_to_empty!(data, i32);
/// Ok(result)
/// }
/// assert_eq!(test(), Ok(42))
/// ```
///
/// ```
/// use nbt_lib::unwrap_to_empty;
/// use nbt_lib::NbtValue;
/// use std::collections::HashMap;
///
/// fn test() -> Result<i32, ()> {
/// let mut map = HashMap::new();
/// map.insert("key".to_string(), NbtValue::Int(42));
/// let data: HashMap<String, NbtValue> = map;
/// let result: i32 = unwrap_to_empty!(data, "key", i32);
/// Ok(result)
/// }
/// assert_eq!(test(), Ok(42))
/// ```
///
/// # Errors
///
/// Returns an error if the option is `None` or if the conversion fails.
#[macro_export]
macro_rules! unwrap_to_empty {
($data:expr, $to:ident) => {
paste::paste! {
$data.ok_or(())?.[<as_ $to>]().map_err(|_|())?
}
};
($data:expr, $name:expr, $to:ident) => {
paste::paste! {
$data.get($name).ok_or(())?.[<as_ $to>]().map_err(|_|())?
}
};
}
/// Unwraps an [`Option<NbtValue>`] and converts it into a specified type.
///
/// # Examples
///
/// ```
/// use nbt_lib::unwrap_to;
/// use nbt_lib::NbtValue;
/// use nbt_lib::traits::FromNbtValue;
///
/// #[derive(Debug, PartialEq)]
/// struct Test { data: i32 };
///
/// impl FromNbtValue for Test {
/// fn from_nbt_value(value: NbtValue) -> Result<Self, ()> {
/// match value {
/// NbtValue::Int(v) => Ok(Self { data: v }),
/// _ => Err(())
/// }
/// }
/// }
///
/// fn test() -> Result<Test, ()> {
/// let data: Option<NbtValue> = Some(NbtValue::Int(42));
/// let result: Test = unwrap_to!(data, Test);
/// Ok(result)
/// }
/// assert_eq!(test(), Ok(Test { data: 42 }));
/// ```
///
/// ```
/// use nbt_lib::unwrap_to;
/// use nbt_lib::NbtValue;
/// use std::collections::HashMap;
/// use nbt_lib::traits::FromNbtValue;
///
/// #[derive(Debug, PartialEq)]
/// struct Test { data: i32 };
///
/// impl FromNbtValue for Test {
/// fn from_nbt_value(value: NbtValue) -> Result<Self, ()> {
/// match value {
/// NbtValue::Int(v) => Ok(Self { data: v }),
/// _ => Err(())
/// }
/// }
/// }
///
/// fn test() -> Result<Test, ()> {
/// let mut map = HashMap::new();
/// map.insert("data".to_string(), NbtValue::Int(42));
/// let data: HashMap<String, NbtValue> = map;
/// let result: Test = unwrap_to!(data, "data", Test);
/// Ok(result)
/// }
/// assert_eq!(test(), Ok(Test { data: 42 }));
/// ```
///
/// # Errors
///
/// Returns an error if the option is `None` or if the conversion fails.
///
/// [`NbtValue`]: `nbt_lib::NbtValue`
#[macro_export]
macro_rules! unwrap_to {
($data:expr, $t:ty) => {
paste::paste! {
<$t>::from_nbt_value($data.ok_or(())?.to_owned()).map_err(|_|())?
}
};
($data:expr, $name:expr, $t:ty) => {
unwrap_to!($data.get($name), $t)
};
}
/// Unwraps an [`Option<NbtValue>`] and converts it to a specific type if the key exists, otherwise returns `None`.
///
/// # Examples
///
/// ```
/// use nbt_lib::{unwrap_to_empty_if_exists, NbtValue};
/// use std::collections::HashMap;
///
/// fn test() -> Result<Option<i32>, ()> {
/// let mut map = HashMap::new();
/// map.insert("key".to_string(), NbtValue::Int(42));
/// let data: HashMap<String, NbtValue> = map;
/// let result: Option<i32> = unwrap_to_empty_if_exists!(data, "key", i32);
/// Ok(result)
/// }
/// assert_eq!(test(), Ok(Some(42)));
/// ```
///
/// # Errors
///
/// Returns an error if the option is `None` or if the conversion fails.
#[macro_export]
macro_rules! unwrap_to_empty_if_exists {
($data:expr, $name:expr, $to:ident) => {
paste::paste! {
if $data.contains_key($name) { Some($data.get($name).ok_or(())?.[<as_ $to>]().map_err(|_|())? ) } else { None }
}
};
}
/// Converts a list of [`NbtValue`]s to a vector of a specified type.
///
/// # Examples
///
/// ```
/// use nbt_lib::{convert_list_to, NbtValue, traits::FromNbtValue};
///
/// #[derive(Debug, PartialEq)]
/// struct Data(i32);
///
/// impl FromNbtValue for Data {
/// fn from_nbt_value(value: NbtValue) -> Result<Self, ()> {
/// match value {
/// NbtValue::Int(v) => Ok(Data(v)),
/// _ => Err(()),
/// }
/// }
/// }
///
/// fn test() -> Result<Vec<Data>, ()> {
/// let data: Option<NbtValue> = Some(NbtValue::List(vec![NbtValue::Int(1), NbtValue::Int(2)]));
/// let result: Vec<Data> = convert_list_to!(data, Data);
/// Ok(result)
/// }
/// assert_eq!(test(), Ok(vec![Data(1), Data(2)]));
/// ```
///
/// # Errors
///
/// Returns an error if the option is `None` or if the conversion fails.
#[macro_export]
macro_rules! convert_list_to {
($data:expr, $t:ty) => {
nbt_lib::unwrap_to_empty!($data, list)
.into_iter()
.map(|data| <$t>::from_nbt_value(data))
.collect::<Result<Vec<_>, ()>>()?
};
($data:expr, $name:expr, $t:ty) => {
nbt_lib::unwrap_to_empty!($data, $name, list)
.into_iter()
.map(|data| <$t>::from_nbt_value(data))
.collect::<Result<Vec<_>, ()>>()?
};
}
/// Converts a list of [`NbtValue`]s to a `Result` with a vector of a specified type or an error.
///
/// # Examples
///
/// ```
/// use nbt_lib::{convert_list_to_result, NbtValue, traits::FromNbtValue};
///
/// #[derive(Debug, PartialEq)]
/// struct Data(i32);
///
/// impl FromNbtValue for Data {
/// fn from_nbt_value(value: NbtValue) -> Result<Self, ()> {
/// match value {
/// NbtValue::Int(v) => Ok(Data(v)),
/// _ => Err(()),
/// }
/// }
/// }
///
/// fn test() -> Result<Vec<Data>, ()> {
/// let data: Option<NbtValue> = Some(NbtValue::List(vec![NbtValue::Int(1), NbtValue::Int(2)]));
/// let result: Result<Vec<Data>, ()> = convert_list_to_result!(data, Data);
/// result
/// }
/// assert_eq!(test(), Ok(vec![Data(1), Data(2)]));
/// ```
///
/// # Errors
///
/// Returns an error if the option is `None` or if the conversion fails.
#[macro_export]
macro_rules! convert_list_to_result {
($data:expr, $t:ty) => {
nbt_lib::unwrap_to_empty!($data, list)
.into_iter()
.map(|data| <$t>::from_nbt_value(data))
.collect::<Result<Vec<_>, ()>>()
};
}
///
#[macro_export]
macro_rules! convert_to_bool {
($data:expr) => {
$data == 1
};
}