Skip to main content

manganis_core/
ffi.rs

1use crate::BundledAsset;
2use const_serialize::{ConstStr, SerializeConst};
3use const_serialize_08 as const_serialize;
4use serde::{Deserialize, Serialize};
5use std::hash::Hash;
6
7/// Unified symbol data that can represent both assets and permissions
8///
9/// This enum is used to serialize different types of metadata into the binary
10/// using the same `__ASSETS__` symbol prefix. The CBOR format allows for
11/// self-describing data, making it easy to add new variants in the future.
12///
13/// Variant order does NOT matter for CBOR enum serialization - variants are
14/// matched by name (string), not by position or tag value.
15#[derive(Debug, Clone, Copy, PartialEq, Eq, SerializeConst)]
16#[repr(C, u8)]
17#[allow(clippy::large_enum_variant)]
18#[non_exhaustive]
19pub enum SymbolData {
20    /// An asset that should be bundled with the application
21    Asset(BundledAsset),
22
23    /// Android plugin metadata (prebuilt artifacts + Gradle deps)
24    AndroidArtifact(AndroidArtifactMetadata),
25
26    /// Swift package metadata (SPM location + product)
27    SwiftPackage(SwiftPackageMetadata),
28}
29
30/// Platform categories for permission mapping
31#[repr(u8)]
32#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, SerializeConst)]
33pub enum Platform {
34    Android,
35    Ios,
36    Macos,
37}
38
39/// Bit flags for supported platforms
40#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, SerializeConst)]
41pub struct PlatformFlags(u8);
42
43impl PlatformFlags {
44    pub const fn new() -> Self {
45        Self(0)
46    }
47}
48
49impl Default for PlatformFlags {
50    fn default() -> Self {
51        Self::new()
52    }
53}
54
55impl PlatformFlags {
56    pub const fn with_platform(mut self, platform: Platform) -> Self {
57        self.0 |= 1 << platform as u8;
58        self
59    }
60
61    pub const fn supports(&self, platform: Platform) -> bool {
62        (self.0 & (1 << platform as u8)) != 0
63    }
64
65    pub const fn all() -> Self {
66        Self(0b000111) // Android + iOS + macOS
67    }
68
69    pub const fn mobile() -> Self {
70        Self(0b000011) // Android + iOS
71    }
72}
73
74/// Platform-specific permission identifiers
75#[derive(Debug, Clone, PartialEq, Eq, Hash)]
76pub struct PlatformIdentifiers {
77    pub android: Option<ConstStr>,
78    pub ios: Option<ConstStr>,
79    pub macos: Option<ConstStr>,
80}
81
82/// Metadata describing an Android plugin artifact (.aar) that must be copied into the host Gradle project.
83#[derive(Debug, Clone, Copy, PartialEq, Eq, SerializeConst, Serialize, Deserialize)]
84pub struct AndroidArtifactMetadata {
85    pub plugin_name: ConstStr,
86    pub artifact_path: ConstStr,
87    pub gradle_dependencies: ConstStr,
88}
89
90impl AndroidArtifactMetadata {
91    pub const fn new(
92        plugin_name: &'static str,
93        artifact_path: &'static str,
94        gradle_dependencies: &'static str,
95    ) -> Self {
96        Self {
97            plugin_name: ConstStr::new(plugin_name),
98            artifact_path: ConstStr::new(artifact_path),
99            gradle_dependencies: ConstStr::new(gradle_dependencies),
100        }
101    }
102}
103
104/// Metadata for a Swift package that needs to be linked into the app (iOS/macOS).
105#[derive(Debug, Clone, Copy, PartialEq, Eq, SerializeConst, Serialize, Deserialize)]
106pub struct SwiftPackageMetadata {
107    pub plugin_name: ConstStr,
108    pub package_path: ConstStr,
109    pub product: ConstStr,
110}
111
112impl SwiftPackageMetadata {
113    pub const fn new(
114        plugin_name: &'static str,
115        package_path: &'static str,
116        product: &'static str,
117    ) -> Self {
118        Self {
119            plugin_name: ConstStr::new(plugin_name),
120            package_path: ConstStr::new(package_path),
121            product: ConstStr::new(product),
122        }
123    }
124}