Skip to main content

santa_data/
models.rs

1// Core data models for Santa Package Manager
2
3use derive_more::{Display, From, Into};
4use serde::{Deserialize, Serialize};
5use serde_enum_str::{Deserialize_enum_str, Serialize_enum_str};
6use std::collections::HashMap;
7
8/// Strong type for package names to prevent mixing up with other strings
9#[derive(Debug, Clone, PartialEq, Eq, Hash, Display, From, Into, Serialize, Deserialize)]
10pub struct PackageName(pub String);
11
12/// Strong type for source names to prevent mixing up with other strings
13#[derive(Debug, Clone, PartialEq, Eq, Hash, Display, From, Into, Serialize, Deserialize)]
14pub struct SourceName(pub String);
15
16/// Strong type for command names to prevent command/package name confusion
17#[derive(Debug, Clone, PartialEq, Eq, Hash, Display, From, Into, Serialize, Deserialize)]
18pub struct CommandName(pub String);
19
20/// Well-known package manager sources that Santa can interact with.
21///
22/// Unknown sources are captured as strings to allow forward compatibility.
23#[non_exhaustive]
24#[derive(Serialize_enum_str, Deserialize_enum_str, Debug, Clone, Eq, PartialEq, Hash)]
25#[serde(rename_all = "camelCase")]
26pub enum KnownSources {
27    /// Debian/Ubuntu APT package manager.
28    Apt,
29    /// Arch User Repository.
30    Aur,
31    /// Homebrew (macOS and Linux).
32    Brew,
33    /// Rust Cargo package manager.
34    Cargo,
35    /// Flathub Flatpak repository.
36    Flathub,
37    /// Nix package manager.
38    Nix,
39    /// Node.js npm registry.
40    Npm,
41    /// Arch Linux pacman package manager.
42    Pacman,
43    /// Scoop package manager for Windows.
44    Scoop,
45    /// An unrecognized source, stored as a raw string.
46    #[serde(other)]
47    Unknown(String),
48}
49
50/// Supported operating systems for platform targeting.
51#[non_exhaustive]
52#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq, Hash)]
53#[serde(rename_all = "camelCase")]
54pub enum OS {
55    /// Apple macOS.
56    Macos,
57    /// Linux (any distribution).
58    Linux,
59    /// Microsoft Windows.
60    Windows,
61}
62
63/// Supported CPU architectures for platform targeting.
64#[non_exhaustive]
65#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq, Hash)]
66#[serde(rename_all = "camelCase")]
67pub enum Arch {
68    /// 64-bit x86 (AMD64/Intel 64).
69    X64,
70    /// 32-bit x86.
71    X86,
72    /// 64-bit ARM (Apple Silicon, AWS Graviton, etc.).
73    Aarch64,
74    /// 32-bit ARM.
75    Arm,
76}
77
78/// Linux distribution identifiers for distro-specific package source selection.
79#[non_exhaustive]
80#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq, Hash)]
81#[serde(rename_all = "camelCase")]
82pub enum Distro {
83    /// No specific distribution (generic Linux or non-Linux OS).
84    None,
85    /// Arch Linux and derivatives.
86    ArchLinux,
87    /// Ubuntu and derivatives.
88    Ubuntu,
89}
90
91/// A target platform defined by operating system, CPU architecture, and optional Linux distribution.
92///
93/// Used to determine which package sources and installation methods are applicable.
94#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq, Hash)]
95#[serde(rename_all = "camelCase")]
96#[non_exhaustive]
97pub struct Platform {
98    /// The operating system.
99    pub os: OS,
100    /// The CPU architecture.
101    pub arch: Arch,
102    /// The Linux distribution, if applicable.
103    pub distro: Option<Distro>,
104}
105
106impl Platform {
107    /// Create a new `Platform` for the given OS, architecture, and optional distro.
108    pub fn new(os: OS, arch: Arch, distro: Option<Distro>) -> Self {
109        Platform { os, arch, distro }
110    }
111}
112
113impl Default for Platform {
114    fn default() -> Self {
115        Platform {
116            os: OS::Linux,
117            arch: Arch::X64,
118            distro: None,
119        }
120    }
121}
122
123impl std::fmt::Display for Platform {
124    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
125        match &self.distro {
126            Some(distro) => {
127                write!(f, "{:?} {:?} ({:?})", self.os, self.arch, distro)
128            }
129            None => {
130                write!(f, "{:?} {:?}", self.os, self.arch)
131            }
132        }
133    }
134}
135
136/// Per-source configuration for a single package, including install hooks and name overrides.
137#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
138#[non_exhaustive]
139pub struct PackageData {
140    /// Source-specific package name override (e.g. a different crate name in Cargo vs Brew).
141    pub name: Option<String>,
142    /// Shell snippet to run before the package manager command.
143    pub before: Option<String>,
144    /// Shell snippet to run after the package manager command.
145    pub after: Option<String>,
146    /// Shell snippet to run as a pre-install step (inside the generated script).
147    pub pre: Option<String>,
148    /// Shell snippet to run as a post-install step (inside the generated script).
149    pub post: Option<String>,
150}
151
152impl PackageData {
153    pub fn new(name: &str) -> Self {
154        PackageData {
155            name: Some(name.to_string()),
156            before: None,
157            after: None,
158            pre: None,
159            post: None,
160        }
161    }
162
163    /// Create a `PackageData` with the given name override and lifecycle hooks.
164    pub fn with_hooks(
165        name: Option<String>,
166        before: Option<String>,
167        after: Option<String>,
168        pre: Option<String>,
169        post: Option<String>,
170    ) -> Self {
171        PackageData {
172            name,
173            before,
174            after,
175            pre,
176            post,
177        }
178    }
179}
180
181/// Map of package names to their per-source configurations.
182///
183/// The outer key is the user-facing package name. The inner map associates each
184/// [`KnownSources`] with an optional [`PackageData`] override for that source.
185pub type PackageDataList = HashMap<String, HashMap<KnownSources, Option<PackageData>>>;