gdext_gen/args/mod.rs
1//! Module with the structs and enums needed to call the main function of the library.
2
3#[cfg(feature = "icons")]
4pub mod icons;
5
6use std::env::var;
7
8#[allow(unused_imports)]
9use super::gdext::GDExtension;
10
11/// The representation of a path **relative** to the `Godot` project folder.
12const PROJECT_FOLDER: &str = "res://";
13
14/// The representation of a path **relative** to the folder where `.gdextension` lies.
15const GDEXTENSION_FOLDER: &str = "";
16
17/// The base directory to use for paths.
18#[derive(Default, Debug, Clone, Copy)]
19pub enum BaseDirectory {
20 /// Uses the folder where `project.godot` lies as the base for relative paths. Makes all paths start with `"res://"`.
21 #[default]
22 ProjectFolder,
23 /// Uses the folder where the `.gdextension` file lies as the base for relative paths. Makes all paths start with `""`.
24 GDExtensionFolder,
25}
26
27impl BaseDirectory {
28 /// Retrieves the base directory as the string to start the paths.
29 ///
30 /// # Returns
31 /// "res://" if it is ProjectFolder or "" if it is GDExtensionFolder.
32 pub fn as_str(&self) -> &'static str {
33 match self {
34 BaseDirectory::ProjectFolder => PROJECT_FOLDER,
35 BaseDirectory::GDExtensionFolder => GDEXTENSION_FOLDER,
36 }
37 }
38}
39
40/// Name of the default entry function `godot-rust` uses for initializing the [`GDExtension`].
41pub const DEFAULT_ENTRY_SYMBOL: &str = "gdext_rust_init";
42
43/// Entry symbol for the [`GDExtension`].
44#[derive(Default, Debug, Clone)]
45pub enum EntrySymbol {
46 /// The default entry symbol to the [`GDExtension`]: [`DEFAULT_ENTRY_SYMBOL`].
47 #[default]
48 GodotRustDefault,
49 /// A generic entry symbol to the [`GDExtension`] based on the crate name in `snake_case`: "lib{crate_name}_init".
50 CrateNameBased,
51 /// A custom entry symbol to the [`GDExtension`], specified through the String.
52 Custom(String),
53}
54
55impl ToString for EntrySymbol {
56 fn to_string(&self) -> String {
57 match self {
58 EntrySymbol::GodotRustDefault => DEFAULT_ENTRY_SYMBOL.into(),
59 EntrySymbol::CrateNameBased => format!(
60 "lib{}_init",
61 var("CARGO_PKG_NAME")
62 .map_or("rust".into(), |entry_symbol| entry_symbol.replace('-', "_"))
63 ),
64 EntrySymbol::Custom(entry_symbol) => entry_symbol.clone(),
65 }
66 }
67}