use serde_json::{Value as Json, json};
use crate::ir::value::LogicalValue;
pub(super) fn value_to_json(v: &LogicalValue) -> Json {
match v {
LogicalValue::Null => Json::Null,
LogicalValue::Bool(b) => Json::Bool(*b),
LogicalValue::Int(i) => Json::Number((*i).into()),
LogicalValue::Float(f) => serde_json::Number::from_f64(*f)
.map(Json::Number)
.unwrap_or(Json::Null),
LogicalValue::String(s) => Json::String(s.clone()),
LogicalValue::Bytes(b) => {
use base64::{Engine as _, engine::general_purpose::STANDARD as B64};
json!({ "$binary": { "base64": B64.encode(b), "subType": "00" } })
}
LogicalValue::Timestamp(t) => json!({ "$date": t.to_rfc3339() }),
LogicalValue::Json(j) => j.clone(),
LogicalValue::Array(values) => Json::Array(values.iter().map(value_to_json).collect()),
}
}
pub(super) fn like_to_regex(pattern: &str) -> String {
let mut out = String::from("^");
for ch in pattern.chars() {
match ch {
'%' => out.push_str(".*"),
'_' => out.push('.'),
'.' | '\\' | '^' | '$' | '*' | '+' | '?' | '(' | ')' | '[' | ']' | '{' | '}' | '|' => {
out.push('\\');
out.push(ch);
}
other => out.push(other),
}
}
out.push('$');
out
}
#[allow(dead_code)]
pub(super) fn regex_escape(s: &str) -> String {
let mut out = String::with_capacity(s.len());
for ch in s.chars() {
if matches!(
ch,
'.' | '\\' | '^' | '$' | '*' | '+' | '?' | '(' | ')' | '[' | ']' | '{' | '}' | '|'
) {
out.push('\\');
}
out.push(ch);
}
out
}