Skip to content

Commit

Permalink
Remove unnecessary method
Browse files Browse the repository at this point in the history
  • Loading branch information
tomekstrzeszkowski committed Jul 30, 2024
1 parent 241d470 commit 4b1eb35
Showing 1 changed file with 8 additions and 16 deletions.
24 changes: 8 additions & 16 deletions src/json_to_csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ impl Builder {
let res: String;
match v {
Value::Object(_) => {
res = item_to_csv(v);
let v = vec![v];
res = items_to_csv(v);
},
Value::Array(_) => {
let v: Vec<Value> = v.as_array().unwrap().clone();
res = items_to_csv(v);
},
_ => res = String::from("JSON value is neither an object nor array")
Expand Down Expand Up @@ -80,30 +82,20 @@ fn create_rows(v: &serde_json::Map<String, serde_json::Value>, header_items: &Ve
row.join(",") + "\n"
}

pub fn items_to_csv(v: Value) -> String {
let mut csv = String::new();
pub fn items_to_csv(v: Vec<Value>) -> String {
let mut csv: String;
let mut header_items: Vec<&String> = Vec::new();
let v: Vec<Value> = v.as_array().unwrap().clone();
for item in &v {
let h = collect_from_object(item);
extend_header(&mut header_items, h);
}
csv += &create_header(&header_items);
csv = create_header(&header_items);
for item in &v {
csv += &create_rows(&item.as_object().unwrap(), &header_items);
}
csv
}

pub fn item_to_csv(v: Value) -> String {
let v = v.as_object().unwrap().clone();
let mut csv = String::new();
let mut header_items: Vec<&String> = Vec::new();
extend_header(&mut header_items, v.keys().collect());
csv += &create_header(&header_items);
csv + &create_rows(&v, &header_items)
}


#[cfg(test)]
mod tests {
Expand All @@ -126,13 +118,13 @@ mod tests {
#[test]
fn simple_object() {
let value = serde_json::from_str(&String::from("{\"a\": 4, \"b\": 8}")).unwrap();
let parsed = item_to_csv(value);
let parsed = items_to_csv(vec![value]);
assert_eq!(format!("{}", parsed), "a,b\n4,8\n");
}
#[test]
fn object_with_null() {
let value = serde_json::from_str(&String::from("{\"a\": null, \"b\": 8}")).unwrap();
let parsed = item_to_csv(value);
let parsed = items_to_csv(vec![value]);
assert_eq!(format!("{}", parsed), "a,b\nnull,8\n");
}
}

0 comments on commit 4b1eb35

Please sign in to comment.