use crate::lib::{Value};
#[doc(hidden)]
pub fn _empty_array() -> Vec<Value> {
vec![]
}
pub fn intersection_x(v1: Value, v2: Value) -> Vec<Value> {
let mut result = vec![];
let vec1 = match v1 {
Value::Null | Value::Bool(_) | Value::Number(_) | Value::String(_) | Value::Object(_) => {
return result
}
Value::Array(vec) => {
if vec.is_empty() {
return result;
}
vec
}
};
match v2 {
Value::Null | Value::Bool(_) | Value::Number(_) | Value::String(_) | Value::Object(_) => {
return result
}
Value::Array(vec2) => {
if vec2.is_empty() {
return result;
}
for v1 in vec1.iter() {
if v1.is_object() || v1.is_array() {
continue;
}
for v2 in vec2.iter() {
if v2.is_object() || v2.is_array() {
continue;
}
if v1 == v2 {
result.push(v1.clone())
}
}
}
}
}
result
}
pub fn intersection(v1: Value, v2: Value) -> Value {
Value::Array(intersection_x(v1, v2))
}
#[macro_export]
macro_rules! intersection_x {
() => (
$crate::_empty_array()
);
($a:expr $(,)*) => {{
if $a.is_array() {
$a.as_array().unwrap_or_else($crate::_empty_array)
} else {
$crate::_empty_array()
}
}};
($a:expr, $b:expr $(,)*) => {
$crate::intersection_x($a, $b)
};
($a:expr, $b:expr, $($rest:tt)*) => {
$crate::intersection_x!($crate::intersection($a, $b), $($rest)*)
};
}
#[macro_export]
macro_rules! intersection {
() => (
json!([])
);
($a:expr $(,)*) => {{
if $a.is_array() {
$a
} else {
json!([])
}
}};
($a:expr, $b:expr $(,)*) => {
$crate::intersection($a, $b)
};
($a:expr, $b:expr, $($rest:tt)*) => {
$crate::intersection!($crate::intersection($a, $b), $($rest)*)
};
}