lux_lib/rockspec/lua/
partial.rs1use super::super::platform;
2use crate::package;
3use std::collections::HashMap;
4
5use crate::{rockspec::lua_dependency, rockspec::Format, ROCKSPEC_FUEL_LIMIT};
6use miette::Diagnostic;
7use ottavino::{Closure, Executor, Fuel};
8use ottavino_util::serde::from_value;
9use thiserror::Error;
10
11use super::super::{build, deploy, description, external_dependency, test};
12use super::parse_lua_tbl_or_default;
13
14#[derive(Debug)]
16pub struct Partial {
17 pub(crate) format: Option<Format>,
18 pub(crate) package: Option<package::Name>,
19 pub(crate) build: Option<build::internal::Spec>,
20 pub(crate) deploy: Option<deploy::Spec>,
21 pub(crate) description: Option<description::Spec>,
22 pub(crate) supported_platforms: Option<platform::Support>,
23 pub(crate) dependencies: Option<Vec<lua_dependency::Spec>>,
24 pub(crate) build_dependencies: Option<Vec<lua_dependency::Spec>>,
25 pub(crate) external_dependencies: Option<HashMap<String, external_dependency::Spec>>,
26 pub(crate) test_dependencies: Option<Vec<lua_dependency::Spec>>,
27 pub(crate) test: Option<test::internal::Spec>,
28}
29
30#[derive(Debug, Error, Diagnostic)]
31pub enum Error {
32 #[error("rockspec execution exceeded fuel limit of {ROCKSPEC_FUEL_LIMIT} steps")]
33 #[diagnostic(help(
34 r#"the rockspec may be too complex or contain too many dependencies.
35 Try reducing the number of dependencies or simplifying build instructions."#
36 ))]
37 FuelLimitExceeded,
38 #[error("field '{0}' should not be declared in extra.rockspec")]
39 ExtraneousField(String),
40 #[error("error while parsing rockspec")]
41 Lua(#[from] ottavino::ExternError),
42 #[error(transparent)]
43 Io(#[from] std::io::Error),
44}
45
46impl Partial {
47 pub fn new(rockspec_content: &str) -> Result<Self, Error> {
48 let mut lua = ottavino::Lua::core();
49
50 let rockspec = lua.try_enter(|ctx| {
51 let closure = Closure::load(ctx, None, rockspec_content.as_bytes())?;
52
53 let executor = Executor::start(ctx, closure.into(), ());
54
55 let output = executor.step(ctx, &mut Fuel::with(ROCKSPEC_FUEL_LIMIT))?;
56
57 if !output {
58 return Ok(Err(Error::FuelLimitExceeded));
59 }
60
61 let globals = ctx.globals();
62
63 if !matches!(globals.get_value(ctx, "version"), ottavino::Value::Nil) {
64 return Ok(Err(Error::ExtraneousField("version".to_string())));
65 }
66 if !matches!(globals.get_value(ctx, "source"), ottavino::Value::Nil) {
67 return Ok(Err(Error::ExtraneousField("source".to_string())));
68 }
69
70 let rockspec = Partial {
71 format: from_value(globals.get_value(ctx, "rockspec_format")).unwrap_or_default(),
72 package: from_value(globals.get_value(ctx, "package")).unwrap_or_default(),
73 description: parse_lua_tbl_or_default(ctx, "description").unwrap_or_default(),
74 supported_platforms: parse_lua_tbl_or_default(ctx, "supported_platforms")
75 .unwrap_or_default(),
76 dependencies: from_value(globals.get_value(ctx, "dependencies"))
77 .unwrap_or_default(),
78 build_dependencies: from_value(globals.get_value(ctx, "build_dependencies"))
79 .unwrap_or_default(),
80 test_dependencies: from_value(globals.get_value(ctx, "test_dependencies"))
81 .unwrap_or_default(),
82 external_dependencies: from_value(globals.get_value(ctx, "external_dependencies"))
83 .unwrap_or_default(),
84 build: from_value(globals.get_value(ctx, "build")).unwrap_or_default(),
85 test: from_value(globals.get_value(ctx, "test")).unwrap_or_default(),
86 deploy: from_value(globals.get_value(ctx, "deploy")).unwrap_or_default(),
87 };
88
89 Ok(Ok(rockspec))
90 })??;
91
92 Ok(rockspec)
93 }
94}
95
96#[cfg(test)]
97mod tests {
98 use super::*;
99
100 #[test]
101 fn parse_partial_rockspec() {
102 let partial_rockspec = r#"
103 package = "my-package"
104 "#;
105
106 Partial::new(partial_rockspec).unwrap();
107
108 let full_rockspec = r#"
110 rockspec_format = "3.0"
111 package = "my-package"
112
113 description = {
114 summary = "A summary",
115 detailed = "A detailed description",
116 license = "MIT",
117 homepage = "https://example.com",
118 issues_url = "https://example.com/issues",
119 maintainer = "John Doe",
120 labels = {"label1", "label2"},
121 }
122
123 supported_platforms = {"linux", "!windows"}
124
125 dependencies = {
126 "lua 5.1",
127 "foo 1.0",
128 "bar >=2.0",
129 }
130
131 build_dependencies = {
132 "baz 1.0",
133 }
134
135 external_dependencies = {
136 foo = { header = "foo.h" },
137 bar = { library = "libbar.so" },
138 }
139
140 test_dependencies = {
141 "busted 1.0",
142 }
143
144 test = {
145 type = "command",
146 script = "test.lua",
147 flags = {"foo", "bar"},
148 }
149
150 build = {
151 type = "builtin",
152 }
153 "#;
154
155 let rockspec = Partial::new(full_rockspec).unwrap();
156
157 assert!(rockspec.format.is_some());
161 assert!(rockspec.package.is_some());
162 assert!(rockspec.description.is_some());
163 assert!(rockspec.supported_platforms.is_some());
164 assert!(rockspec.dependencies.is_some());
165 assert!(rockspec.build_dependencies.is_some());
166 assert!(rockspec.external_dependencies.is_some());
167 assert!(rockspec.test_dependencies.is_some());
168 assert!(rockspec.build.is_some());
169 assert!(rockspec.test.is_some());
170
171 let partial_rockspec = r#"
173 version = "2.0.0"
174 "#;
175
176 Partial::new(partial_rockspec).unwrap_err();
177
178 let partial_rockspec = r#"
179 source = {
180 url = "https://example.com",
181 }
182 "#;
183
184 Partial::new(partial_rockspec).unwrap_err();
185 }
186}