Skip to main content

lux_lib/rockspec/
mod.rs

1use crate::package;
2use std::{
3    collections::HashMap,
4    ops::{Deref, DerefMut},
5    path::PathBuf,
6};
7
8use itertools::Itertools;
9use miette::Diagnostic;
10use serde::{Deserialize, Serialize};
11use thiserror::Error;
12pub mod build;
13pub mod dependency;
14pub mod deploy;
15pub mod description;
16pub(crate) mod display;
17pub mod external_dependency;
18mod format;
19pub mod lua;
20pub mod lua_dependency;
21pub mod platform;
22pub(crate) mod serde_util;
23pub mod source;
24pub mod test;
25pub mod toml;
26
27pub use format::*;
28pub use platform::PerPlatform;
29
30use crate::config::Config;
31
32#[derive(Debug, Error, Diagnostic)]
33#[non_exhaustive]
34pub enum Error {
35    #[error(transparent)]
36    #[diagnostic(transparent)]
37    LuaRockspec(Box<lua::Error>),
38    #[error(transparent)]
39    #[diagnostic(transparent)]
40    Toml(Box<toml::Error>),
41    #[error(
42        r#"
43The lua version {0} is not supported by {1} version {2}.
44
45HINT: If Lux has auto-detected an incompatible Lua installation,
46      use `--lua-version` to specify the Lua version to use.
47      Valid versions are: '5.1', '5.2', '5.3', '5.4', '5.5', 'jit', 'jit52' and 'luau'.
48"#
49    )]
50    UnsupportedLuaVersion(crate::lua::Version, package::Name, Box<package::Version>),
51    #[error(transparent)]
52    #[diagnostic(transparent)]
53    LuaVersion(#[from] crate::lua::version::Error),
54}
55
56impl From<lua::Error> for Error {
57    fn from(err: lua::Error) -> Self {
58        Self::LuaRockspec(Box::new(err))
59    }
60}
61
62impl From<toml::Error> for Error {
63    fn from(err: toml::Error) -> Self {
64        Self::Toml(Box::new(err))
65    }
66}
67
68pub trait Rockspec {
69    fn package(&self) -> &package::Name;
70    fn version(&self) -> &package::Version;
71    fn description(&self) -> &description::Spec;
72    fn supported_platforms(&self) -> &platform::Support;
73    fn lua(&self) -> &package::VersionReq;
74    fn dependencies(&self) -> &PerPlatform<Vec<lua_dependency::Spec>>;
75    fn build_dependencies(&self) -> &PerPlatform<Vec<lua_dependency::Spec>>;
76    fn external_dependencies(&self) -> &PerPlatform<HashMap<String, external_dependency::Spec>>;
77    fn test_dependencies(&self) -> &PerPlatform<Vec<lua_dependency::Spec>>;
78
79    fn build(&self) -> &PerPlatform<build::Spec>;
80    fn test(&self) -> &PerPlatform<test::Spec>;
81    fn source(&self) -> &PerPlatform<source::Remote>;
82    fn deploy(&self) -> &PerPlatform<deploy::Spec>;
83
84    fn build_mut(&mut self) -> &mut PerPlatform<build::Spec>;
85    fn test_mut(&mut self) -> &mut PerPlatform<test::Spec>;
86    fn source_mut(&mut self) -> &mut PerPlatform<source::Remote>;
87    fn deploy_mut(&mut self) -> &mut PerPlatform<deploy::Spec>;
88
89    fn format(&self) -> &Option<Format>;
90
91    /// Shorthand to extract the binaries that are part of the rockspec.
92    fn binaries(&self) -> RockBinaries {
93        RockBinaries(
94            self.build()
95                .current_platform()
96                .install
97                .bin
98                .keys()
99                .map_into()
100                .collect(),
101        )
102    }
103
104    /// Converts the rockspec to a string that can be uploaded to a luarocks server.
105    fn to_lua_remote_rockspec_string(&self) -> Result<String, Error>;
106}
107
108pub trait LuaVersionCompatibility {
109    /// Ensures that the rockspec is compatible with the specified lua version.
110    /// Returns an error if the rockspec is not compatible.
111    fn validate_lua_version(&self, lua_version: &crate::lua::Version) -> Result<(), Error>;
112
113    /// Ensures that the rockspec is compatible with the lua version established in the config.
114    /// Returns an error if the rockspec is not compatible.
115    fn validate_lua_version_from_config(&self, config: &Config) -> Result<(), Error>;
116
117    /// Ensures that the rockspec is compatible with the lua version established in the config,
118    /// and returns the lua version from the config if it is compatible.
119    fn lua_version_matches(&self, config: &Config) -> Result<crate::lua::Version, Error>;
120
121    /// Checks if the rockspec supports the given lua version.
122    fn supports_lua_version(&self, lua_version: &crate::lua::Version) -> bool;
123
124    /// Returns the lua version required by the rockspec.
125    fn lua_version(&self) -> Option<crate::lua::Version>;
126}
127
128impl<T: Rockspec> LuaVersionCompatibility for T {
129    fn validate_lua_version(&self, version: &crate::lua::Version) -> Result<(), Error> {
130        if self.supports_lua_version(version) {
131            Ok(())
132        } else {
133            Err(Error::UnsupportedLuaVersion(
134                version.clone(),
135                self.package().to_owned(),
136                Box::new(self.version().to_owned()),
137            ))
138        }
139    }
140
141    fn validate_lua_version_from_config(&self, config: &Config) -> Result<(), Error> {
142        let _ = self.lua_version_matches(config)?;
143        Ok(())
144    }
145
146    fn lua_version_matches(&self, config: &Config) -> Result<crate::lua::Version, Error> {
147        let version = crate::lua::Version::from(config)?.clone();
148        if self.supports_lua_version(&version) {
149            Ok(version)
150        } else {
151            Err(Error::UnsupportedLuaVersion(
152                version,
153                self.package().to_owned(),
154                Box::new(self.version().to_owned()),
155            ))
156        }
157    }
158
159    fn supports_lua_version(&self, lua_version: &crate::lua::Version) -> bool {
160        if self.lua().is_luau() {
161            return matches!(lua_version, crate::lua::Version::Luau);
162        }
163        self.lua().matches(&lua_version.as_version())
164    }
165
166    fn lua_version(&self) -> Option<crate::lua::Version> {
167        if self.lua().is_luau() {
168            return Some(crate::lua::Version::Luau);
169        }
170        for (possibility, version) in [
171            ("5.5.0", crate::lua::Version::Lua54),
172            ("5.4.0", crate::lua::Version::Lua54),
173            ("5.3.0", crate::lua::Version::Lua53),
174            ("5.2.0", crate::lua::Version::Lua52),
175            ("5.1.0", crate::lua::Version::Lua51),
176        ] {
177            let possibility = unsafe { possibility.parse().unwrap_unchecked() };
178            if self.lua().matches(&possibility) {
179                return Some(version);
180            }
181        }
182        None
183    }
184}
185
186#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialOrd, Ord, Hash, PartialEq, Eq)]
187pub struct RockBinaries(Vec<PathBuf>);
188
189impl Deref for RockBinaries {
190    type Target = Vec<PathBuf>;
191
192    fn deref(&self) -> &Self::Target {
193        &self.0
194    }
195}
196
197impl DerefMut for RockBinaries {
198    fn deref_mut(&mut self) -> &mut Self::Target {
199        &mut self.0
200    }
201}