santa-data 0.5.0

Data models and CCL parser for Santa package manager
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
// Schema-based data structures for Santa Package Manager
// These structs match the YAML schemas defined in /data/*.yaml files

use crate::models::{Platform, OS};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Package definition matching package_schema.yaml
/// Supports both simple array format and complex object format
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum PackageDefinition {
    /// Simple array format: just a list of source names
    Simple(Vec<String>),
    /// Complex object format: with metadata and source-specific configs
    Complex(ComplexPackageDefinition),
}

impl PackageDefinition {
    /// Get all sources where this package is available
    pub fn get_sources(&self) -> Vec<&str> {
        match self {
            PackageDefinition::Simple(sources) => sources.iter().map(|s| s.as_str()).collect(),
            PackageDefinition::Complex(complex) => complex.get_sources(),
        }
    }

    /// Get source-specific configuration for a source
    pub fn get_source_config(&self, source: &str) -> Option<&SourceSpecificConfig> {
        match self {
            PackageDefinition::Simple(_) => None,
            PackageDefinition::Complex(complex) => complex.get_source_config(source),
        }
    }

    /// Check if package is available in a specific source
    pub fn is_available_in(&self, source: &str) -> bool {
        self.get_sources().contains(&source)
    }

    /// Get the package description if available
    pub fn get_description(&self) -> Option<&str> {
        match self {
            PackageDefinition::Simple(_) => None,
            PackageDefinition::Complex(complex) => complex.description.as_deref(),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(default)]
#[non_exhaustive]
pub struct ComplexPackageDefinition {
    /// Short description of the package
    #[serde(rename = "_description", skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// List of sources where package is available with same name as key
    #[serde(rename = "_sources", skip_serializing_if = "Option::is_none")]
    pub sources: Option<Vec<String>>,

    /// Platforms where this package is available
    #[serde(rename = "_platforms", skip_serializing_if = "Option::is_none")]
    pub platforms: Option<Vec<String>>,

    /// Alternative names for search and discovery
    #[serde(rename = "_aliases", skip_serializing_if = "Option::is_none")]
    pub aliases: Option<Vec<String>>,

    /// Source-specific configurations (flatten other fields)
    #[serde(flatten)]
    pub source_configs: HashMap<String, SourceSpecificConfig>,
}

/// Source-specific configuration for a package
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum SourceSpecificConfig {
    /// Simple name override
    Name(String),
    /// Complex configuration with hooks and modifications
    Complex(SourceConfig),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct SourceConfig {
    /// Override package name for this source
    pub name: Option<String>,
    /// Command to run before installation
    pub pre: Option<String>,
    /// Command to run after successful installation  
    pub post: Option<String>,
    /// String to prepend to package name during installation
    pub prefix: Option<String>,
    /// String to append to the install command
    pub install_suffix: Option<String>,
}

/// Sources configuration matching sources_schema.yaml
pub type SourcesDefinition = HashMap<String, SourceDefinition>;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct SourceDefinition {
    /// Emoji icon to represent this source
    pub emoji: String,
    /// Command template to install packages
    pub install: String,
    /// Command to list installed packages from this source
    pub check: String,
    /// String to prepend to package names (optional)
    pub prefix: Option<String>,
    /// Platform-specific command overrides
    #[serde(rename = "_overrides", skip_serializing_if = "Option::is_none")]
    pub overrides: Option<HashMap<String, PlatformOverride>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct PlatformOverride {
    pub install: Option<String>,
    pub check: Option<String>,
}

/// Configuration matching config_schema.yaml
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ConfigDefinition {
    /// List of package sources to use (in priority order)
    pub sources: Vec<String>,
    /// List of packages to install/manage
    pub packages: Vec<String>,
    /// Advanced configuration options
    #[serde(rename = "_settings", skip_serializing_if = "Option::is_none")]
    pub settings: Option<ConfigSettings>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ConfigSettings {
    /// Automatically update packages
    #[serde(default)]
    pub auto_update: bool,
    /// Maximum parallel package installations
    #[serde(default = "default_parallel_installs")]
    pub parallel_installs: u8,
    /// Ask for confirmation before installing packages
    #[serde(default = "default_true")]
    pub confirm_before_install: bool,
}

fn default_parallel_installs() -> u8 {
    3
}
fn default_true() -> bool {
    true
}

impl ComplexPackageDefinition {
    /// Create a new ComplexPackageDefinition with the given sources
    pub fn with_sources(sources: Vec<String>) -> Self {
        Self {
            sources: Some(sources),
            ..Default::default()
        }
    }

    /// Set the platforms for this package definition
    pub fn set_platforms(&mut self, platforms: Vec<String>) {
        self.platforms = Some(platforms);
    }

    /// Set the aliases for this package definition
    pub fn set_aliases(&mut self, aliases: Vec<String>) {
        self.aliases = Some(aliases);
    }

    /// Set the description for this package definition
    pub fn set_description(&mut self, description: String) {
        self.description = Some(description);
    }

    /// Get all sources where this package is available
    pub fn get_sources(&self) -> Vec<&str> {
        let mut all_sources = Vec::new();

        // Add sources from _sources array
        if let Some(sources) = &self.sources {
            all_sources.extend(sources.iter().map(|s| s.as_str()));
        }

        // Add sources from explicit configurations
        all_sources.extend(self.source_configs.keys().map(|s| s.as_str()));

        all_sources
    }

    /// Get source-specific configuration for a source
    pub fn get_source_config(&self, source: &str) -> Option<&SourceSpecificConfig> {
        self.source_configs.get(source)
    }

    /// Check if package is available in a specific source
    pub fn is_available_in(&self, source: &str) -> bool {
        self.get_sources().contains(&source)
    }
}

impl SourceConfig {
    /// Create a new `SourceConfig` with the given source-specific overrides.
    pub fn new(
        name: Option<String>,
        pre: Option<String>,
        post: Option<String>,
        prefix: Option<String>,
        install_suffix: Option<String>,
    ) -> Self {
        Self {
            name,
            pre,
            post,
            prefix,
            install_suffix,
        }
    }
}

impl PlatformOverride {
    /// Create a new `PlatformOverride` with the given install and check commands.
    pub fn new(install: Option<String>, check: Option<String>) -> Self {
        Self { install, check }
    }
}

impl ConfigDefinition {
    /// Create a new `ConfigDefinition` with the given sources, packages, and settings.
    pub fn new(
        sources: Vec<String>,
        packages: Vec<String>,
        settings: Option<ConfigSettings>,
    ) -> Self {
        Self {
            sources,
            packages,
            settings,
        }
    }
}

impl ConfigSettings {
    /// Create a new `ConfigSettings` with the given values.
    pub fn new(auto_update: bool, parallel_installs: u8, confirm_before_install: bool) -> Self {
        Self {
            auto_update,
            parallel_installs,
            confirm_before_install,
        }
    }
}

impl SourceDefinition {
    /// Create a new `SourceDefinition` with the given fields.
    pub fn new(
        emoji: String,
        install: String,
        check: String,
        prefix: Option<String>,
        overrides: Option<HashMap<String, PlatformOverride>>,
    ) -> Self {
        Self {
            emoji,
            install,
            check,
            prefix,
            overrides,
        }
    }

    /// Get the appropriate command for the current platform
    pub fn get_install_command(&self, platform: &Platform) -> &str {
        if let Some(overrides) = &self.overrides {
            let platform_key = match platform.os {
                OS::Windows => "windows",
                OS::Linux => "linux",
                OS::Macos => "macos",
            };

            if let Some(platform_override) = overrides.get(platform_key) {
                if let Some(install) = &platform_override.install {
                    return install;
                }
            }
        }
        &self.install
    }

    /// Get the appropriate check command for the current platform
    pub fn get_check_command(&self, platform: &Platform) -> &str {
        if let Some(overrides) = &self.overrides {
            let platform_key = match platform.os {
                OS::Windows => "windows",
                OS::Linux => "linux",
                OS::Macos => "macos",
            };

            if let Some(platform_override) = overrides.get(platform_key) {
                if let Some(check) = &platform_override.check {
                    return check;
                }
            }
        }
        &self.check
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_package_definition_simple_format() {
        // Test simple array format using our custom ccl-parser
        let ccl = r#"
bat =
  = brew
  = scoop
  = pacman
  = nix
"#;
        let packages: HashMap<String, PackageDefinition> = crate::parse_ccl_to(ccl).unwrap();
        let def = packages.get("bat").unwrap();

        assert!(def.is_available_in("brew"));
        assert!(def.is_available_in("scoop"));
        assert!(def.is_available_in("pacman"));
        assert!(def.is_available_in("nix"));

        // Simple format should not have source configs
        assert!(def.get_source_config("brew").is_none());

        // Check that all sources are present
        let sources = def.get_sources();
        assert_eq!(sources.len(), 4);
        assert!(sources.contains(&"brew"));
        assert!(sources.contains(&"scoop"));
        assert!(sources.contains(&"pacman"));
        assert!(sources.contains(&"nix"));
    }

    #[test]
    fn test_package_definition_complex_format() {
        let ccl = r#"
ripgrep =
  brew = gh
  _sources =
    = scoop
    = apt
    = pacman
    = nix
"#;
        let packages: HashMap<String, PackageDefinition> = crate::parse_ccl_to(ccl).unwrap();
        let def = packages.get("ripgrep").unwrap();

        assert!(def.is_available_in("brew"));
        assert!(def.is_available_in("scoop"));
        assert!(def.get_source_config("brew").is_some());

        // Check that sources list includes all sources
        let sources = def.get_sources();
        assert!(sources.contains(&"scoop"));
        assert!(sources.contains(&"apt"));
        assert!(sources.contains(&"pacman"));
        assert!(sources.contains(&"nix"));
        assert!(sources.contains(&"brew"));
    }

    #[test]
    fn test_source_definition() {
        let ccl = r#"
emoji = 🍺
install = brew install {package}
check = brew leaves --installed-on-request
"#;
        let def: SourceDefinition = sickle::from_str(ccl).unwrap();

        assert_eq!(def.emoji, "🍺");
        assert!(def.install.contains("{package}"));
    }

    #[test]
    fn test_package_with_description() {
        let ccl = r#"
bat =
  _description = A cat clone with syntax highlighting.
  _sources =
    = brew
    = scoop
"#;
        let packages: HashMap<String, PackageDefinition> = crate::parse_ccl_to(ccl).unwrap();
        let def = packages.get("bat").unwrap();

        // Should have description
        assert_eq!(
            def.get_description(),
            Some("A cat clone with syntax highlighting.")
        );

        // Should still have sources
        assert!(def.is_available_in("brew"));
        assert!(def.is_available_in("scoop"));
    }

    #[test]
    fn test_simple_package_no_description() {
        let ccl = r#"
jq =
  = brew
  = apt
"#;
        let packages: HashMap<String, PackageDefinition> = crate::parse_ccl_to(ccl).unwrap();
        let def = packages.get("jq").unwrap();

        // Simple format has no description
        assert_eq!(def.get_description(), None);
    }
}