Skip to main content

mirascript_vm/value/
type_enum.rs

1use std::fmt;
2
3/// A MiraScript runtime value category.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5pub enum MiraType {
6    /// The `nil` value.
7    Nil,
8    /// A boolean value.
9    Boolean,
10    /// A numeric value.
11    Number,
12    /// A string value.
13    String,
14    /// An array value.
15    Array,
16    /// A record value.
17    Record,
18    /// A callable function.
19    Function,
20    /// A module value.
21    Module,
22    /// A reserved external value.
23    Extern,
24}
25
26impl MiraType {
27    /// Return the MiraScript type name for this category.
28    #[inline]
29    pub const fn name(self) -> &'static str {
30        match self {
31            Self::Nil => "nil",
32            Self::Boolean => "boolean",
33            Self::Number => "number",
34            Self::String => "string",
35            Self::Array => "array",
36            Self::Record => "record",
37            Self::Function => "function",
38            Self::Module => "module",
39            Self::Extern => "extern",
40        }
41    }
42}
43
44impl fmt::Display for MiraType {
45    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
46        formatter.write_str(self.name())
47    }
48}
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn type_display() {
56        assert_eq!(MiraType::Nil.to_string(), "nil");
57        assert_eq!(MiraType::Boolean.to_string(), "boolean");
58        assert_eq!(MiraType::Number.to_string(), "number");
59        assert_eq!(MiraType::String.to_string(), "string");
60        assert_eq!(MiraType::Array.to_string(), "array");
61        assert_eq!(MiraType::Record.to_string(), "record");
62        assert_eq!(MiraType::Function.to_string(), "function");
63        assert_eq!(MiraType::Module.to_string(), "module");
64        assert_eq!(MiraType::Extern.to_string(), "extern");
65    }
66}