1
0
dust/dust-lang/src/native_function.rs

44 lines
1.2 KiB
Rust
Raw Normal View History

2024-10-30 07:08:25 +00:00
use serde::{Deserialize, Serialize};
const PANIC: u8 = 0b0000_0000;
const TO_STRING: u8 = 0b0000_0001;
const WRITE: u8 = 0b0000_0010;
const WRITE_LINE: u8 = 0b0000_0011;
2024-10-30 07:08:25 +00:00
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum NativeFunction {
Panic = PANIC as isize,
ToString = TO_STRING as isize,
Write = WRITE as isize,
2024-10-30 07:08:25 +00:00
WriteLine = WRITE_LINE as isize,
}
impl From<u8> for NativeFunction {
fn from(byte: u8) -> Self {
match byte {
PANIC => NativeFunction::Panic,
TO_STRING => NativeFunction::ToString,
WRITE => NativeFunction::Write,
2024-10-30 07:08:25 +00:00
WRITE_LINE => NativeFunction::WriteLine,
_ => {
if cfg!(test) {
panic!("Invalid native function byte: {}", byte)
2024-10-30 07:08:25 +00:00
} else {
NativeFunction::Panic
}
}
}
}
}
impl From<NativeFunction> for u8 {
fn from(native_function: NativeFunction) -> Self {
match native_function {
NativeFunction::Panic => PANIC,
NativeFunction::ToString => TO_STRING,
NativeFunction::Write => WRITE,
2024-10-30 07:08:25 +00:00
NativeFunction::WriteLine => WRITE_LINE,
}
}
}