1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use core::{convert::TryFrom, fmt::Display, fmt, fmt::Formatter};

/// A dynamically typed value that may be passed back and forth across the
/// runtime.
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
#[non_exhaustive]
pub enum Value {
    Byte(u8),
    Short(i16),
    Integer(i32),
    Float(f32),
    SignedByte(i8),
}

impl Value {
    /// Get a buffer big enough to be used with [`Value::to_le_bytes()`].
    pub const fn buffer() -> [u8; core::mem::size_of::<Value>()] {
        [0; core::mem::size_of::<Value>()]
    }

    pub fn from_le_bytes(ty: Type, bytes: &[u8]) -> Option<Self> {
        match ty {
            Type::Byte => bytes.get(0).copied().map(Value::Byte),
            Type::SignedByte => {
                if let [byte, ..] = bytes {
                    Some(Value::SignedByte(i8::from_le_bytes([*byte])))
                } else {
                    None
                }
            },
            Type::Short => {
                const LEN: usize = core::mem::size_of::<i16>();

                bytes.get(..LEN).map(|bytes| {
                    let mut buffer = [0; LEN];
                    buffer.copy_from_slice(bytes);
                    Value::Short(i16::from_le_bytes(buffer))
                })
            },
            Type::Integer => {
                const LEN: usize = core::mem::size_of::<i32>();

                bytes.get(..LEN).map(|bytes| {
                    let mut buffer = [0; LEN];
                    buffer.copy_from_slice(bytes);
                    Value::Integer(i32::from_le_bytes(buffer))
                })
            },
            Type::Float => {
                const LEN: usize = core::mem::size_of::<f32>();

                bytes.get(..LEN).map(|bytes| {
                    let mut buffer = [0; LEN];
                    buffer.copy_from_slice(bytes);
                    Value::Float(f32::from_le_bytes(buffer))
                })
            },
        }
    }

    /// Write this [`Value`]'s underlying value to the start of the provided
    /// buffer, returning the number of bytes written.
    ///
    /// The buffer should have at least `core::mem::size_of::<Value>()` bytes.
    /// You can use the [`Value::buffer()`] helper for creating an adequately
    /// sized buffer.
    pub fn to_le_bytes(self, buffer: &mut [u8]) -> usize {
        match self {
            Value::Byte(b) => {
                buffer[0] = b;
                1
            },
            Value::SignedByte(b) => {
                buffer[..1].copy_from_slice(&b.to_le_bytes());
                1
            },
            Value::Short(short) => {
                let bytes = short.to_le_bytes();
                buffer[..bytes.len()].copy_from_slice(&bytes);
                bytes.len()
            },
            Value::Integer(int) => {
                let bytes = int.to_le_bytes();
                buffer[..bytes.len()].copy_from_slice(&bytes);
                bytes.len()
            },
            Value::Float(float) => {
                let bytes = float.to_le_bytes();
                buffer[..bytes.len()].copy_from_slice(&bytes);
                bytes.len()
            },
        }
    }

    pub fn ty(&self) -> Type {
        match self {
            Value::Byte(_) => Type::Byte,
            Value::SignedByte(_) => Type::SignedByte,
            Value::Short(_) => Type::Short,
            Value::Integer(_) => Type::Integer,
            Value::Float(_) => Type::Float,
        }
    }
}

impl Display for Value {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Value::Byte(b) => write!(f, "{}_u8", b),
            Value::SignedByte(b) => write!(f, "{}_i8", b),
            Value::Short(s) => write!(f, "{}_i16", s),
            Value::Integer(i) => write!(f, "{}_i32", i),
            Value::Float(float) => write!(f, "{:.1}", float),
        }
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[repr(u32)]
#[non_exhaustive]
pub enum Type {
    /// A 32-bit signed integer.
    Integer = 1,
    /// A 32-bit floating point number.
    Float = 2,
    /// An 8-bit unsigned integer.
    Byte = 5,
    /// A 16-bit signed integer.
    Short = 6,
    // Note: Enum discriminant are important here. We want to stay
    // compatible with PARAM_TYPE so the mobile runtime isn't broken.
    //
    // https://github.com/hotg-ai/runic_mobile/blob/94f9e72d6de8bd57c004952dc3ba31adc7603381/ios/Runner/hmr/hmr.hpp#L23-L29
    //
    // Don't forget to update TryFrom if you add new variants!
    //
    // We *could* use #[derive(FromPrimitive)] to automate things, but I'd
    // prefer not to add a proc-macro dependency to the crate that every
    // single rune or proc block will depend on.
    SignedByte = 7,
}

impl From<Type> for u32 {
    fn from(t: Type) -> Self { t as u32 }
}

impl TryFrom<u32> for Type {
    type Error = ();

    fn try_from(value: u32) -> Result<Self, Self::Error> {
        match value {
            1 => Ok(Type::Integer),
            2 => Ok(Type::Float),
            5 => Ok(Type::Byte),
            6 => Ok(Type::Short),
            _ => Err(()),
        }
    }
}

/// A Rust primitive which has a corresponding [`Type`] and can be converted to
/// or from a [`Value`].
pub trait AsType
where
    Self: Into<Value>,
    Self: TryFrom<Value>,
{
    /// The corresponding [`Type`] variant.
    const TYPE: Type;
}

macro_rules! impl_as_type {
    ($($type:ty => $variant:ident),* $(,)?) => {
        $(
            impl AsType for $type {
                const TYPE: Type = Type::$variant;
            }

            impl From<$type> for Value {
                fn from(other: $type) -> Value {
                    Value::$variant(other)
                }
            }

            impl TryFrom<Value> for $type {
                type Error = InvalidConversionError;

                fn try_from(value: Value) -> Result<Self, Self::Error> {
                    match value {
                        Value::$variant(v) => Ok(v),
                        _ => Err(InvalidConversionError {
                            value,
                            target_type: Type::$variant,
                        }),
                    }
                }
            }
        )*
    }
}

impl_as_type!(u8 => Byte, i16 => Short, i32 => Integer, f32 => Float, i8 => SignedByte);

#[derive(Debug, Copy, Clone, PartialEq)]
pub struct InvalidConversionError {
    pub value: Value,
    pub target_type: Type,
}

impl Display for InvalidConversionError {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Unable to convert {} ({:?}) to a {:?}",
            self.value,
            self.value.ty(),
            self.target_type
        )
    }
}

#[cfg(feature = "std")]
impl std::error::Error for InvalidConversionError {}