Macro nbt_lib::unwrap_to_empty_if_exists
source · macro_rules! unwrap_to_empty_if_exists { ($data:expr, $name:expr, $to:ident) => { ... }; }
Expand description
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.