summaryrefslogtreecommitdiff
path: root/src/state/view/mode.rs
blob: 63e6e5b6317aed897fea230c8a6d5d216d85ef39 (plain)
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
use mlua::{FromLua, IntoLua};

#[derive(Default, Debug, Clone, Copy)]
pub enum Mode {
    #[default]
    Normal,
    Visual,
    Insert,
    Command,
}

impl<'lua> FromLua<'lua> for Mode {
    fn from_lua(
        value: mlua::prelude::LuaValue<'lua>,
        _lua: &'lua mlua::prelude::Lua,
    ) -> mlua::prelude::LuaResult<Self> {
        if value.is_string() {
            match value.as_str().unwrap().to_lowercase().as_ref() {
                "normal" => Ok(Mode::Normal),
                "visual" => Ok(Mode::Visual),
                "insert" => Ok(Mode::Insert),
                "command" => Ok(Mode::Command),
                _ => Err(mlua::Error::runtime(
                    "mode needs to be 'normal', 'visual', 'insert' or 'command'",
                )),
            }
        } else {
            Err(mlua::Error::runtime(
                "mode needs to be 'normal', 'visual' 'insert', or 'command'",
            ))
        }
    }
}

impl<'lua> IntoLua<'lua> for Mode {
    fn into_lua(
        self,
        lua: &'lua mlua::prelude::Lua,
    ) -> mlua::prelude::LuaResult<mlua::prelude::LuaValue<'lua>> {
        match self {
            Mode::Normal => "normal",
            Mode::Visual => "visual",
            Mode::Insert => "insert",
            Mode::Command => "command",
        }
        .into_lua(lua)
    }
}