mod import;
mod export;
use crate::{model::{Format, Graph, NodeRef, Profile, toml::{export::toml_value_from_node, import::parse_toml_object_value}}, runtime::Error};
use toml::{Table, Value};
#[derive(Debug)]
pub struct TomlFormat;
impl Format for TomlFormat {
fn identifiers(&self) -> Vec<String> {
vec!["toml".into()]
}
fn content_type(&self) -> String {
"text/toml".into()
}
fn string_import(&self, graph: &mut Graph, _format: &str, src: &str, node: Option<NodeRef>, _profile: &Profile) -> Result<(), Error> {
if src.is_empty() { return Ok(()); }
match src.parse::<Table>() {
Ok(table) => {
let mut parse_node = graph.ensure_main_root();
if let Some(nd) = node {
parse_node = nd;
}
parse_toml_object_value(graph, &parse_node, table);
Ok(())
},
Err(error) => {
Err(Error::TOMLStringImport(error.to_string()))
}
}
}
fn string_export(&self, graph: &Graph, _format: &str, node: Option<NodeRef>) -> Result<String, Error> {
let exp_node;
if let Some(nd) = node {
exp_node = nd;
} else {
exp_node = graph.main_root().expect("graph does not have a main 'root' node for default TOML export");
}
let value = toml_value_from_node(graph, &exp_node);
match toml::to_string(&Value::Table(value)) {
Ok(toml) => {
Ok(toml)
},
Err(error) => {
Err(Error::TOMLStringExport(error.to_string()))
}
}
}
}