use crate::dotnet::signature::ElementType;
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "reflect", derive(facet::Facet))]
pub struct Constant {
pub element_type: ElementType,
pub value: Vec<u8>,
}
impl Constant {
pub fn decode(&self) -> Option<ConstantValue> {
let v = &self.value;
let le = |n: usize| v.get(..n);
Some(match self.element_type {
ElementType::Boolean => ConstantValue::Bool(*v.first()? != 0),
ElementType::Char => ConstantValue::Char(u16::from_le_bytes(le(2)?.try_into().ok()?)),
ElementType::I1 => ConstantValue::I1(*v.first()? as i8),
ElementType::U1 => ConstantValue::U1(*v.first()?),
ElementType::I2 => ConstantValue::I2(i16::from_le_bytes(le(2)?.try_into().ok()?)),
ElementType::U2 => ConstantValue::U2(u16::from_le_bytes(le(2)?.try_into().ok()?)),
ElementType::I4 => ConstantValue::I4(i32::from_le_bytes(le(4)?.try_into().ok()?)),
ElementType::U4 => ConstantValue::U4(u32::from_le_bytes(le(4)?.try_into().ok()?)),
ElementType::I8 => ConstantValue::I8(i64::from_le_bytes(le(8)?.try_into().ok()?)),
ElementType::U8 => ConstantValue::U8(u64::from_le_bytes(le(8)?.try_into().ok()?)),
ElementType::R4 => ConstantValue::R4(f32::from_le_bytes(le(4)?.try_into().ok()?)),
ElementType::R8 => ConstantValue::R8(f64::from_le_bytes(le(8)?.try_into().ok()?)),
ElementType::String => {
let units: Vec<u16> = v
.as_chunks::<2>()
.0
.iter()
.map(|c| u16::from_le_bytes(*c))
.collect();
ConstantValue::String(String::from_utf16(&units).ok()?)
}
ElementType::Class => ConstantValue::Null,
_ => return None,
})
}
pub fn as_i64(&self) -> Option<i64> {
self.decode()?.as_i64()
}
pub fn as_string(&self) -> Option<String> {
match self.decode()? {
ConstantValue::String(s) => Some(s),
_ => None,
}
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "reflect", derive(facet::Facet))]
#[cfg_attr(feature = "reflect", repr(C))]
pub enum ConstantValue {
Bool(bool),
Char(u16),
I1(i8),
U1(u8),
I2(i16),
U2(u16),
I4(i32),
U4(u32),
I8(i64),
U8(u64),
R4(f32),
R8(f64),
String(String),
Null,
}
impl ConstantValue {
pub fn as_i64(&self) -> Option<i64> {
Some(match *self {
Self::Bool(b) => b as i64,
Self::Char(c) => c as i64,
Self::I1(v) => v as i64,
Self::U1(v) => v as i64,
Self::I2(v) => v as i64,
Self::U2(v) => v as i64,
Self::I4(v) => v as i64,
Self::U4(v) => v as i64,
Self::I8(v) => v,
Self::U8(v) => v as i64,
_ => return None,
})
}
pub fn as_string(&self) -> Option<&str> {
match self {
Self::String(s) => Some(s),
_ => None,
}
}
}
impl std::fmt::Display for ConstantValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Bool(v) => write!(f, "{v}"),
Self::Char(v) => write!(f, "{v}"),
Self::I1(v) => write!(f, "{v}"),
Self::U1(v) => write!(f, "{v}"),
Self::I2(v) => write!(f, "{v}"),
Self::U2(v) => write!(f, "{v}"),
Self::I4(v) => write!(f, "{v}"),
Self::U4(v) => write!(f, "{v}"),
Self::I8(v) => write!(f, "{v}"),
Self::U8(v) => write!(f, "{v}"),
Self::R4(v) => write!(f, "{v}"),
Self::R8(v) => write!(f, "{v}"),
Self::String(v) => write!(f, "{v:?}"),
Self::Null => f.write_str("null"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn constant(element_type: ElementType, value: Vec<u8>) -> Constant {
Constant {
element_type,
value,
}
}
#[test]
fn decodes_typed_values() {
assert_eq!(
constant(ElementType::I4, 42i32.to_le_bytes().to_vec()).decode(),
Some(ConstantValue::I4(42))
);
assert_eq!(
constant(ElementType::Boolean, vec![1]).decode(),
Some(ConstantValue::Bool(true))
);
assert_eq!(
constant(ElementType::R8, 1.5f64.to_le_bytes().to_vec()).decode(),
Some(ConstantValue::R8(1.5))
);
assert_eq!(
constant(ElementType::Class, vec![0, 0, 0, 0]).decode(),
Some(ConstantValue::Null)
);
let utf16: Vec<u8> = "hi".encode_utf16().flat_map(u16::to_le_bytes).collect();
let string = constant(ElementType::String, utf16);
assert_eq!(string.decode(), Some(ConstantValue::String("hi".into())));
assert_eq!(string.as_string().as_deref(), Some("hi"));
assert_eq!(
constant(ElementType::I4, 42i32.to_le_bytes().to_vec()).as_i64(),
Some(42)
);
assert_eq!(ConstantValue::I4(42).to_string(), "42");
assert_eq!(ConstantValue::Bool(false).to_string(), "false");
assert_eq!(ConstantValue::Null.to_string(), "null");
assert_eq!(constant(ElementType::I4, vec![1, 2]).decode(), None);
assert_eq!(constant(ElementType::Void, vec![]).decode(), None);
}
}