Skip to content

Commit

Permalink
validate: refactor to_json_instance
Browse files Browse the repository at this point in the history
  • Loading branch information
jqnatividad committed Oct 3, 2024
1 parent 81ec737 commit ad3cfcc
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/cmd/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ Common options:
"#;

use std::{
borrow::Cow,
env,
fs::File,
io::{BufReader, BufWriter, Read, Write},
Expand Down Expand Up @@ -921,27 +922,25 @@ fn to_json_instance(
let mut json_object_map: Map<String, Value> = Map::with_capacity(header_len);

let mut key_string: String;
let mut value_string: String;
for ((key_iter, json_type), value) in header_types.iter().zip(record.iter()) {
key_string = key_iter.to_owned();
for ((key, json_type), value) in header_types.iter().zip(record.iter()) {
key_string = key.to_owned();

if value.is_empty() {
json_object_map.insert(key_string, Value::Null);
continue;
}

let value_str = match simdutf8::basic::from_utf8(value) {
Ok(v) => v,
Err(e) => {
Ok(v) => Cow::Borrowed(v),
Err(_) => {
let s = String::from_utf8_lossy(value);
return fail_encoding_clierror!("CSV value \"{s}\" is not valid UTF-8: {e}");
return fail_encoding_clierror!("CSV value \"{s}\" is not valid UTF-8");
},
};

match *json_type {
JSONtypes::String => {
value_string = value_str.to_owned();
json_object_map.insert(key_string, Value::String(value_string));
json_object_map.insert(key_string, Value::String(value_str.into_owned()));
},
JSONtypes::Number => {
if let Ok(float) = value_str.parse::<f64>() {
Expand Down Expand Up @@ -972,6 +971,7 @@ fn to_json_instance(
}
},
JSONtypes::Unsupported => {
// unreachable because we assigned JSONtypes
unreachable!("we should never get an unsupported JSON type");
},
}
Expand Down

0 comments on commit ad3cfcc

Please sign in to comment.