jarvy 0.0.5

Jarvy is a fast, cross-platform CLI that installs and manages developer tools across macOS and Linux.
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
//! Role Definition Types
//!
//! Defines the structure of roles in jarvy.toml:
//! ```toml
//! [roles.frontend]
//! description = "Frontend development stack"
//! tools = ["node", "bun", "pnpm"]
//!
//! [roles.frontend.tools]
//! node = "20"

#![allow(dead_code)] // Public API for role definitions
//! bun = "latest"
//! ```

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Role assignment - can be single or multiple roles
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(untagged)]
pub enum RoleAssignment {
    /// Single role assignment: `role = "frontend"`
    Single(String),
    /// Multiple role assignment: `roles = ["frontend", "devops"]`
    Multiple(Vec<String>),
}

impl RoleAssignment {
    /// Get all assigned roles as a vector
    pub fn as_vec(&self) -> Vec<&str> {
        match self {
            RoleAssignment::Single(s) => vec![s.as_str()],
            RoleAssignment::Multiple(v) => v.iter().map(String::as_str).collect(),
        }
    }

    /// Check if any roles are assigned
    pub fn is_empty(&self) -> bool {
        match self {
            RoleAssignment::Single(s) => s.is_empty(),
            RoleAssignment::Multiple(v) => v.is_empty(),
        }
    }

    /// Get the number of assigned roles
    pub fn len(&self) -> usize {
        match self {
            RoleAssignment::Single(s) => {
                if s.is_empty() {
                    0
                } else {
                    1
                }
            }
            RoleAssignment::Multiple(v) => v.len(),
        }
    }
}

impl Default for RoleAssignment {
    fn default() -> Self {
        RoleAssignment::Multiple(Vec::new())
    }
}

/// Role inheritance - can extend single or multiple parent roles
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(untagged)]
pub enum RoleExtends {
    /// Single parent: `extends = "base-role"`
    Single(String),
    /// Multiple parents: `extends = ["role-a", "role-b"]`
    Multiple(Vec<String>),
}

impl RoleExtends {
    /// Get all parent role names as a vector
    pub fn as_vec(&self) -> Vec<&str> {
        match self {
            RoleExtends::Single(s) => vec![s.as_str()],
            RoleExtends::Multiple(v) => v.iter().map(String::as_str).collect(),
        }
    }

    /// Check if extends is empty
    pub fn is_empty(&self) -> bool {
        match self {
            RoleExtends::Single(s) => s.is_empty(),
            RoleExtends::Multiple(v) => v.is_empty(),
        }
    }
}

impl Default for RoleExtends {
    fn default() -> Self {
        RoleExtends::Multiple(Vec::new())
    }
}

/// Tool specification in a role - can be simple name or detailed config
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(untagged)]
pub enum RoleToolSpec {
    /// Detailed tool config with version and options
    Detailed {
        version: String,
        #[serde(default)]
        version_manager: Option<bool>,
        #[serde(default)]
        use_sudo: Option<bool>,
    },
    /// Simple version string: `"latest"` or `"20"`
    Simple(String),
}

impl RoleToolSpec {
    /// Get the version string
    pub fn version(&self) -> &str {
        match self {
            RoleToolSpec::Detailed { version, .. } => version,
            RoleToolSpec::Simple(v) => v,
        }
    }

    /// Check if version_manager is enabled (defaults to true)
    pub fn version_manager(&self) -> bool {
        match self {
            RoleToolSpec::Detailed {
                version_manager, ..
            } => version_manager.unwrap_or(true),
            RoleToolSpec::Simple(_) => true,
        }
    }

    /// Get sudo override if specified
    pub fn use_sudo(&self) -> Option<bool> {
        match self {
            RoleToolSpec::Detailed { use_sudo, .. } => *use_sudo,
            RoleToolSpec::Simple(_) => None,
        }
    }
}

/// A role definition within the config
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Default)]
pub struct RoleDefinition {
    /// Human-readable description of the role
    #[serde(default)]
    pub description: Option<String>,

    /// Parent role(s) to inherit from
    #[serde(default)]
    pub extends: Option<RoleExtends>,

