gdext_gen/gdext/
config.rs

1//! Module for the definition of the [`Configuration`] struct for the configuration section of the `.gdextension` file.
2
3use std::default::Default;
4
5use crate::args::EntrySymbol;
6
7#[allow(unused_imports)]
8use super::GDExtension;
9use serde::{Deserialize, Serialize};
10
11/// Configuration section of the `.gdextension` file.
12#[derive(Deserialize, Serialize, Debug)]
13pub struct Configuration {
14    /// Name of the entry function for initializing the [`GDExtension`]. By default, its name is `"gdext_rust_init"`, but it can be changed by using the attribute `entry_point` (`godot-rust <= 0.2.0`) or `entry_symbol` (`>= 0.2.1`).
15    /// # Examples
16    /// In lib.rs:
17    /// ```
18    /// #[gdextension(entry_symbol = libmy_rust_init)]
19    /// unsafe impl ExtensionLibrary for MyExtension {}
20    /// ```
21    entry_symbol: String,
22    /// Minimum compatible version of `Godot`. This prevents older versions of `Godot` from loading [`GDExtension`]s that depend on features from newer versions of `Godot`. It's formatted as follows: `<major>.<minor>`.
23    compatibility_minimum: Option<f64>,
24    /// Maximum compatible version of `Godot`. This prevents newer versions of `Godot` from loading the [`GDExtension`]. It's formatted as follows: `<major>.<minor>`.
25    compatibility_maximum: Option<f64>,
26    /// Whether or not to allow the reloading of the [`GDExtension`] upon recompilation. Supported only for `Godot 4.2` and later. Meant generally for development and debug purposes, and it can fail, it always is safer to close and reopen the engine, but it's a good quality of life feature in general.
27    reloadable: Option<bool>,
28    /// The [`GDExtension`] is part of a `v2 Android` plugin. During export this flag will indicate to the editor that the [`GDExtension`] native shared libraries are exported by the `Android` plugin `AAR` binaries.
29    android_aar_plugin: Option<bool>,
30}
31
32impl Configuration {
33    /// Creates a new instance of [`Configuration`], by using parameters with sensible types instead of the types [`Configuration`] will store.
34    ///
35    /// # Parameters
36    ///
37    /// * `entry_symbol` - [`EntrySymbol`] for initializing the [`GDExtension`]. It uses its `to_string` method to provide its representation.
38    /// * `compatibility_minimum` - Minimum compatible version of `Godot`, with format `(major, minor)`, in case [`Some`] is provided.
39    /// * `compatibility_maximum` - Maximum compatible version of `Godot`, with format `(major, minor)`, in case [`Some`] is provided.
40    /// * `is_reloadable` - Whether or not to allow the reloading of the [`GDExtension`] upon recompilation.
41    /// * `are_exported_by_android_aar_plugin` - Whether or not the [`GDExtension`] native shared libraries are exported by the `Android` plugin `AAR` binaries.
42    ///
43    /// # Returns
44    ///
45    /// The [`Configuration`] with the necessary fields properly parsed.
46    pub fn new(
47        entry_symbol: EntrySymbol,
48        compatibility_minimum: Option<(u8, u8)>,
49        compatibility_maximum: Option<(u8, u8)>,
50        is_reloadable: bool,
51        are_exported_by_android_aar_plugin: bool,
52    ) -> Self {
53        Self {
54            entry_symbol: entry_symbol.to_string(),
55            compatibility_minimum: compatibility_minimum
56                .map(|(major, minor)| format!("{}.{}", major, minor).parse().unwrap_or(4.1)),
57            compatibility_maximum: compatibility_maximum.and_then(|(major, minor)| match format!(
58                "{}.{}",
59                major, minor
60            )
61            .parse()
62            {
63                Ok(com_min) => Some(com_min),
64                _ => None,
65            }),
66            reloadable: is_reloadable.then_some(true),
67            android_aar_plugin: are_exported_by_android_aar_plugin.then_some(true),
68        }
69    }
70
71    /// Creates a new instance of [`Configuration`], by using the parameters as are.
72    ///
73    /// # Parameters
74    ///
75    /// * `entry_symbol` - Name of the entry function for initializing the [`GDExtension`].
76    /// * `compatibility_minimum` - Minimum compatible version of `Godot`, with format `major.minor`, in case [`Some`] is provided.
77    /// * `compatibility_maximum` - Maximum compatible version of `Godot`, with format `major.minor`, in case [`Some`] is provided.
78    /// * `reloadable` - Whether or not to allow the reloading of the [`GDExtension`] upon recompilation, in case [`Some`] is provided.
79    /// * `android_aar_plugin` - Whether or not the [`GDExtension`] native shared libraries are exported by the `Android` plugin `AAR` binaries in case [`Some`] is provided.
80    ///
81    /// # Returns
82    ///
83    /// The [`Configuration`] with the necessary fields properly parsed.
84    pub fn raw_new(
85        entry_symbol: String,
86        compatibility_minimum: Option<f64>,
87        compatibility_maximum: Option<f64>,
88        reloadable: Option<bool>,
89        android_aar_plugin: Option<bool>,
90    ) -> Self {
91        Self {
92            entry_symbol,
93            compatibility_minimum,
94            compatibility_maximum,
95            reloadable,
96            android_aar_plugin,
97        }
98    }
99
100    /// Creates a new instance of [`Configuration`], by using a specified [`EntrySymbol`].
101    ///
102    /// # Parameters
103    ///
104    /// * `entry_symbol` - [`EntrySymbol`] for initializing the [`GDExtension`]. It uses its `to_string` method to provide its representation.
105    ///
106    /// # Returns
107    ///
108    /// The [`Configuration`] with the `entry_symbol` field properly parsed.
109    pub fn from_entry_symbol(entry_symbol: EntrySymbol) -> Self {
110        return Configuration {
111            entry_symbol: entry_symbol.to_string(),
112            ..Default::default()
113        };
114    }
115
116    /// Creates a new instance of [`Configuration`], by using a specified [`String`] as the empty symbol as is.
117    ///
118    /// # Parameters
119    ///
120    /// * `entry_symbol` - Name of the entry function for initializing the [`GDExtension`].
121    ///
122    /// # Returns
123    ///
124    /// The [`Configuration`] with the `entry_symbol` field properly parsed.
125    pub fn from_raw_entry_symbol(entry_symbol: String) -> Self {
126        return Configuration {
127            entry_symbol,
128            ..Default::default()
129        };
130    }
131
132    /// Sets the `compatibility_minimum` of the [`Configuration`] to the one passed as parameter properly parsed and returns it.
133    ///
134    /// # Parameters
135    ///
136    /// * `compatibility_minimum` - Minimum compatible version of `Godot`, with format `(major, minor)`.
137    pub fn with_compatibility_minimum(mut self, compatibility_minimum: (u8, u8)) -> Self {
138        let (major, minor) = compatibility_minimum;
139        self.compatibility_minimum = Some(major as f64 + (minor as f64 / 10.0));
140        return self;
141    }
142
143    /// Sets the `compatibility_minimum` of the [`Configuration`] to the one passed as parameter and returns it.
144    ///
145    /// # Parameters
146    ///
147    /// * `compatibility_minimum` - Minimum compatible version of `Godot`, with format `major.minor`.
148    pub fn with_raw_compatibility_minimum(mut self, compatibility_minimum: f64) -> Self {
149        self.compatibility_minimum = Some(compatibility_minimum);
150        return self;
151    }
152
153    /// Sets the `compatibility_maximum` of the [`Configuration`] to the one passed as parameter properly parsed and returns it.
154    ///
155    /// # Parameters
156    ///
157    /// * `compatibility_maximum` - Maximum compatible version of `Godot`, with format `(major, minor)`.
158    pub fn with_compatibility_maximum(mut self, compatibility_maximum: (u8, u8)) -> Self {
159        let (major, minor) = compatibility_maximum;
160        self.compatibility_maximum = Some(major as f64 + (minor as f64 / 10.0));
161        return self;
162    }
163
164    /// Sets the `compatibility_maximum` of the [`Configuration`] to the one passed as parameter and returns it.
165    ///
166    /// # Parameters
167    ///
168    /// * `compatibility_maximum` - Maximum compatible version of `Godot`, with format `major.minor`.
169    pub fn with_raw_compatibility_maximum(mut self, compatibility_maximum: f64) -> Self {
170        self.compatibility_maximum = Some(compatibility_maximum);
171        return self;
172    }
173
174    /// Changes the [`Configuration`] to allow the reloading of the [`GDExtension`] upon recompilation.
175    pub fn with_reloadability(mut self) -> Self {
176        self.reloadable = Some(true);
177        self
178    }
179
180    /// Changes the [`Configuration`] so the [`GDExtension`] native shared libraries are exported by the `Android` plugin `AAR` binaries and returns it.
181    pub fn with_android_aar_plugin(mut self) -> Self {
182        self.android_aar_plugin = Some(true);
183        self
184    }
185}
186
187impl Default for Configuration {
188    /// The [`Configuration`] with the entry symbol found in the `godot-rust` book.
189    fn default() -> Self {
190        Configuration {
191            entry_symbol: EntrySymbol::GodotRustDefault.to_string(),
192            compatibility_minimum: None,
193            compatibility_maximum: None,
194            reloadable: None,
195            android_aar_plugin: None,
196        }
197    }
198}