aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: 628dcc029149d39206418b6c05b4e4fb33030efd (plain) (blame)
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
use std::fmt::{Display, Formatter, Result};

use rlua::Value;

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum ValueType {
    /// The Lua value `nil`.
    Nil,
    /// The Lua value `true` or `false`.
    Boolean,
    /// A "light userdata" object, equivalent to a raw pointer.
    LightUserData,
    /// An integer number.
    ///
    /// Any Lua number convertible to a `Integer` will be represented as this variant.
    Integer,
    /// A floating point number.
    Number,
    /// An interned string, managed by Lua.
    ///
    /// Unlike Rust strings, Lua strings may not be valid UTF-8.
    String,
    /// Reference to a Lua table.
    Table,
    /// Reference to a Lua function (or closure).
    Function,
    /// Reference to a Lua thread (or coroutine).
    Thread,
    /// Reference to a userdata object that holds a custom type which implements `UserData`.
    /// Special builtin userdata types will be represented as other `Value` variants.
    UserData,
    /// `Error` is a special builtin userdata type.  When received from Lua it is implicitly cloned.
    Error,
}

impl Display for ValueType {
    fn fmt(&self, f: &mut Formatter) -> Result {
        use ValueType::*;

        match self {
            Nil => write!(f, "Nil"),
            Boolean => write!(f, "Boolean"),
            LightUserData => write!(f, "LightUserData"),
            Integer => write!(f, "Integer"),
            Number => write!(f, "Number"),
            String => write!(f, "String"),
            Table => write!(f, "Table"),
            Function => write!(f, "Function"),
            Thread => write!(f, "Thread"),
            UserData => write!(f, "UserData"),
            Error => write!(f, "Error"),
        }
    }
}

pub fn value_type(value: &Value) -> ValueType {
    match value {
        Value::Nil => ValueType::Nil,
        Value::Boolean(_) => ValueType::Boolean,
        Value::LightUserData(_) => ValueType::LightUserData,
        Value::Integer(_) => ValueType::Integer,
        Value::Number(_) => ValueType::Number,
        Value::String(_) => ValueType::String,
        Value::Table(_) => ValueType::Table,
        Value::Function(_) => ValueType::Function,
        Value::Thread(_) => ValueType::Thread,
        Value::UserData(_) => ValueType::UserData,
        Value::Error(_) => ValueType::Error,
    }
}