Export the value_to_string helper function

This commit is contained in:
Kerollmops 2020-05-18 12:40:33 +02:00
parent 65ed2dcc1b
commit 2558ce9a00
No known key found for this signature in database
GPG Key ID: 92ADA4E935E71FA4

View File

@ -118,29 +118,6 @@ fn index_value(
value: &Value, value: &Value,
) -> Option<usize> ) -> Option<usize>
{ {
fn value_to_string(string: &mut String, value: &Value) {
match value {
Value::Null => (),
Value::Bool(boolean) => { let _ = write!(string, "{}", &boolean); },
Value::Number(number) => { let _ = write!(string, "{}", &number); },
Value::String(text) => string.push_str(&text),
Value::Array(array) => {
for value in array {
value_to_string(string, value);
let _ = string.write_str(". ");
}
},
Value::Object(object) => {
for (key, value) in object {
string.push_str(key);
let _ = string.write_str(". ");
value_to_string(string, value);
let _ = string.write_str(". ");
}
},
}
}
match value { match value {
Value::Null => None, Value::Null => None,
Value::Bool(boolean) => { Value::Bool(boolean) => {
@ -158,20 +135,48 @@ fn index_value(
Some(number_of_words) Some(number_of_words)
}, },
Value::Array(_) => { Value::Array(_) => {
let mut text = String::new(); let text = value_to_string(value);
value_to_string(&mut text, value);
let number_of_words = indexer.index_text(document_id, indexed_pos, &text); let number_of_words = indexer.index_text(document_id, indexed_pos, &text);
Some(number_of_words) Some(number_of_words)
}, },
Value::Object(_) => { Value::Object(_) => {
let mut text = String::new(); let text = value_to_string(value);
value_to_string(&mut text, value);
let number_of_words = indexer.index_text(document_id, indexed_pos, &text); let number_of_words = indexer.index_text(document_id, indexed_pos, &text);
Some(number_of_words) Some(number_of_words)
}, },
} }
} }
// TODO move this helper functions elsewhere
fn value_to_string(value: &Value) -> String {
fn internal_value_to_string(string: &mut String, value: &Value) {
match value {
Value::Null => (),
Value::Bool(boolean) => { let _ = write!(string, "{}", &boolean); },
Value::Number(number) => { let _ = write!(string, "{}", &number); },
Value::String(text) => string.push_str(&text),
Value::Array(array) => {
for value in array {
internal_value_to_string(string, value);
let _ = string.write_str(". ");
}
},
Value::Object(object) => {
for (key, value) in object {
string.push_str(key);
let _ = string.write_str(". ");
internal_value_to_string(string, value);
let _ = string.write_str(". ");
}
},
}
}
let mut string = String::new();
internal_value_to_string(&mut string, value);
string
}
// TODO move this helper functions elsewhere // TODO move this helper functions elsewhere
fn value_to_number(value: &Value) -> Option<Number> { fn value_to_number(value: &Value) -> Option<Number> {
use std::str::FromStr; use std::str::FromStr;