Skip to main content

alien_bindings/
lib.rs

1use alien_error::AlienError;
2
3// Re-export core traits and types
4pub use alien_context::AlienContext;
5pub use alien_core::{
6    BindingsMode, Platform, ENV_ALIEN_BASE_PLATFORM, ENV_ALIEN_BINDINGS_MODE,
7    ENV_ALIEN_DEPLOYMENT_TYPE,
8};
9pub use error::{ErrorData, Result};
10pub use provider::BindingsProvider;
11pub use traits::{
12    ArtifactRegistry, ArtifactRegistryCredentials, ArtifactRegistryPermissions,
13    AwsServiceAccountInfo, AzureServiceAccountInfo, Binding, BindingsProviderApi, Build, Container,
14    GcpServiceAccountInfo, ImpersonationRequest, Kv, Queue, RegistryAuthMethod, RepositoryResponse,
15    ServiceAccount, ServiceAccountInfo, Storage, Vault, Worker,
16};
17pub use wait_until::{DrainConfig, DrainResponse, WaitUntil, WaitUntilContext};
18
19pub mod error;
20#[cfg(feature = "grpc")]
21pub mod grpc;
22pub mod providers;
23pub mod traits;
24// Re-export presigned types from alien-core
25pub mod presigned {
26    pub use alien_core::presigned::*;
27}
28pub mod alien_context;
29pub mod http_client;
30pub mod provider;
31
32mod wait_until;
33
34#[cfg(feature = "grpc")]
35pub use grpc::control;
36#[cfg(feature = "grpc")]
37pub use grpc::control_service::ControlGrpcServer;
38#[cfg(feature = "grpc")]
39pub use grpc::GrpcServerHandles;
40
41/// Gets the current platform from the ALIEN_DEPLOYMENT_TYPE environment variable.
42/// This is used by the runtime to determine which platform-specific implementations to use.
43pub fn get_current_platform() -> Result<Platform> {
44    let env_vars: std::collections::HashMap<String, String> = std::env::vars().collect();
45    get_platform_from_env(&env_vars)
46}
47
48/// Gets the platform from a HashMap of environment variables.
49pub fn get_platform_from_env(env: &std::collections::HashMap<String, String>) -> Result<Platform> {
50    let deployment_type = env.get(ENV_ALIEN_DEPLOYMENT_TYPE).ok_or_else(|| {
51        AlienError::new(ErrorData::EnvironmentVariableMissing {
52            variable_name: ENV_ALIEN_DEPLOYMENT_TYPE.to_string(),
53        })
54    })?;
55
56    deployment_type.parse().map_err(|_| {
57        AlienError::new(ErrorData::InvalidEnvironmentVariable {
58            variable_name: ENV_ALIEN_DEPLOYMENT_TYPE.to_string(),
59            value: deployment_type.clone(),
60            reason: "Cannot parse the ALIEN_DEPLOYMENT_TYPE environment variable".to_string(),
61        })
62    })
63}
64
65/// Parse ALIEN_BINDINGS_MODE from environment variables.
66/// Defaults to Direct if not specified.
67pub fn get_bindings_mode_from_env(
68    env: &std::collections::HashMap<String, String>,
69) -> Result<BindingsMode> {
70    let mode_str = env
71        .get(ENV_ALIEN_BINDINGS_MODE)
72        .map(|s| s.as_str())
73        .unwrap_or("direct");
74
75    mode_str.parse().map_err(|reason: String| {
76        AlienError::new(ErrorData::InvalidEnvironmentVariable {
77            variable_name: ENV_ALIEN_BINDINGS_MODE.to_string(),
78            value: mode_str.to_string(),
79            reason,
80        })
81    })
82}