use crate::error::ConversionError;
use crate::xml_utils::{write_declaration, write_comment, write_start_tag, write_empty_tag, write_end_tag, write_content};
use quick_xml::Writer;
use quick_xml::events::{BytesEnd, BytesStart};
use serde_json::{Value, Map, from_str};
use std::collections::HashMap;
use std::io::Cursor;
use std::fs;
use toml::de::from_str as toml_from_str;
pub fn json_to_xml(json_string: &str) -> Result<String, ConversionError> {
json_to_xml_with_root(json_string, "Root")
}
pub fn json_to_xml_with_root(json_string: &str, root: &str) -> Result<String, ConversionError> {
let json_value: Value = from_str(&json_string)?;
let mut writer = Writer::new_with_indent(Cursor::new(Vec::new()), b' ', 2);
write_declaration(&mut writer, "1.0", Some("UTF-8"))?;
let mut version = get_dependency_version("Cargo.toml")?;
version = format!("Generated with json_to_xml {}", version);
write_comment(&mut writer, &version)?;
create_xml_element(&json_value, &mut writer, root)?;
if has_top_level_attributes(&json_value) {
write_end_tag(&mut writer, &BytesEnd::new(root))?;
}
Ok(String::from_utf8(writer.into_inner().into_inner())?)
}
pub fn get_dependency_version(file_path: &str) -> Result<String, ConversionError> {
let content = fs::read_to_string(file_path)?;
let toml: Value = toml_from_str(&content)?;
if let Some(deps) = toml.get("dependencies") {
if let Some(dep) = deps.get("json_to_xml") {
if dep.is_object() {
if let Some(version) = dep.get("version").and_then(|v| v.as_str()) {
return Ok(version.to_string());
}
} else if let Some(version) = dep.as_str() {
return Ok(version.to_string());
}
}
}
if let Some(pkg) = toml.get("package") {
if let Some(version) = pkg.get("version").and_then(|v| v.as_str()) {
return Ok(version.to_string());
}
}
Err(ConversionError::Toml(toml_from_str::<Value>("").unwrap_err()))
}
fn create_xml_element(
json_data: &Value,
writer: &mut Writer<Cursor<Vec<u8>>>,
parent_tag: &str
) -> Result<(), ConversionError> {
match json_data {
Value::Object(map) => {
handle_object(writer, map, parent_tag)?;
},
Value::Array(arr) => {
handle_array(writer, arr, parent_tag)?;
},
Value::String(s) => {
write_content(writer, s)?;
},
Value::Number(num) => {
write_content(writer, &num.to_string())?;
}
Value::Bool(b) => {
write_content(writer, &b.to_string())?;
}
Value::Null => {
write_empty_tag(writer, &BytesStart::new("None"))?;
}
}
Ok(())
}
fn handle_object(
writer: &mut Writer<Cursor<Vec<u8>>>,
map: &Map<String, Value>,
parent_tag: &str
) -> Result<(), ConversionError> {
let parent_tag = capitalize_word(parent_tag);
let mut element = BytesStart::new(parent_tag.clone());
let attributes: HashMap<_, _> = map
.iter()
.filter(|(key, _)| key.starts_with("@"))
.map(|(key, value)| (&key[1..], value))
.collect();
for (key, value) in &attributes {
if let Some(value_str) = value.as_str() {
element.push_attribute((*key, value_str));
}
}
if !attributes.is_empty() {
write_start_tag(writer, &element)?;
}
if let Some(text_content) = map.get("$text").and_then(|v| v.as_str()) {
write_content(writer, text_content)?;
}
for (key, value) in map {
let key_tag = capitalize_word(key);
element = BytesStart::new(key_tag.clone());
if value.as_object().map_or(false, |m| m.is_empty()) {
write_empty_tag(writer, &element)?;
continue;
}
if key.starts_with("@") || key == "$text" {
continue;
} else {
if !(is_attribute_key(value) || is_array_with_attribute_key(value)) {
write_start_tag(writer, &element)?;
}
create_xml_element(value, writer, key)?;
if !value.is_array() {
write_end_tag(writer, &BytesEnd::new(key_tag))?;
}
}
}
Ok(())
}
fn handle_array(
writer: &mut Writer<Cursor<Vec<u8>>>,
arr: &Vec<Value>,
parent_tag: &str
) -> Result<(), ConversionError> {
let mut parent_tag = capitalize_word(parent_tag);
let original_tag = parent_tag.clone();
let item_tag = parent_tag.clone() + "Item";
let mixed_array = contains_objects_and_primitives(arr);
if arr.is_empty() {
write_end_tag(writer, &BytesEnd::new(&parent_tag))?;
return Ok(());
}
for (i, value) in arr.iter().enumerate() {
if value.is_object() {
if i == 0 && mixed_array {
parent_tag = item_tag.clone();
write_start_tag(writer, &BytesStart::new(&parent_tag))?;
}
handle_object_array(writer, i, value, &parent_tag)?;
} else {
if parent_tag != item_tag {
parent_tag = item_tag.clone();
}
write_start_tag(writer, &BytesStart::new(&parent_tag))?;
create_xml_element(value, writer, &original_tag)?;
write_end_tag(writer, &BytesEnd::new(&parent_tag))?;
if i == arr.len() - 1 {
write_end_tag(writer, &BytesEnd::new(&original_tag))?;
}
}
if i == arr.len() - 1 && value.is_object() && mixed_array {
write_end_tag(writer, &BytesEnd::new(&original_tag))?;
}
}
Ok(())
}
fn handle_object_array(
writer: &mut Writer<Cursor<Vec<u8>>>,
index: usize,
value: &Value,
parent_tag: &str
) -> Result<(), ConversionError> {
if let Some(obj) = value.as_object() {
if !obj.is_empty() {
let first_key = obj.keys().next().unwrap();
if !first_key.starts_with("@") && index > 0 {
write_start_tag(writer, &BytesStart::new(parent_tag))?;
}
create_xml_element(value, writer, &parent_tag)?;
write_end_tag(writer, &BytesEnd::new(parent_tag))?;
}
}
Ok(())
}
fn contains_objects_and_primitives(arr: &Vec<Value>) -> bool {
let mut contains_obj = false;
let mut contains_primitive = false;
for v in arr {
if v.is_object() {
contains_obj = true;
} else {
contains_primitive = true;
}
if contains_obj && contains_primitive {
return true;
}
}
false
}
fn has_top_level_attributes(json: &Value) -> bool {
if let Value::Object(map) = json {
map.keys().any(|key| key.starts_with('@'))
} else {
false
}
}
fn is_attribute_key(value: &Value) -> bool {
value.as_object().map_or(false, |m| m.keys().any(|key| key.starts_with("@")))
}
fn is_array_with_attribute_key(value: &Value) -> bool {
value.as_array().and_then(|a| a.first()).map_or(false, |v| is_attribute_key(v))
}
fn capitalize_word(word: &str) -> String {
let mut chars = word.chars();
match chars.next() {
None => String::new(),
Some(f) => f.to_uppercase().collect::<String>() + chars.as_str(),
}
}