    /// Simple tool list (tool names with default "latest" version)
    #[serde(default)]
    pub tools: Vec<String>,

    /// Detailed tool specifications with versions
    /// This is populated from [roles.name.tools] section
    #[serde(flatten)]
    pub tool_versions: HashMap<String, RoleToolSpec>,
}

impl RoleDefinition {
    /// Check if this role extends other roles
    pub fn has_extends(&self) -> bool {
        self.extends
            .as_ref()
            .map(|e| !e.is_empty())
            .unwrap_or(false)
    }

    /// Get the list of parent role names
    pub fn get_extends(&self) -> Vec<&str> {
        self.extends
            .as_ref()
            .map(|e| e.as_vec())
            .unwrap_or_default()
    }

    /// Get all tools defined in this role (both simple and detailed)
    /// Returns a map of tool name -> version
    pub fn get_tools(&self) -> HashMap<String, String> {
        let mut result = HashMap::new();

        // Add simple tool list with "latest" version
        for tool in &self.tools {
            result.insert(tool.clone(), "latest".to_string());
        }

        // Add/override with detailed tool specs
        for (name, spec) in &self.tool_versions {
            // Skip the "tools" key which contains the nested table
            if name != "tools" {
                result.insert(name.clone(), spec.version().to_string());
            }
        }

        result
    }

    /// Get the number of tools in this role (not counting inheritance)
    pub fn tool_count(&self) -> usize {
        let mut count = self.tools.len();
        for name in self.tool_versions.keys() {
            if name != "tools" && !self.tools.contains(name) {
                count += 1;
            }
        }
        count
    }
}

/// Roles section in jarvy.toml config
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
pub struct RolesConfig {
    /// Map of role name -> role definition
    #[serde(flatten)]
    pub roles: HashMap<String, RoleDefinitionWrapper>,
}

/// Wrapper to handle the [roles.name] and [roles.name.tools] TOML structure
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(untagged)]
pub enum RoleDefinitionWrapper {
    /// Role with tools section: [roles.name] + [roles.name.tools]
    WithTools {
        #[serde(default)]
        description: Option<String>,
        #[serde(default)]
        extends: Option<RoleExtends>,
        #[serde(default)]
        tools: RoleToolsSection,
    },
    /// Simple role with just tool names array
    Simple(RoleDefinition),
}

/// Tools section can be array of names or table with versions
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(untagged)]
pub enum RoleToolsSection {
    /// Array of tool names: tools = ["node", "bun"]
    Names(Vec<String>),
    /// Table with versions: [roles.name.tools] node = "20"
    Versions(HashMap<String, RoleToolSpec>),
}

impl Default for RoleToolsSection {
    fn default() -> Self {
        RoleToolsSection::Names(Vec::new())
    }
}

impl RoleDefinitionWrapper {
    /// Convert to RoleDefinition
    pub fn into_definition(self) -> RoleDefinition {
        match self {
            RoleDefinitionWrapper::WithTools {
                description,
                extends,
                tools,
            } => {
                let (tool_list, tool_versions) = match tools {
                    RoleToolsSection::Names(names) => (names, HashMap::new()),
                    RoleToolsSection::Versions(versions) => (Vec::new(), versions),
                };
                RoleDefinition {
                    description,
                    extends,
                    tools: tool_list,
                    tool_versions,
                }
            }
            RoleDefinitionWrapper::Simple(def) => def,
        }
    }

    /// Get reference to RoleDefinition-like data
    pub fn description(&self) -> Option<&str> {
        match self {
            RoleDefinitionWrapper::WithTools { description, .. } => description.as_deref(),
            RoleDefinitionWrapper::Simple(def) => def.description.as_deref(),
        }
    }

    /// Check if extends is specified
    pub fn has_extends(&self) -> bool {
        match self {
            RoleDefinitionWrapper::WithTools { extends, .. } => {
                extends.as_ref().map(|e| !e.is_empty()).unwrap_or(false)
            }
            RoleDefinitionWrapper::Simple(def) => def.has_extends(),
        }
    }

