Skip to main content

mecha10_deploy/services/package/
types.rs

1//! Package service types
2//!
3//! Data types for package creation and manifest generation.
4
5use chrono::{DateTime, Utc};
6use mecha10_core::enum_str_method;
7use serde::{Deserialize, Serialize};
8use std::collections::HashMap;
9use std::str::FromStr;
10
11/// Supported target architectures
12#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
13#[serde(rename_all = "kebab-case")]
14pub enum TargetArch {
15    X8664UnknownLinuxGnu,
16    Aarch64UnknownLinuxGnu,
17    ArmV7UnknownLinuxGnueabihf,
18    X8664AppleDarwin,
19    Aarch64AppleDarwin,
20}
21
22impl FromStr for TargetArch {
23    type Err = String;
24
25    fn from_str(s: &str) -> Result<Self, Self::Err> {
26        match s {
27            "x86_64-unknown-linux-gnu" | "x86_64" => Ok(TargetArch::X8664UnknownLinuxGnu),
28            "aarch64-unknown-linux-gnu" | "aarch64" | "arm64" => Ok(TargetArch::Aarch64UnknownLinuxGnu),
29            "armv7-unknown-linux-gnueabihf" | "armv7" | "armhf" => Ok(TargetArch::ArmV7UnknownLinuxGnueabihf),
30            "x86_64-apple-darwin" => Ok(TargetArch::X8664AppleDarwin),
31            "aarch64-apple-darwin" => Ok(TargetArch::Aarch64AppleDarwin),
32            _ => Err(format!("Invalid target architecture: {}", s)),
33        }
34    }
35}
36
37enum_str_method!(
38    pub fn as_str(TargetArch) -> &str {
39        X8664UnknownLinuxGnu => "x86_64-unknown-linux-gnu",
40        Aarch64UnknownLinuxGnu => "aarch64-unknown-linux-gnu",
41        ArmV7UnknownLinuxGnueabihf => "armv7-unknown-linux-gnueabihf",
42        X8664AppleDarwin => "x86_64-apple-darwin",
43        Aarch64AppleDarwin => "aarch64-apple-darwin",
44    }
45);
46
47enum_str_method!(
48    pub fn display_name(TargetArch) -> &str {
49        X8664UnknownLinuxGnu => "x86_64 (Linux)",
50        Aarch64UnknownLinuxGnu => "ARM64 (Linux)",
51        ArmV7UnknownLinuxGnueabihf => "ARMv7 (Linux)",
52        X8664AppleDarwin => "x86_64 (macOS)",
53        Aarch64AppleDarwin => "ARM64 (macOS)",
54    }
55);
56
57impl TargetArch {
58    /// Get default target for current platform
59    pub fn default_for_platform() -> Self {
60        #[cfg(all(target_arch = "x86_64", target_os = "linux"))]
61        return TargetArch::X8664UnknownLinuxGnu;
62
63        #[cfg(all(target_arch = "aarch64", target_os = "linux"))]
64        return TargetArch::Aarch64UnknownLinuxGnu;
65
66        #[cfg(all(target_arch = "x86_64", target_os = "macos"))]
67        return TargetArch::X8664AppleDarwin;
68
69        #[cfg(all(target_arch = "aarch64", target_os = "macos"))]
70        return TargetArch::Aarch64AppleDarwin;
71
72        #[cfg(not(any(
73            all(target_arch = "x86_64", target_os = "linux"),
74            all(target_arch = "aarch64", target_os = "linux"),
75            all(target_arch = "x86_64", target_os = "macos"),
76            all(target_arch = "aarch64", target_os = "macos")
77        )))]
78        return TargetArch::X8664UnknownLinuxGnu;
79    }
80}
81
82/// Package manifest containing metadata about the package
83#[derive(Debug, Clone, Serialize, Deserialize)]
84pub struct PackageManifest {
85    pub format_version: String,
86    pub name: String,
87    pub version: String,
88    pub build_timestamp: DateTime<Utc>,
89    pub target_arch: TargetArch,
90    pub binaries: Vec<BinaryInfo>,
91    pub configs: Vec<ConfigInfo>,
92    pub assets: Vec<AssetInfo>,
93    pub dependencies: HashMap<String, String>,
94    pub metadata: HashMap<String, serde_json::Value>,
95    pub build_profile: String,
96    pub git_commit: Option<String>,
97    pub environment: Option<String>,
98}
99
100impl PackageManifest {
101    pub const FORMAT_VERSION: &'static str = "1.0.0";
102}
103
104#[derive(Debug, Clone, Serialize, Deserialize)]
105pub struct BinaryInfo {
106    pub name: String,
107    pub path: String,
108    pub size_bytes: u64,
109    pub checksum: String,
110}
111
112#[derive(Debug, Clone, Serialize, Deserialize)]
113pub struct ConfigInfo {
114    pub name: String,
115    pub path: String,
116    pub config_type: String,
117    pub checksum: String,
118}
119
120#[derive(Debug, Clone, Serialize, Deserialize)]
121pub struct AssetInfo {
122    pub name: String,
123    pub path: String,
124    pub asset_type: String,
125    pub size_bytes: u64,
126    pub checksum: String,
127}
128
129/// Package configuration
130#[derive(Debug, Clone)]
131pub struct PackageConfig {
132    pub output_dir: std::path::PathBuf,
133    pub target_arch: TargetArch,
134    pub build_profile: String,
135    pub include_assets: bool,
136    pub environment: Option<String>,
137    pub custom_metadata: HashMap<String, serde_json::Value>,
138}
139
140impl Default for PackageConfig {
141    fn default() -> Self {
142        Self {
143            output_dir: std::path::PathBuf::from("target/packages"),
144            target_arch: TargetArch::default_for_platform(),
145            build_profile: "release".to_string(),
146            include_assets: true,
147            environment: None,
148            custom_metadata: HashMap::new(),
149        }
150    }
151}