use crate::execution_step::ValueAggregate;
use crate::JValue;
use air_interpreter_value::JsonString;
use std::fmt::Display;
use std::fmt::Formatter;
pub(crate) static KEY_FIELD_NAME: &str = "key";
#[derive(Clone, PartialEq, Debug, Eq, Hash)]
pub(crate) enum StreamMapKey {
Str(JsonString),
U64(u64),
I64(i64),
}
impl StreamMapKey {
pub fn from_value(value: JValue) -> Option<Self> {
match value {
JValue::String(s) => Some(StreamMapKey::Str(s)),
JValue::Number(n) if n.is_i64() => Some(StreamMapKey::I64(n.as_i64().unwrap())),
JValue::Number(n) if n.is_u64() => Some(StreamMapKey::U64(n.as_u64().unwrap())),
_ => None,
}
}
pub fn from_value_ref(value: &JValue) -> Option<Self> {
match value {
JValue::String(s) => Some(StreamMapKey::Str(s.clone())),
JValue::Number(n) if n.is_i64() => Some(StreamMapKey::I64(n.as_i64().unwrap())),
JValue::Number(n) if n.is_u64() => Some(StreamMapKey::U64(n.as_u64().unwrap())),
_ => None,
}
}
pub(crate) fn from_kvpair_owned(value: &ValueAggregate) -> Option<Self> {
let object = value.get_result().as_object()?;
let key = object.get(KEY_FIELD_NAME)?.clone();
StreamMapKey::from_value(key)
}
pub(crate) fn to_key(&self) -> JsonString {
match self {
StreamMapKey::Str(s) => s.clone(),
StreamMapKey::U64(n) => format!("{n}").into(),
StreamMapKey::I64(n) => format!("{n}").into(),
}
}
}
impl From<i64> for StreamMapKey {
fn from(value: i64) -> Self {
StreamMapKey::I64(value)
}
}
impl From<u64> for StreamMapKey {
fn from(value: u64) -> Self {
StreamMapKey::U64(value)
}
}
impl From<u32> for StreamMapKey {
fn from(value: u32) -> Self {
StreamMapKey::I64(value.into())
}
}
impl From<&str> for StreamMapKey {
fn from(value: &str) -> Self {
StreamMapKey::Str(value.into())
}
}
impl From<JsonString> for StreamMapKey {
fn from(value: JsonString) -> Self {
StreamMapKey::Str(value)
}
}
impl From<StreamMapKey> for JValue {
fn from(value: StreamMapKey) -> Self {
match value {
StreamMapKey::Str(s) => JValue::string(s),
StreamMapKey::U64(n) => n.into(),
StreamMapKey::I64(n) => n.into(),
}
}
}
impl Display for StreamMapKey {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
StreamMapKey::Str(s) => write!(f, "{}", s),
StreamMapKey::U64(n) => write!(f, "{}", n),
StreamMapKey::I64(n) => write!(f, "{}", n),
}
}
}