    /// Get extends references
    pub fn get_extends(&self) -> Vec<&str> {
        match self {
            RoleDefinitionWrapper::WithTools { extends, .. } => {
                extends.as_ref().map(|e| e.as_vec()).unwrap_or_default()
            }
            RoleDefinitionWrapper::Simple(def) => def.get_extends(),
        }
    }
}

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

    #[test]
    fn test_role_assignment_single() {
        let assignment = RoleAssignment::Single("frontend".to_string());
        assert_eq!(assignment.as_vec(), vec!["frontend"]);
        assert_eq!(assignment.len(), 1);
        assert!(!assignment.is_empty());
    }

    #[test]
    fn test_role_assignment_multiple() {
        let assignment =
            RoleAssignment::Multiple(vec!["frontend".to_string(), "devops".to_string()]);
        assert_eq!(assignment.as_vec(), vec!["frontend", "devops"]);
        assert_eq!(assignment.len(), 2);
        assert!(!assignment.is_empty());
    }

    #[test]
    fn test_role_assignment_empty() {
        let assignment = RoleAssignment::Multiple(Vec::new());
        assert!(assignment.is_empty());
        assert_eq!(assignment.len(), 0);

        let single_empty = RoleAssignment::Single(String::new());
        assert!(single_empty.is_empty());
        assert_eq!(single_empty.len(), 0);
    }

    #[test]
    fn test_role_extends_single() {
        let extends = RoleExtends::Single("base".to_string());
        assert_eq!(extends.as_vec(), vec!["base"]);
        assert!(!extends.is_empty());
    }

    #[test]
    fn test_role_extends_multiple() {
        let extends = RoleExtends::Multiple(vec!["base".to_string(), "common".to_string()]);
        assert_eq!(extends.as_vec(), vec!["base", "common"]);
        assert!(!extends.is_empty());
    }

    #[test]
    fn test_role_tool_spec_simple() {
        let spec = RoleToolSpec::Simple("20".to_string());
        assert_eq!(spec.version(), "20");
        assert!(spec.version_manager());
        assert!(spec.use_sudo().is_none());
    }

    #[test]
    fn test_role_tool_spec_detailed() {
        let spec = RoleToolSpec::Detailed {
            version: "18".to_string(),
            version_manager: Some(false),
            use_sudo: Some(true),
        };
        assert_eq!(spec.version(), "18");
        assert!(!spec.version_manager());
        assert_eq!(spec.use_sudo(), Some(true));
    }

    #[test]
    fn test_role_definition_get_tools() {
        let mut def = RoleDefinition {
            tools: vec!["node".to_string(), "bun".to_string()],
            ..Default::default()
        };
        def.tool_versions
            .insert("node".to_string(), RoleToolSpec::Simple("20".to_string()));
        def.tool_versions
            .insert("pnpm".to_string(), RoleToolSpec::Simple("8".to_string()));

        let tools = def.get_tools();
        assert_eq!(tools.get("node"), Some(&"20".to_string())); // Version overrides "latest"
        assert_eq!(tools.get("bun"), Some(&"latest".to_string()));
        assert_eq!(tools.get("pnpm"), Some(&"8".to_string()));
    }

    #[test]
    fn test_role_definition_has_extends() {
        let mut def = RoleDefinition::default();
        assert!(!def.has_extends());

        def.extends = Some(RoleExtends::Single("base".to_string()));
        assert!(def.has_extends());
    }

    #[test]
    fn test_role_assignment_deserialize_single() {
        let toml_str = r#"role = "frontend""#;
        #[derive(Deserialize)]
        struct Test {
            role: RoleAssignment,
        }
        let test: Test = toml::from_str(toml_str).unwrap();
        assert!(matches!(test.role, RoleAssignment::Single(_)));
    }

    #[test]
    fn test_role_assignment_deserialize_multiple() {
        let toml_str = r#"role = ["frontend", "devops"]"#;
        #[derive(Deserialize)]
        struct Test {
            role: RoleAssignment,
        }
        let test: Test = toml::from_str(toml_str).unwrap();
        assert!(matches!(test.role, RoleAssignment::Multiple(_)));
    }
}