dart_bindgen/
config.rs

1use fmt::Display;
2use std::fmt;
3
4/// Represents the different methods to create a `DynamicLibrary`.
5#[derive(Debug, Clone)]
6pub enum DynamicLibraryCreationMode {
7    /// Creates a dynamic library representing the running executable.
8    Executable,
9    /// Loads a dynamic library file with local visibility.
10    Open(String),
11    /// Creates a dynamic library holding all global symbols.
12    Process,
13}
14
15impl DynamicLibraryCreationMode {
16    /// a suger method for [`DynamicLibraryCreationMode::Open`]
17    pub fn open(path: impl Into<String>) -> Self { Self::Open(path.into()) }
18}
19
20/// Configures how a `DynamicLibrary` should be created on a platform.
21///
22/// The different constructors represents the different constructors modes of
23/// `DynamicLibrary`. See [`DynamicLibraryCreationMode`] for more information.
24#[derive(Debug, Clone)]
25pub struct DynamicLibraryPlatformConfig {
26    creation_mode: DynamicLibraryCreationMode,
27}
28
29impl DynamicLibraryPlatformConfig {
30    /// Create [`DynamicLibraryPlatformConfig`] using the provided
31    /// [`DynamicLibraryCreationMode`]
32    pub const fn new(mode: DynamicLibraryCreationMode) -> Self {
33        Self {
34            creation_mode: mode,
35        }
36    }
37}
38
39impl From<DynamicLibraryCreationMode> for Option<DynamicLibraryPlatformConfig> {
40    fn from(mode: DynamicLibraryCreationMode) -> Self {
41        Some(DynamicLibraryPlatformConfig::new(mode))
42    }
43}
44
45impl Display for DynamicLibraryPlatformConfig {
46    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47        match self.creation_mode {
48            DynamicLibraryCreationMode::Executable => {
49                write!(f, "DynamicLibrary.executable()")
50            },
51            DynamicLibraryCreationMode::Open(ref path) => {
52                write!(f, "DynamicLibrary.open('{}')", path)
53            },
54            DynamicLibraryCreationMode::Process => {
55                write!(f, "DynamicLibrary.process()")
56            },
57        }
58    }
59}
60
61/// Defines, how the dynamic library should be loaded on each of dart's known
62/// platforms.
63#[derive(Debug, Clone, Default)]
64pub struct DynamicLibraryConfig {
65    /// how to load the dynamic library when the operating system is a version
66    /// of `Android`.
67    pub android: Option<DynamicLibraryPlatformConfig>,
68    /// how to load the dynamic library when the operating system is a version
69    /// `Fuchsia`.
70    pub fuchsia: Option<DynamicLibraryPlatformConfig>,
71    /// how to load the dynamic library when the operating system is a version
72    /// `iOS`.
73    pub ios: Option<DynamicLibraryPlatformConfig>,
74    /// how to load the dynamic library when the operating system is a version
75    /// `Linux`.
76    pub linux: Option<DynamicLibraryPlatformConfig>,
77    /// how to load the dynamic library when the operating system is a version
78    /// `macOS`.
79    pub macos: Option<DynamicLibraryPlatformConfig>,
80    /// how to load the dynamic library when the operating system is a version
81    /// `Windows`.
82    pub windows: Option<DynamicLibraryPlatformConfig>,
83}