use crate::{Row, Value};
pub(crate) fn extract_algo_array(result: &[&Row]) -> Vec<Row> {
if result.len() != 1 {
return result.iter().map(|r| (*r).clone()).collect();
}
let row = result[0];
for col_name in ["column_0", "wcc()", "scc()", "pagerank()", "degree_centrality()",
"betweenness_centrality()", "closeness_centrality()", "eigenvector_centrality()",
"labelPropagation()", "louvain()"] {
if let Some(Value::Array(arr)) = row.get_value(col_name) {
return arr.iter().filter_map(|v| {
if let Value::Object(obj) = v {
Some(Row::from_map(obj.clone()))
} else {
None
}
}).collect();
}
}
result.iter().map(|r| (*r).clone()).collect()
}
pub(crate) fn extract_node_id(row: &Row) -> Option<String> {
row.get_value("node_id").and_then(|v| match v {
Value::Integer(i) => Some(i.to_string()),
Value::String(s) => Some(s.clone()),
_ => None,
})
}
pub(crate) fn extract_user_id(row: &Row) -> Option<String> {
row.get_value("user_id").and_then(|v| match v {
Value::String(s) => Some(s.clone()),
Value::Integer(i) => Some(i.to_string()),
_ => None,
})
}
pub(crate) fn extract_float(row: &Row, field: &str) -> f64 {
row.get_value(field)
.map(|v| match v {
Value::Float(f) => *f,
Value::Integer(i) => *i as f64,
_ => 0.0,
})
.unwrap_or(0.0)
}
pub(crate) fn extract_int(row: &Row, field: &str) -> i64 {
row.get_value(field)
.map(|v| match v {
Value::Integer(i) => *i,
Value::Float(f) => *f as i64,
_ => 0,
})
.unwrap_or(0)
}
pub(crate) fn extract_string(row: &Row, field: &str) -> Option<String> {
row.get_value(field).and_then(|v| match v {
Value::String(s) => Some(s.clone()),
Value::Integer(i) => Some(i.to_string()),
_ => None,
})
}