Skip to main content

hive_console_sdk/expressions/values/
string.rs

1use std::string::FromUtf8Error;
2
3use crate::expressions::FromVrlValue;
4use vrl::core::Value as VrlValue;
5
6/// Error type for String conversion failures
7#[derive(Debug, thiserror::Error, Clone)]
8pub enum StringConversionError {
9    #[error("Failed to convert bytes to UTF-8 string: {0}")]
10    InvalidUtf8(#[from] FromUtf8Error),
11
12    #[error("Cannot convert {type_name} to string")]
13    UnsupportedType { type_name: String },
14}
15
16impl FromVrlValue for String {
17    type Error = StringConversionError;
18
19    #[inline]
20    fn from_vrl_value(value: VrlValue) -> Result<Self, Self::Error> {
21        match value {
22            VrlValue::Bytes(b) => Ok(String::from_utf8(b.to_vec())?),
23            VrlValue::Integer(i) => Ok(i.to_string()),
24            VrlValue::Float(f) => Ok(f.to_string()),
25            VrlValue::Boolean(b) => Ok(if b { "true" } else { "false" }.to_string()),
26            VrlValue::Null => Ok(String::new()),
27            other => Err(StringConversionError::UnsupportedType {
28                type_name: other.kind().to_string(),
29            }),
30        }
31    }
32}