Cleanup clippy lints and formatting

This commit is contained in:
R Tyler Croy 2021-05-23 09:51:39 -07:00
parent db610105c4
commit 95c9ac4895
1 changed files with 104 additions and 105 deletions

View File

@ -8,16 +8,14 @@
extern crate serde_json;
use log::*;
use quick_xml::Reader;
use quick_xml::events::Event;
use quick_xml::Reader;
use serde_json::{Map, Value};
#[derive(Debug)]
pub struct Error {
}
pub struct Error {}
fn read<'a>(mut reader: &mut Reader<&'a [u8]>, depth: u64) -> Value {
fn read(mut reader: &mut Reader<&[u8]>, depth: u64) -> Value {
let mut buf = Vec::new();
let mut values = Vec::new();
let mut node = Map::new();
@ -31,32 +29,34 @@ fn read<'a>(mut reader: &mut Reader<&'a [u8]>, depth: u64) -> Value {
let mut attrs = Map::new();
debug!("{} children: {:?}", name, child);
e.attributes().map(|a| {
if let Ok(attr) = a {
let key = String::from_utf8(attr.key.to_vec());
let value = String::from_utf8(attr.value.to_vec());
let _ = e
.attributes()
.map(|a| {
if let Ok(attr) = a {
let key = String::from_utf8(attr.key.to_vec());
let value = String::from_utf8(attr.value.to_vec());
// Only bother adding the attribute if both key and value are valid utf8
if key.is_ok() && value.is_ok() {
let key = format!("@{}", key.unwrap());
let value = Value::String(value.unwrap());
// Only bother adding the attribute if both key and value are valid utf8
if let (Ok(key), Ok(value)) = (key, value) {
let key = format!("@{}", key);
let value = Value::String(value);
// If the child is already an object, that's where the insert
// should happen
if child.is_object() {
child.as_object_mut().unwrap().insert(key, value);
}
else {
attrs.insert(key, value);
// If the child is already an object, that's where the insert
// should happen
if child.is_object() {
child.as_object_mut().unwrap().insert(key, value);
} else {
attrs.insert(key, value);
}
}
}
}
}).collect::<Vec<_>>();
})
.collect::<Vec<_>>();
/*
/*
* nodes with attributes need to be handled special
*/
if attrs.len() > 0 {
if !attrs.is_empty() {
if child.is_string() {
attrs.insert("#text".to_string(), child);
}
@ -64,42 +64,37 @@ fn read<'a>(mut reader: &mut Reader<&'a [u8]>, depth: u64) -> Value {
if let Ok(attrs) = serde_json::to_value(attrs) {
node.insert(name, attrs);
}
}
else {
if node.contains_key(&name) {
debug!("Node contains `{}` already, need to convert to array", name);
let (_, mut existing) = node.remove_entry(&name).unwrap();
let mut entries: Vec<Value> = vec![];
} else if node.contains_key(&name) {
debug!("Node contains `{}` already, need to convert to array", name);
let (_, mut existing) = node.remove_entry(&name).unwrap();
let mut entries: Vec<Value> = vec![];
if existing.is_array() {
let existing = existing.as_array_mut().unwrap();
while existing.len() > 0 {
entries.push(existing.remove(0));
}
if existing.is_array() {
let existing = existing.as_array_mut().unwrap();
while !existing.is_empty() {
entries.push(existing.remove(0));
}
else {
entries.push(existing);
}
entries.push(child);
} else {
entries.push(existing);
}
entries.push(child);
node.insert(name, Value::Array(entries));
}
else {
node.insert(name, child);
}
node.insert(name, Value::Array(entries));
} else {
node.insert(name, child);
}
}
},
}
Ok(Event::Text(ref e)) => {
if let Ok(decoded) = e.unescape_and_decode(&reader) {
values.push(Value::String(decoded));
}
},
}
Ok(Event::CData(ref e)) => {
if let Ok(decoded) = e.unescape_and_decode(&reader) {
node.insert("#cdata".to_string(), Value::String(decoded));
}
},
}
Ok(Event::End(ref _e)) => break,
Ok(Event::Eof) => break,
_ => (),
@ -107,7 +102,7 @@ fn read<'a>(mut reader: &mut Reader<&'a [u8]>, depth: u64) -> Value {
}
debug!("values to return: {:?}", values);
if node.len() > 0 {
if !node.is_empty() {
// If we had collected some text along the way, that needs to be inserted
// so we don't lose it
let mut index = 0;
@ -130,9 +125,7 @@ fn read<'a>(mut reader: &mut Reader<&'a [u8]>, depth: u64) -> Value {
match values.len() {
0 => Value::Null,
1 => values.pop().unwrap(),
_ => {
Value::Array(values)
}
_ => Value::Array(values),
}
}
@ -159,25 +152,19 @@ mod tests {
#[test]
fn single_node() {
json_eq(
json!({"e" : null}),
to_json("<e></e>")
);
json_eq(json!({ "e": null }), to_json("<e></e>"));
}
#[test]
fn node_with_text() {
json_eq(
json!({"e" : "foo"}),
to_json("<e>foo</e>")
);
json_eq(json!({"e" : "foo"}), to_json("<e>foo</e>"));
}
#[test]
fn node_with_attr() {
json_eq(
json!({"e" : {"@name":"value"}}),
to_json("<e name=\"value\"></e>")
to_json("<e name=\"value\"></e>"),
);
}
@ -185,7 +172,7 @@ mod tests {
fn node_with_attr_and_text() {
json_eq(
json!({"e": {"@name":"value", "#text" : "text"}}),
to_json(r#"<e name="value">text</e>"#)
to_json(r#"<e name="value">text</e>"#),
);
}
@ -193,13 +180,13 @@ mod tests {
fn node_with_children() {
json_eq(
json!(
{
"e":{
"a":"text1",
"b":"text2"
}
}),
to_json(r#"<e> <a>text1</a> <b>text2</b> </e>"#)
{
"e":{
"a":"text1",
"b":"text2"
}
}),
to_json(r#"<e> <a>text1</a> <b>text2</b> </e>"#),
);
}
@ -207,69 +194,72 @@ mod tests {
fn node_with_multiple_identical_children() {
json_eq(
json!({
"e":{"a":[
"text",
"text"
]}
}),
to_json(r#"<e><a>text</a><a>text</a></e>"#)
"e":{"a":[
"text",
"text"
]}
}),
to_json(r#"<e><a>text</a><a>text</a></e>"#),
);
}
#[test]
fn node_with_N_identical_children() {
fn node_with_n_identical_children() {
json_eq(
json!({
"e":{"a":[
"text1",
"text2",
"text3"
]}
}),
to_json(r#"<e><a>text1</a><a>text2</a><a>text3</a></e>"#)
"e":{"a":[
"text1",
"text2",
"text3"
]}
}),
to_json(r#"<e><a>text1</a><a>text2</a><a>text3</a></e>"#),
);
}
#[test]
fn node_with_text_and_child() {
json_eq(json!(
json_eq(
json!(
{
"e":{
"#text":"lol",
"a":"text"
}
}),
to_json(r#"<e> lol <a>text</a></e>"#)
to_json(r#"<e> lol <a>text</a></e>"#),
);
}
#[test]
fn node_with_just_text() {
json_eq(json!(
json_eq(
json!(
{
"a":"hello"
}),
to_json(r#"<a>hello</a>"#)
to_json(r#"<a>hello</a>"#),
);
}
#[test]
fn node_with_attrs_and_text() {
json_eq(json!(
json_eq(
json!(
{
"a":{
"@x":"y",
"#text":"hello"
}
}),
to_json(r#"<a x="y">hello</a>"#)
to_json(r#"<a x="y">hello</a>"#),
);
}
#[test]
fn nested_nodes_with_attrs() {
json_eq(json!(
json_eq(
json!(
{
"a":{
"@id":"a",
@ -279,7 +269,7 @@ mod tests {
}
}
}),
to_json(r#"<a id="a"><b id="b">hey!</b></a>"#)
to_json(r#"<a id="a"><b id="b">hey!</b></a>"#),
);
}
@ -298,17 +288,19 @@ mod tests {
#[test]
fn node_with_empty_attrs() {
json_eq(json!(
json_eq(
json!(
{
"x":{"@u":""}
}),
to_json(r#"<x u=""/>"#)
to_json(r#"<x u=""/>"#),
);
}
#[test]
fn some_basic_html() {
json_eq(json!(
json_eq(
json!(
{
"html":{
"head":{
@ -321,14 +313,16 @@ mod tests {
"body":null
}
}),
to_json(r#"<html><head><title>Xml/Json</title><meta name="x" content="y"/></head><body/></html>"#)
to_json(
r#"<html><head><title>Xml/Json</title><meta name="x" content="y"/></head><body/></html>"#,
),
);
}
#[test]
fn more_complex_html() {
json_eq(json!(
json_eq(
json!(
{
"ol":{
"@class":"xoxo",
@ -353,23 +347,27 @@ mod tests {
]
}
}),
to_json(r#"<ol class="xoxo"><li>Subject 1 <ol><li>subpoint a</li><li>subpoint b</li></ol></li><li><span>Subject 2</span><ol compact="compact"><li>subpoint c</li><li>subpoint d</li></ol></li></ol>"#)
);
to_json(
r#"<ol class="xoxo"><li>Subject 1 <ol><li>subpoint a</li><li>subpoint b</li></ol></li><li><span>Subject 2</span><ol compact="compact"><li>subpoint c</li><li>subpoint d</li></ol></li></ol>"#,
),
);
}
#[test]
fn node_with_cdata() {
json_eq(json!(
json_eq(
json!(
{
"e":{"#cdata":" .. some data .. "}
}),
to_json(r#"<e><![CDATA[ .. some data .. ]]></e>"#)
to_json(r#"<e><![CDATA[ .. some data .. ]]></e>"#),
);
}
#[test]
fn node_with_cdata_and_siblings() {
json_eq(json!(
json_eq(
json!(
{
"e":{
"a":null,
@ -377,7 +375,7 @@ mod tests {
"b":null
}
}),
to_json(r#"<e><a/><![CDATA[ .. some data .. ]]><b/></e>"#)
to_json(r#"<e><a/><![CDATA[ .. some data .. ]]><b/></e>"#),
);
}
@ -396,7 +394,8 @@ mod tests {
#[test]
fn node_with_child_cdata_and_text() {
json_eq(json!(
json_eq(
json!(
{
"e":{
"#text":"some text",
@ -404,7 +403,7 @@ mod tests {
"a":null
}
}),
to_json(r#"<e> some text <![CDATA[ .. some data .. ]]><a/></e>"#)
to_json(r#"<e> some text <![CDATA[ .. some data .. ]]><a/></e>"#),
);
}