diff --git a/prebuilt-template/src/lib.rs b/prebuilt-template/src/lib.rs index 74b9728..6de13c8 100644 --- a/prebuilt-template/src/lib.rs +++ b/prebuilt-template/src/lib.rs @@ -1,47 +1,58 @@ /* + * Copyright (c) godot-rust; Bromeon and contributors. * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -// Note: this API is not necessarily forward-compatible. It can be updated on demand (for all published Godot versions). -// Important is that the latest version of gdext can be used together with all latest published artifacts. -// For long-term stability, it may be helpful to have a dynamic query mechanism, such as: -// fn get_property(key: &str) -> Option +// Since this lib.rs is repurposed as a module file, avoid crate-level attributes such as +// #![doc(html_logo_url)] here. -/// Version of the Godot engine that the API JSON and C header mirror. -/// Note that this currently only contains the `major.minor[.patch]` part, so even `4.2-rc1` will be `4.2`. -pub const GODOT_VERSION: &str = "%GODOT4_VER%"; +//! # GDExtension API for Godot %GODOT4_VER% + +use std::borrow::Cow; -/// Version of the Rust compiler used to build this artifact. -/// Is still a number even for nightly versions. -pub const RUST_VERSION: &str = "%RUST_VER%"; +/// Abstracts from borrow/owned and allows to change implementation without affecting API. +pub type CowStr = Cow<'static, str>; -/// Version of the `bindgen` crate used to generate the `gdextension_interface.h` Rust binding. -pub const BINDGEN_VERSION: &str = "%BINDGEN_VER%"; +/// Version of the Godot engine that the API JSON and C header mirror. +/// +/// 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 +/// not published). +pub const GODOT_VERSION_STRING: &str = "%GODOT4_VER%"; /// Returns the contents of the header file `gdextension_interface.h`. -pub const fn load_gdextension_header_h() -> &'static str { - include_str!("../res/gdextension_interface.h") +pub const fn load_gdextension_header_h() -> CowStr { + CowStr::Borrowed(include_str!("../res/gdextension_interface.h")) } /// Returns the contents of the header file `gdextension_interface.rs`, generated for the corresponding platform. -pub const fn load_gdextension_header_rs() -> &'static str { +pub const fn load_gdextension_header_rs() -> CowStr { #[cfg(windows)] - { - include_str!("../res/gdextension_interface_windows.rs") - } + let s = include_str!("../res/gdextension_interface_windows.rs"); + #[cfg(target_os = "macos")] - { - include_str!("../res/gdextension_interface_macos.rs") - } + let s = include_str!("../res/gdextension_interface_macos.rs"); + #[cfg(all(unix, not(target_os = "macos")))] - { - include_str!("../res/gdextension_interface_linux.rs") - } + let s = include_str!("../res/gdextension_interface_linux.rs"); + + CowStr::Borrowed(s) } /// Returns the contents of the JSON API file `extension_api.json`. -pub const fn load_gdextension_json() -> &'static str { - include_str!("../res/extension_api.json") +pub const fn load_gdextension_json() -> CowStr { + Cow::Borrowed(include_str!("../res/extension_api.json")) +} + +/// Dynamically fetch a property of this crate. +pub fn get_package_property(key: &str) -> Option { + let value = match key { + "godot_version_string" => Cow::Borrowed(GODOT_VERSION_STRING), + "rust_version_string" => Cow::Borrowed("%RUST_VER%"), + "bindgen_version_string" => Cow::Borrowed("%BINDGEN_VER%"), + _ => return None, + }; + + Some(value) }