summaryrefslogtreecommitdiffstats
path: root/plugins/plugin_lua/src/plugin.rs
blob: 188b14127369afdc16a166be05d483e660915865 (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
49
50
51
52
53
54
55
56
57
58
use std::sync::Arc;

use async_trait::async_trait;
use rlua::Lua;
use tedge_api::Plugin;
use tedge_api::PluginError;
use tokio::sync::RwLock;

use crate::config::Config;

#[derive(Debug)]
pub struct LuaPlugin {
    lua: RwLock<Lua>,
}

impl LuaPlugin {
    pub(crate) fn new(config: &Config) -> Self {
        LuaPlugin {
            lua: {
                let lua = rlua::Lua::new();
                let memory_limit = config.memory_limit.map(|bs| {
                    let bs = bs.into_bytesize().as_u64();
                    if (usize::MAX as u64) < bs {
                        usize::MAX
                    } else {
                        bs as usize
                    }
                });

                lua.set_memory_limit(memory_limit);

                RwLock::new(lua)
            },
        }
    }
}

impl tedge_api::plugin::PluginDeclaration for LuaPlugin {
    type HandledMessages = ();
}

#[async_trait]
impl Plugin for LuaPlugin {
    #[tracing::instrument(name = "plugin.lua.start", skip(self))]
    async fn start(&mut self) -> Result<(), PluginError> {
        Ok(())
    }

    #[tracing::instrument(name = "plugin.lua.main", skip(self))]
    async fn main(&self) -> Result<(), PluginError> {
        Ok(())
    }

    #[tracing::instrument(name = "plugin.lua.shutdown", skip(self))]
    async fn shutdown(&mut self) -> Result<(), PluginError> {
        Ok(())
    }
}