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#[non_exhaustive]
21#[derive(Serialize_enum_str, Deserialize_enum_str, Debug, Clone, Eq, PartialEq, Hash)]
22#[serde(rename_all = "camelCase")]
23pub enum KnownSources {
24    Apt,
25    Aur,
26    Brew,
27    Cargo,
28    Flathub,
29    Nix,
30    Npm,
31    Pacman,
32    Scoop,
33    #[serde(other)]
34    Unknown(String),
35}
36
37#[non_exhaustive]
38#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq, Hash)]
39#[serde(rename_all = "camelCase")]
40pub enum OS {
41    Macos,
42    Linux,
43    Windows,
44}
45
46#[non_exhaustive]
47#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq, Hash)]
48#[serde(rename_all = "camelCase")]
49pub enum Arch {
50    X64,
51    X86,
52    Aarch64,
53    Arm,
54}
55
56#[non_exhaustive]
57#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq, Hash)]
58#[serde(rename_all = "camelCase")]
59pub enum Distro {
60    None,
61    ArchLinux,
62    Ubuntu,
63}
64
65#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq, Hash)]
66#[serde(rename_all = "camelCase")]
67pub struct Platform {
68    pub os: OS,
69    pub arch: Arch,
70    pub distro: Option<Distro>,
71}
72
73impl Default for Platform {
74    fn default() -> Self {
75        Platform {
76            os: OS::Linux,
77            arch: Arch::X64,
78            distro: None,
79        }
80    }
81}
82
83impl std::fmt::Display for Platform {
84    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
85        match &self.distro {
86            Some(distro) => {
87                write!(f, "{:?} {:?} ({:?})", self.os, self.arch, distro)
88            }
89            None => {
90                write!(f, "{:?} {:?}", self.os, self.arch)
91            }
92        }
93    }
94}
95
96/// Package-specific configuration data
97#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
98pub struct PackageData {
99    pub name: Option<String>,
100    pub before: Option<String>,
101    pub after: Option<String>,
102    pub pre: Option<String>,
103    pub post: Option<String>,
104}
105
106impl PackageData {
107    pub fn new(name: &str) -> Self {
108        PackageData {
109            name: Some(name.to_string()),
110            before: None,
111            after: None,
112            pre: None,
113            post: None,
114        }
115    }
116}
117
118/// Map of package names to their source configurations
119pub type PackageDataList = HashMap<String, HashMap<KnownSources, Option<PackageData>>>;