//! Shared string utilities for UDFs.
//!
//! Provides helpers for working with Utf8, LargeUtf8, and Utf8View scalars
//! so that UDFs can accept all string types from sources like Parquet.

use datafusion::arrow::datatypes::DataType;
use datafusion::common::{exec_err, Result, ScalarValue};

/// The three string DataTypes that UDF signatures should accept.
pub const STRING_TYPES: [DataType; 3] = [DataType::Utf8, DataType::LargeUtf8, DataType::Utf8View];

/// Extract an `Option<&str>` from a string scalar value (any variant).
pub fn scalar_to_str(scalar: &ScalarValue) -> Result<Option<&str>> {
    match scalar {
        ScalarValue::Utf8(s) | ScalarValue::LargeUtf8(s) | ScalarValue::Utf8View(s) => {
            Ok(s.as_deref())
        }
        ScalarValue::Null => Ok(None),
        other => exec_err!("expected string type, got {}", other.data_type()),
    }
}
