1use std::{collections::HashMap, convert::TryFrom, error::Error, ffi::CString};
2
3use crate::{
4 bindings::{mono_string_to_utf8, MonoString},
5 object::Object,
6};
7
8#[derive(Clone, Debug)]
9pub enum Value {
10 Void(()),
11 Bool(bool),
12 UInt8(u8),
13 Int8(i8),
14 Char(char),
15 UInt16(u16),
16 Int16(i16),
17 UInt32(u32),
18 Int32(i32),
19 UInt64(u64),
20 Int64(i64),
21 Size(isize),
22 USize(usize),
23 Float(f32),
24 Double(f64),
25 Str(String),
26 Object(Object),
27 Array(Vec<Value>),
28 Dict(HashMap<Value, Value>),
29 Enum(String, Box<Value>),
30 Ptr(isize),
31}
32
33impl TryFrom<Object> for Value {
34 type Error = Box<dyn Error>;
35
36 fn try_from(object: Object) -> Result<Self, Self::Error> {
37 let value_string_object = object.mono_ptr as *mut MonoString;
39 let value_string = unsafe { mono_string_to_utf8(value_string_object) };
40 let value_string = unsafe { CString::from_raw(value_string) };
41
42 Ok(Value::Str(String::from(value_string.to_str()?)))
43 }
44}