//! Utility macros for creating standard library collections.
//!
//! This module provides convenient macros for creating `HashMap` instances
//! with a familiar syntax similar to the `datavalue!` macro.

/// Macro for creating `std::collections::HashMap` instances.
///
/// This is a utility macro used internally and can also be used directly
/// for creating standard HashMaps with convenient syntax.
///
/// # Examples
///
/// ```rust
/// use trs_data_value::stdhashmap;
/// use std::collections::HashMap;
///
/// let map: HashMap<&str, &str> = stdhashmap!(
///     "key1" => "value1",
///     "key2" => "value2"
/// );
/// assert_eq!(map.get("key1"), Some(&"value1"));
/// ```
#[macro_export]
macro_rules! stdhashmap {
    (@single $($x:tt)*) => (());
    (@count $($rest:expr),*) => (<[()]>::len(&[$($crate::stdhashmap!(@single $rest)),*]));

    // case { "a" => 1, }
    ($($key:expr => $value:expr,)+) => { $crate::stdhashmap!($($key => $value),+) };
    // case { "a" => vec![1, 2, 3] }
    ($($key:expr => vec![$($value:expr),*]),*) => {
        $crate::stdhashmap!($($key => [$($value),*]),*)
    };
    // case { "a" => [1, 2, 3] }
    ($($key:expr => [$($value:expr),*]),*) => {
        {
            let _cap = $crate::stdhashmap!(@count $($key),*);
            let mut _map = ::std::collections::HashMap::with_capacity(_cap);
            $(

                let mut _arr = Vec::new();
                $(
                    _arr.push($value.into());
                )*

                    _map.insert($key.into(), _arr);
            )*

            _map
        }
    };
    // case { "a" => 1 }
    ($($key:expr => $value:expr),*) => {
        {
            let _cap = $crate::stdhashmap!(@count $($key),*);
            let mut _map = ::std::collections::HashMap::with_capacity(_cap);
            $(
                _map.insert($key.into(), $value.into());
            )*
            _map
        }
    };
}
