vrl/core/
encode_logfmt.rs

1use std::collections::BTreeMap;
2
3use crate::value::{KeyString, Value};
4use serde::Serialize;
5
6use super::encode_key_value::{EncodingError, to_string as encode_key_value};
7
8/// Serialize the input value map into a logfmt string.
9///
10/// # Errors
11///
12/// Returns an `EncodingError` if any of the keys are not strings.
13pub fn encode_map<V: Serialize>(input: &BTreeMap<KeyString, V>) -> Result<String, EncodingError> {
14    encode_key_value(input, &[], "=", " ", true)
15}
16
17/// Serialize the input value into a logfmt string. If the value is not an object,
18/// it is treated as the value of an object where the key is "message".
19///
20/// # Errors
21///
22/// Returns an `EncodingError` if any of the keys are not strings.
23pub fn encode_value(input: &Value) -> Result<String, EncodingError> {
24    if let Some(map) = input.as_object() {
25        encode_map(map)
26    } else {
27        let mut map = BTreeMap::new();
28        map.insert("message".to_string().into(), &input);
29        encode_map(&map)
30    }
31}