use super::expression::Type;
use super::rule::RuleCompiler;
#[derive(Clone, Debug)]
pub(crate) struct ExternalSymbol {
pub name: String,
pub default_value: ExternalValue,
}
#[derive(Clone, Debug)]
pub enum ExternalValue {
Integer(i64),
Float(f64),
Bytes(Vec<u8>),
Boolean(bool),
}
impl ExternalValue {
pub(super) fn get_type(&self) -> Type {
match self {
Self::Integer(_) => Type::Integer,
Self::Float(_) => Type::Float,
Self::Bytes(_) => Type::Bytes,
Self::Boolean(_) => Type::Boolean,
}
}
}
pub(super) fn get_external_symbol<'a>(
compiler: &'a RuleCompiler,
name: &str,
) -> Option<(usize, &'a ExternalValue)> {
for (index, sym) in compiler.external_symbols.iter().enumerate() {
if sym.name == name {
return Some((index, &sym.default_value));
}
}
None
}
impl From<i64> for ExternalValue {
fn from(v: i64) -> Self {
Self::Integer(v)
}
}
impl From<f64> for ExternalValue {
fn from(v: f64) -> Self {
Self::Float(v)
}
}
impl From<bool> for ExternalValue {
fn from(v: bool) -> Self {
Self::Boolean(v)
}
}
macro_rules! impl_into_bytes {
($ty:ty) => {
impl From<$ty> for ExternalValue {
fn from(v: $ty) -> Self {
Self::Bytes(v.into())
}
}
};
}
impl_into_bytes!(Vec<u8>);
impl_into_bytes!(&[u8]);
impl_into_bytes!(String);
impl_into_bytes!(&str);
#[cfg(test)]
mod tests {
use super::*;
use crate::test_helpers::test_type_traits;
#[test]
fn test_types_traits() {
test_type_traits(ExternalSymbol {
name: "a".to_owned(),
default_value: ExternalValue::Integer(0),
});
test_type_traits(ExternalValue::Integer(0));
}
}