Skip to content

Commit

Permalink
adjustment format
Browse files Browse the repository at this point in the history
  • Loading branch information
szy1231 committed Jun 2, 2024
1 parent f09877c commit 8420645
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
10 changes: 7 additions & 3 deletions examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use rsxml2json::{Convert, ConvertConfig};
use serde_json::{to_string_pretty, Value};

extern crate rsxml2json;
extern crate serde_json;

fn main() {
let conf = ConvertConfig::default();
Expand All @@ -14,7 +16,9 @@ fn main() {
}
};

let parsed_json: Value = serde_json::from_str(json_str.as_str()).expect("Unable to parse JSON");
let pretty_json = to_string_pretty(&parsed_json).expect("Unable to convert to pretty JSON");
let parsed_json: serde_json::Value =
serde_json::from_str(json_str.as_str()).expect("Unable to parse JSON");
let pretty_json =
serde_json::to_string_pretty(&parsed_json).expect("Unable to convert to pretty JSON");
println!("{}", pretty_json);
}
12 changes: 8 additions & 4 deletions src/convert.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::config::ConvertConfig;
use roxmltree::{Document, Node};
extern crate roxmltree;
use std::{collections::HashMap, error::Error};

pub struct Convert {
config: ConvertConfig,
}

type ChildrenMap<'a, 'input> = HashMap<String, Vec<Node<'a, 'input>>>;
type ChildrenMap<'a, 'input> = HashMap<String, Vec<roxmltree::Node<'a, 'input>>>;

struct ConversionError;

Expand All @@ -33,7 +33,7 @@ impl Convert {
if xml.is_empty() {
return Ok("".to_string());
}
let doc = Document::parse(xml.as_str())?;
let doc = roxmltree::Document::parse(xml.as_str())?;
let root = doc.root_element();
let mut json_string = String::new();
convert_node_to_json(&mut json_string, root, &self.config);
Expand All @@ -46,7 +46,11 @@ impl Convert {
}
}

fn convert_node_to_json(json_output: &mut String, current_node: Node, config: &ConvertConfig) {
fn convert_node_to_json(
json_output: &mut String,
current_node: roxmltree::Node,
config: &ConvertConfig,
) {
let mut element_count = 0;
let mut child_elements_map: ChildrenMap = HashMap::new();
let mut child_text_flag = false;
Expand Down
1 change: 1 addition & 0 deletions tests/convert.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
extern crate rsxml2json;
use rsxml2json::{Convert, ConvertConfig};
use std::{fs::File, io::Read, path::Path};

Expand Down

0 comments on commit 8420645

Please sign in to comment.