use nadi_plugin::nadi_internal_plugin;
#[nadi_internal_plugin]
mod table {
use crate::prelude::*;
use crate::table::Table;
use nadi_plugin::network_func;
use std::path::PathBuf;
use std::str::FromStr;
use std::{fs::File, io::Write, path::Path};
#[network_func]
fn save_csv(
net: &Network,
path: &Path,
fields: Vec<String>,
filter: Option<Vec<bool>>,
) -> anyhow::Result<()> {
let mut file = File::create(path)?;
writeln!(file, "{}", fields.join(","))?;
let nodes: Vec<_> = match filter {
Some(filt) => net
.nodes()
.zip(filt)
.filter(|(_, f)| *f)
.map(|(n, _)| n)
.collect(),
None => net.nodes().collect(),
};
for node in nodes {
let values = fields
.iter()
.map(|a| {
node.lock().attr_dot(a).map(|a| match a {
Some(v) => v.to_string(),
None => String::new(),
})
})
.collect::<Result<Vec<String>, String>>()
.map_err(anyhow::Error::msg)?;
writeln!(file, "{}", values.join(","))?;
}
Ok(())
}
#[network_func]
fn table_to_markdown(
net: &Network,
table: Option<PathBuf>,
template: Option<String>,
outfile: Option<PathBuf>,
connections: Option<String>,
) -> anyhow::Result<()> {
let tab = match (table, template) {
(Some(t), None) => Table::from_file(t)?,
(None, Some(t)) => Table::from_str(&t)?,
(Some(_), Some(_)) => return Err(anyhow::Error::msg("table and template both given")),
(None, None) => return Err(anyhow::Error::msg("neither table nor template given")),
};
let md = tab.render_markdown(net, connections)?;
if let Some(out) = outfile {
let mut output = std::fs::File::create(out)?;
write!(output, "{md}")?;
} else {
println!("{md}");
}
Ok(())
}
}