gdextension_api/versions/4.5.1/src/lib.rs
1/*
2 * Copyright (c) godot-rust; Bromeon and contributors.
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
6 */
7
8// Since this lib.rs is repurposed as a module file, avoid crate-level attributes such as
9// #![doc(html_logo_url)] here.
10
11//! # GDExtension API for Godot 4.5.1
12
13use std::borrow::Cow;
14
15/// Abstracts from borrow/owned and allows to change implementation without affecting API.
16pub type CowStr = Cow<'static, str>;
17
18/// Version of the Godot engine that the API JSON and C header mirror.
19///
20/// Note that this currently only contains the `major.minor[.patch]` part, so even `4.2-rc1` would be `4.2` (although pre-releases are currently
21/// not published).
22pub const GODOT_VERSION_STRING: &str = "4.5.1";
23
24/// Target platform for which the GDExtension interface is generated.
25///
26/// Since the `gdextension-api` crate is typically used as a build dependency, it won't have compile-time access to the build target
27/// (only runtime via `CARGO_CFG_TARGET_*` env vars). For more flexibility and testing, this choice is delegated to the caller.
28///
29/// See [`load_gdextension_header_rs_for_platform()`] for usage.
30#[non_exhaustive]
31pub enum TargetPlatform {
32 Windows,
33 /// Includes iOS.
34 MacOS,
35 /// Includes other Unix-like systems such as BSD and Android.
36 Linux,
37 Wasm,
38}
39
40/// Returns the contents of the header file `gdextension_interface.h`.
41pub const fn load_gdextension_header_h() -> CowStr {
42 CowStr::Borrowed(include_str!("../../4.5/res/gdextension_interface.h"))
43}
44
45/// Returns the contents of the header file `gdextension_interface.rs`, generated for the corresponding platform.
46#[deprecated = "Wrongly dispatches based on host and doesn't support Wasm. Use load_gdextension_header_rs_for_platform() instead."]
47pub const fn load_gdextension_header_rs() -> CowStr {
48 #[cfg(windows)]
49 let s = include_str!("../../4.5/res/gdextension_interface_windows.rs");
50
51 #[cfg(target_os = "macos")]
52 let s = include_str!("../../4.5/res/gdextension_interface_macos.rs");
53
54 #[cfg(all(unix, not(target_os = "macos")))]
55 let s = include_str!("../../4.5/res/gdextension_interface_linux.rs");
56
57 CowStr::Borrowed(s)
58}
59
60/// Returns the contents of the header file `gdextension_interface.rs`, generated for the corresponding platform.
61pub fn load_gdextension_header_rs_for_platform(platform: TargetPlatform) -> CowStr {
62 let s = match platform {
63 TargetPlatform::Windows => include_str!("../../4.5/res/gdextension_interface_windows.rs"),
64 TargetPlatform::MacOS => include_str!("../../4.5/res/gdextension_interface_macos.rs"),
65 TargetPlatform::Linux => include_str!("../../4.5/res/gdextension_interface_linux.rs"),
66 TargetPlatform::Wasm => include_str!("../../4.5/res/gdextension_interface_wasm.rs"),
67 };
68
69 CowStr::Borrowed(s)
70}
71
72/// Returns the contents of the JSON API file `extension_api.json`.
73pub const fn load_gdextension_json() -> CowStr {
74 Cow::Borrowed(include_str!("../res/extension_api.json"))
75}
76
77/// Dynamically fetch a property of this crate.
78pub fn get_package_property(key: &str) -> Option<CowStr> {
79 let value = match key {
80 "godot_version_string" => Cow::Borrowed(GODOT_VERSION_STRING),
81 "rust_version_string" => Cow::Borrowed(""),
82 "bindgen_version_string" => Cow::Borrowed(""),
83 _ => return None,
84 };
85
86 Some(value)
87}