#[macro_export]
macro_rules! matvar {
($($matvar:tt)+) => {
$crate::matvar_internal!($($matvar)+)
}
}
#[macro_export]
#[doc(hidden)]
macro_rules! matvar_internal {
(@array [$($elems:expr,)*] $next:tt, $($rest:tt)*) => {{
$crate::matvar_internal!(@array [$($elems,)* $crate::matvar_internal!($next),] $($rest)*)
}};
(@array [$($elems:expr,)*] $last:tt) => {{
$crate::matvar_internal!(@array [$($elems,)* $crate::matvar_internal!($last)])
}};
(@array [$($elems:expr),*] , $($rest:tt)*) => {
$crate::matvar_internal!(@array [$($elems,)*] $($rest)*)
};
(@array [$($elems:expr,)*]) => {{
let v = vec![$(($elems),)*];
if v.iter().all(|x| matches!(x, $crate::MatVariable::NumericArray(_))) && $crate::check_same_dim(&v) && $crate::check_same_type(&v) {
$crate::MatVariable::NumericArray($crate::NumericArray::from_nested_matvar(vec![1, v.len()], v).unwrap())
} else if v.iter().all(|x| matches!(x, $crate::MatVariable::Structure(_))) && $crate::check_same_fields(&v) {
$crate::MatVariable::StructureArray($crate::StructureArray::from_structures(vec![1, v.len()], v))
} else {
$crate::MatVariable::CellArray($crate::CellArray::new(vec![1, v.len()], v).unwrap())
}
}};
(@array [$($elems:expr),*]) => {{
let v = vec![$(($elems)),*];
if v.iter().all(|x| matches!(x, $crate::MatVariable::NumericArray(_))) && $crate::check_same_dim(&v) && $crate::check_same_type(&v) {
$crate::MatVariable::NumericArray($crate::NumericArray::from_nested_matvar(vec![1, v.len()], v).unwrap())
} else if v.iter().all(|x| matches!(x, $crate::MatVariable::Structure(_))) && $crate::check_same_fields(&v) {
$crate::MatVariable::StructureArray($crate::StructureArray::from_structures(vec![1, v.len()], v))
} else {
$crate::MatVariable::CellArray($crate::CellArray::new(vec![1, v.len()], v).unwrap())
}
}};
(@structure $structure:ident () () ()) => {};
(@structure $structure:ident ($key:ident) ($value:expr) , $($rest:tt)*) => {
let _ = $structure.insert(stringify!($key).into(), $value);
$crate::matvar_internal!(@structure $structure () ($($rest)*) ($($rest)*));
};
(@structure $structure:ident ($key:ident) ($value:expr)) => {
let _ = $structure.insert(stringify!($key).into(), $value);
};
(@structure $structure:ident ($key:ident) (: [$($array:tt)*] $($rest:tt)*) $copy:tt) => {
$crate::matvar_internal!(@structure $structure ($key) ($crate::matvar_internal!([$($array)*])) $($rest)*);
};
(@structure $structure:ident ($key:ident) (: {$($map:tt)*} $($rest:tt)*) $copy:tt) => {
$crate::matvar_internal!(@structure $structure ($key) ($crate::matvar_internal!({$($map)*})) $($rest)*);
};
(@structure $structure:ident ($key:ident) (: $value:expr , $($rest:tt)*) $copy:tt) => {
$crate::matvar_internal!(@structure $structure ($key) ($crate::matvar_internal!($value)) , $($rest)*);
};
(@structure $structure:ident ($key:ident) (: $value:expr) $copy:tt) => {
$crate::matvar_internal!(@structure $structure ($key) ($crate::matvar_internal!($value)));
};
(@structure $structure:ident () ($tt:ident $($rest:tt)*) $copy:tt) => {
$crate::matvar_internal!(@structure $structure ($tt) ($($rest)*) ($($rest)*));
};
([]) => {
$crate::MatVariable::NumericArray($crate::NumericArray::from_nested_matvar(vec![0, 0], vec![]).unwrap())
};
([ $($tt:tt)+ ]) => {{
$crate::matvar_internal!(@array [] $($tt)+)
}};
({}) => {{
$crate::MatVariable::Structure($crate::Structure::new($crate::__private::IndexMap::new()))
}};
({ $($tt:tt)+ }) => {
$crate::MatVariable::Structure($crate::Structure::new({
let mut structure = $crate::__private::IndexMap::new();
$crate::matvar_internal!(@structure structure () ($($tt)+) ($($tt)+));
structure
}))
};
($other:expr) => {{
$crate::MatVariable::from($other)
}};
}
#[cfg(test)]
mod tests {
#[test]
fn macro_test_1() {
let v = matvar!([true, true, true, false]);
println!("v = {:#?}", v);
let x = v[[0, 0]].to_bool();
println!("{:?}", x)
}
#[test]
fn macro_test_2() {
let v = matvar!([1.0]);
println!("v = {:#?}", v);
let x = v[[0, 0]].to_f64();
println!("{:?}", x)
}
#[test]
fn macro_test_3() {
let v = matvar!([]);
println!("v = {:#?}", v);
}
#[test]
fn macro_test_4() {
let v = matvar!({});
println!("v = {:#?}", v);
}
#[test]
fn macro_test_5() {
let v = matvar!({
code: 200,
success: true,
payload: {
features: [
1.0,
2.0,
],
homepage: false
}
});
println!("v = {:#?}", v);
}
#[test]
fn macro_test_6() {
let v = matvar!([
{ f1: 1.0 },
{ f1: 2.0 }
]);
println!("v = {:#?}", v);
}
#[test]
fn macro_test_7() {
let v = matvar!([
{ f1: [1.0, 2.2] },
{ f2: 2.0 }
]);
println!("v = {:#?}", v);
}
#[test]
fn macro_test_8() {
let v = matvar!("test");
println!("v = {:#?}", v);
}
#[test]
fn macro_test_9() {
let v = matvar!([1, 2, 3, 4]);
println!("v = {:#?}", v);
}
#[test]
fn macro_test_10() {
let v = matvar!([[1, 2], [3, 4]]);
println!("v = {:#?}", v);
}
#[test]
fn macro_test_11() {
let v = matvar!([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]);
println!("v = {:#?}", v);
}
#[test]
fn macro_test_12() {
let v = matvar!([
[[[1, 2], [3, 4]], [[5, 6], [7, 8]]],
[[[11, 12], [13, 14]], [[15, 16], [17, 18]]]
]);
println!("v = {:#?}", v);
}
#[test]
fn macro_test_13() {
let v = matvar!([(1.0, 42.), (2.0, 43.), (3.0, 44.)]);
println!("v = {:#?}", v);
}
}
#[macro_export]
macro_rules! matfile {
($($matfile:tt)+) => {
$crate::matfile_internal!($($matfile)+)
}
}
#[macro_export]
#[doc(hidden)]
macro_rules! matfile_internal {
(@variable $mat:ident $($name:ident: $var:expr,)*) => {{
$(
let varname = stringify!($name);
$mat.insert(varname, $var);
)*
$mat
}};
(@variable $mat:ident $($name:ident: $var:expr),*) => {{
$(
let varname = stringify!($name);
$mat.insert(varname, $var);
)*
$mat
}};
() => {
$crate::MatFile::new()
};
( $($tt:tt)+ ) => {{
let mut m = $crate::MatFile::new();
$crate::matfile_internal!(@variable m $($tt)+)
}};
}
#[cfg(test)]
mod matfile_tests {
#[test]
fn matfile_1() {
let f = matfile!(
var1: matvar!(1.0),
var2: matvar!(2),
);
println!("{:#?}", f)
}
}