Skip to main content

lux_lib/lua_rockspec/
deploy.rs

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