Skip to main content

lux_lib/rockspec/
deploy.rs

1use super::platform;
2use std::convert::Infallible;
3
4use serde::Deserialize;
5
6use super::PerPlatform;
7
8/// An undocumented part of the rockspec format.
9///
10/// Specifies additional install options
11#[derive(Clone, Debug, PartialEq, Deserialize, lux_macros::AsLuaKV)]
12#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
13#[display_lua(key = "deploy")]
14pub struct Spec {
15    /// Whether to wrap installed Lua bin scripts to be executed with
16    /// the detected or configured Lua installation.
17    /// Defaults to `true`.
18    #[serde(default = "default_wrap_bin_scripts")]
19    pub wrap_bin_scripts: bool,
20}
21
22impl Default for Spec {
23    fn default() -> Self {
24        Self {
25            wrap_bin_scripts: true,
26        }
27    }
28}
29
30impl platform::PartialOverride for Spec {
31    type Err = Infallible;
32
33    fn apply_overrides(&self, override_spec: &Self) -> Result<Self, Self::Err> {
34        Ok(Self {
35            wrap_bin_scripts: override_spec.wrap_bin_scripts,
36        })
37    }
38}
39
40impl platform::Overridable for Spec {
41    type Err = Infallible;
42
43    fn on_nil<T>() -> Result<PerPlatform<T>, <Self as platform::Overridable>::Err>
44    where
45        T: platform::Overridable,
46        T: Default,
47    {
48        Ok(PerPlatform::default())
49    }
50}
51
52fn default_wrap_bin_scripts() -> bool {
53    true
54}
55
56#[cfg(test)]
57mod tests {
58    use crate::rockspec::display::AsLuaKV;
59
60    use super::*;
61
62    fn eval_lua_global<T: serde::de::DeserializeOwned>(code: &str, key: &'static str) -> T {
63        use ottavino::{Closure, Executor, Fuel, Lua};
64        use ottavino_util::serde::from_value;
65        Lua::core()
66            .try_enter(|ctx| {
67                let closure = Closure::load(ctx, None, code.as_bytes())?;
68                let executor = Executor::start(ctx, closure.into(), ());
69                executor.step(ctx, &mut Fuel::with(i32::MAX))?;
70                from_value(ctx.globals().get_value(ctx, key)).map_err(ottavino::Error::from)
71            })
72            .unwrap()
73    }
74
75    #[test]
76    pub fn deploy_spec_roundtrip_true() {
77        let spec = Spec {
78            wrap_bin_scripts: true,
79        };
80        let lua = spec.display_lua().to_string();
81        let restored: Spec = eval_lua_global(&lua, "deploy");
82        assert_eq!(spec, restored);
83    }
84
85    #[test]
86    pub fn deploy_spec_roundtrip_false() {
87        let spec = Spec {
88            wrap_bin_scripts: false,
89        };
90        let lua = spec.display_lua().to_string();
91        let restored: Spec = eval_lua_global(&lua, "deploy");
92        assert_eq!(spec, restored);
93    }
94}