artichoke_backend/release_metadata.rs
1//! Information about an Artichoke build.
2//!
3//! Release metadata allows populating Ruby constants that describe the build,
4//! like `RUBY_COPYRIGHT` for copyright information or `RUBY_PLATFORM` for
5//! target architecture.
6
7use artichoke_core::release_metadata;
8
9/// Information about an Artichoke build.
10///
11/// This build information is injected into `artichoke-backend` by the
12/// `artichoke` crate at interpreter initialization time.
13#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
14pub struct ReleaseMetadata<'a> {
15 /// Copyright information.
16 ///
17 /// This value will populate the `RUBY_COPYRIGHT` constant.
18 ///
19 /// # Examples
20 ///
21 /// ```text
22 /// artichoke - Copyright (c) 2019-2020 Ryan Lopopolo \<rjl@hyperbo.la\>
23 /// ```
24 pub copyright: &'a str,
25 /// A description of the current build.
26 ///
27 /// This value will populate the `RUBY_DESCRIPTION` constant.
28 ///
29 /// # Examples
30 ///
31 /// ```text
32 /// artichoke 0.1.0-pre.0 (2021-01-12 revision 4009) [x86_64-apple-darwin]
33 /// ```
34 pub description: &'a str,
35 /// The engine, or VM, used in the current build.
36 ///
37 /// This value will populate the `RUBY_ENGINE` constant.
38 ///
39 /// # Examples
40 ///
41 /// ```text
42 /// artichoke-mruby
43 /// ```
44 pub engine: &'a str,
45 /// The version of the engine, or VM, used in the current build.
46 ///
47 /// This value will populate the `RUBY_ENGINE_VERSION` constant.
48 ///
49 /// # Examples
50 ///
51 /// ```text
52 /// 0.1.0-pre.0
53 /// ```
54 pub engine_version: &'a str,
55 /// The patch level the current build.
56 ///
57 /// This value will populate the `RUBY_PATCHLEVEL` constant.
58 ///
59 /// # Examples
60 ///
61 /// ```text
62 /// 0
63 /// ```
64 pub patchlevel: &'a str,
65 /// The target triple of the platform this build targets.
66 ///
67 /// The platform will be a [Rust or LLVM target triple][triple].
68 ///
69 /// This value will populate the `RUBY_PLATFORM` constant.
70 ///
71 /// # Examples
72 ///
73 /// ```text
74 /// x86_64-apple-darwin
75 /// ```
76 ///
77 /// [triple]: https://forge.rust-lang.org/release/platform-support.html
78 pub platform: &'a str,
79 /// The build date of this release.
80 ///
81 /// This value will populate the `RUBY_RELEASE_DATE` constant.
82 ///
83 /// # Examples
84 ///
85 /// ```text
86 /// 2021-01-12
87 /// ```
88 pub release_date: &'a str,
89 /// The target MRI Ruby version for this build.
90 ///
91 /// This value will populate the `RUBY_VERSION` constant.
92 ///
93 /// # Examples
94 ///
95 /// ```text
96 /// 3.1.2
97 /// ```
98 pub revision: &'a str,
99 /// The target MRI Ruby version for this build.
100 ///
101 /// This value will populate the `RUBY_VERSION` constant.
102 ///
103 /// # Examples
104 ///
105 /// ```text
106 /// 3.1.2
107 /// ```
108 pub ruby_version: &'a str,
109 /// A description of the compiler used to build Artichoke.
110 ///
111 /// This value will populate the `ARTICHOKE_COMPILER_VERSION` constant.
112 ///
113 /// # Examples
114 ///
115 /// ```text
116 /// rustc 1.49.0 (e1884a8e3 2020-12-29) on x86_64-apple-darwin
117 /// ```
118 pub compiler_version: Option<&'a str>,
119}
120
121impl Default for ReleaseMetadata<'_> {
122 fn default() -> Self {
123 Self::new()
124 }
125}
126
127impl release_metadata::ReleaseMetadata for ReleaseMetadata<'_> {
128 fn ruby_copyright(&self) -> &str {
129 self.copyright
130 }
131
132 fn ruby_description(&self) -> &str {
133 self.description
134 }
135
136 fn ruby_engine(&self) -> &str {
137 self.engine
138 }
139
140 fn ruby_engine_version(&self) -> &str {
141 self.engine_version
142 }
143
144 fn ruby_patchlevel(&self) -> &str {
145 self.patchlevel
146 }
147
148 fn ruby_platform(&self) -> &str {
149 self.platform
150 }
151
152 fn ruby_release_date(&self) -> &str {
153 self.release_date
154 }
155
156 fn ruby_revision(&self) -> &str {
157 self.revision
158 }
159
160 fn ruby_version(&self) -> &str {
161 self.ruby_version
162 }
163
164 fn artichoke_compiler_version(&self) -> Option<&str> {
165 self.compiler_version
166 }
167}
168
169impl<'a> ReleaseMetadata<'a> {
170 #[must_use]
171 pub const fn new() -> Self {
172 Self {
173 copyright: "Copyright (c) 2019 Ryan Lopopolo <rjl@hyperbo.la>",
174 description: "Artichoke Ruby",
175 engine: "artichoke-mruby",
176 engine_version: env!("CARGO_PKG_VERSION"),
177 patchlevel: "0",
178 platform: "host",
179 release_date: "",
180 revision: "1",
181 ruby_version: "3.1.2",
182 compiler_version: Some("rustc"),
183 }
184 }
185
186 /// Set copyright information.
187 ///
188 /// This value will populate the `RUBY_COPYRIGHT` constant.
189 ///
190 /// # Examples
191 ///
192 /// ```text
193 /// artichoke - Copyright (c) 2019-2020 Ryan Lopopolo \<rjl@hyperbo.la\>
194 /// ```
195 #[must_use]
196 pub fn with_ruby_copyright(mut self, copyright: &'a str) -> Self {
197 self.copyright = copyright;
198 self
199 }
200
201 /// Set a description of the current build.
202 ///
203 /// This value will populate the `RUBY_DESCRIPTION` constant.
204 ///
205 /// # Examples
206 ///
207 /// ```text
208 /// artichoke 0.1.0-pre.0 (2021-01-12 revision 4009) [x86_64-apple-darwin]
209 /// ```
210 #[must_use]
211 pub fn with_ruby_description(mut self, description: &'a str) -> Self {
212 self.description = description;
213 self
214 }
215
216 /// Set the engine, or VM, used in the current build.
217 ///
218 /// This value will populate the `RUBY_ENGINE` constant.
219 ///
220 /// # Examples
221 ///
222 /// ```text
223 /// artichoke-mruby
224 /// ```
225 #[must_use]
226 pub fn with_ruby_engine(mut self, engine: &'a str) -> Self {
227 self.engine = engine;
228 self
229 }
230
231 /// Set the version of the engine, or VM, used in the current build.
232 ///
233 /// This value will populate the `RUBY_ENGINE_VERSION` constant.
234 ///
235 /// # Examples
236 ///
237 /// ```text
238 /// 0.1.0-pre.0
239 /// ```
240 #[must_use]
241 pub fn with_ruby_engine_version(mut self, engine_version: &'a str) -> Self {
242 self.engine_version = engine_version;
243 self
244 }
245
246 /// Set the patch level the current build.
247 ///
248 /// This value will populate the `RUBY_PATCHLEVEL` constant.
249 ///
250 /// # Examples
251 ///
252 /// ```text
253 /// 0
254 /// ```
255 #[must_use]
256 pub fn with_ruby_patchlevel(mut self, patchlevel: &'a str) -> Self {
257 self.patchlevel = patchlevel;
258 self
259 }
260
261 /// Set the target triple of the platform this build targets.
262 ///
263 /// The platform will be a [Rust or LLVM target triple][triple].
264 ///
265 /// This value will populate the `RUBY_PLATFORM` constant.
266 ///
267 /// # Examples
268 ///
269 /// ```text
270 /// x86_64-apple-darwin
271 /// ```
272 ///
273 /// [triple]: https://forge.rust-lang.org/release/platform-support.html
274 #[must_use]
275 pub fn with_ruby_platform(mut self, platform: &'a str) -> Self {
276 self.platform = platform;
277 self
278 }
279
280 /// Set the build date of this release.
281 ///
282 /// This value will populate the `RUBY_RELEASE_DATE` constant.
283 ///
284 /// # Examples
285 ///
286 /// ```text
287 /// 2021-01-12
288 /// ```
289 #[must_use]
290 pub fn with_ruby_release_date(mut self, release_date: &'a str) -> Self {
291 self.release_date = release_date;
292 self
293 }
294
295 /// Set the target MRI Ruby version for this build.
296 ///
297 /// This value will populate the `RUBY_VERSION` constant.
298 ///
299 /// # Examples
300 ///
301 /// ```text
302 /// 3.1.2
303 /// ```
304 #[must_use]
305 pub fn with_ruby_revision(mut self, revision: &'a str) -> Self {
306 self.revision = revision;
307 self
308 }
309
310 /// Set the target MRI Ruby version for this build.
311 ///
312 /// This value will populate the `RUBY_VERSION` constant.
313 ///
314 /// # Examples
315 ///
316 /// ```text
317 /// 3.1.2
318 /// ```
319 #[must_use]
320 pub fn with_ruby_version(mut self, ruby_version: &'a str) -> Self {
321 self.ruby_version = ruby_version;
322 self
323 }
324
325 /// Set a description of the compiler used to build Artichoke.
326 ///
327 /// This value will populate the `ARTICHOKE_COMPILER_VERSION` constant.
328 ///
329 /// # Examples
330 ///
331 /// ```text
332 /// rustc 1.49.0 (e1884a8e3 2020-12-29) on x86_64-apple-darwin
333 /// ```
334 #[must_use]
335 pub fn with_artichoke_compiler_version(mut self, compiler_version: Option<&'a str>) -> Self {
336 self.compiler_version = compiler_version;
337 self
338 }
339
340 /// Copyright information.
341 ///
342 /// This value will populate the `RUBY_COPYRIGHT` constant.
343 ///
344 /// # Examples
345 ///
346 /// ```text
347 /// artichoke - Copyright (c) 2019-2020 Ryan Lopopolo \<rjl@hyperbo.la\>
348 /// ```
349 #[must_use]
350 pub const fn ruby_copyright(&self) -> &str {
351 self.copyright
352 }
353
354 /// A description of the current build.
355 ///
356 /// This value will populate the `RUBY_DESCRIPTION` constant.
357 ///
358 /// # Examples
359 ///
360 /// ```text
361 /// artichoke 0.1.0-pre.0 (2021-01-12 revision 4009) [x86_64-apple-darwin]
362 /// ```
363 #[must_use]
364 pub const fn ruby_description(&self) -> &str {
365 self.description
366 }
367
368 /// The engine, or VM, used in the current build.
369 ///
370 /// This value will populate the `RUBY_ENGINE` constant.
371 ///
372 /// # Examples
373 ///
374 /// ```text
375 /// artichoke-mruby
376 /// ```
377 #[must_use]
378 pub const fn ruby_engine(&self) -> &str {
379 self.engine
380 }
381
382 /// The version of the engine, or VM, used in the current build.
383 ///
384 /// This value will populate the `RUBY_ENGINE_VERSION` constant.
385 ///
386 /// # Examples
387 ///
388 /// ```text
389 /// 0.1.0-pre.0
390 /// ```
391 #[must_use]
392 pub const fn ruby_engine_version(&self) -> &str {
393 self.engine_version
394 }
395
396 /// The patch level the current build.
397 ///
398 /// This value will populate the `RUBY_PATCHLEVEL` constant.
399 ///
400 /// # Examples
401 ///
402 /// ```text
403 /// 0
404 /// ```
405 #[must_use]
406 pub const fn ruby_patchlevel(&self) -> &str {
407 self.patchlevel
408 }
409
410 /// The target triple of the platform this build targets.
411 ///
412 /// The platform will be a [Rust or LLVM target triple][triple].
413 ///
414 /// This value will populate the `RUBY_PLATFORM` constant.
415 ///
416 /// # Examples
417 ///
418 /// ```text
419 /// x86_64-apple-darwin
420 /// ```
421 ///
422 /// [triple]: https://forge.rust-lang.org/release/platform-support.html
423 #[must_use]
424 pub const fn ruby_platform(&self) -> &str {
425 self.platform
426 }
427
428 /// The build date of this release.
429 ///
430 /// This value will populate the `RUBY_RELEASE_DATE` constant.
431 ///
432 /// # Examples
433 ///
434 /// ```text
435 /// 2021-01-12
436 /// ```
437 #[must_use]
438 pub const fn ruby_release_date(&self) -> &str {
439 self.release_date
440 }
441
442 /// The target MRI Ruby version for this build.
443 ///
444 /// This value will populate the `RUBY_VERSION` constant.
445 ///
446 /// # Examples
447 ///
448 /// ```text
449 /// 3.1.2
450 /// ```
451 #[must_use]
452 pub const fn ruby_revision(&self) -> &str {
453 self.revision
454 }
455
456 /// The target MRI Ruby version for this build.
457 ///
458 /// This value will populate the `RUBY_VERSION` constant.
459 ///
460 /// # Examples
461 ///
462 /// ```text
463 /// 3.1.2
464 /// ```
465 #[must_use]
466 pub const fn ruby_version(&self) -> &str {
467 self.ruby_version
468 }
469
470 /// A description of the compiler used to build Artichoke.
471 ///
472 /// This value will populate the `ARTICHOKE_COMPILER_VERSION` constant.
473 ///
474 /// # Examples
475 ///
476 /// ```text
477 /// rustc 1.49.0 (e1884a8e3 2020-12-29) on x86_64-apple-darwin
478 /// ```
479 #[must_use]
480 pub const fn artichoke_compiler_version(&self) -> Option<&str> {
481 self.compiler_version
482 }
483}