cuenv_core/manifest/
mod.rs

1//! Root Cuenv configuration type
2//!
3//! Based on schema/cuenv.cue
4
5use schemars::JsonSchema;
6use serde::{Deserialize, Serialize};
7use std::collections::HashMap;
8
9use crate::config::Config;
10use crate::environment::Env;
11use crate::hooks::Hook;
12use crate::tasks::TaskDefinition;
13
14/// Collection of hooks that can be executed
15#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Default)]
16pub struct Hooks {
17    /// Hooks to execute when entering an environment
18    #[serde(skip_serializing_if = "Option::is_none")]
19    #[serde(rename = "onEnter")]
20    pub on_enter: Option<HookList>,
21
22    /// Hooks to execute when exiting an environment
23    #[serde(skip_serializing_if = "Option::is_none")]
24    #[serde(rename = "onExit")]
25    pub on_exit: Option<HookList>,
26}
27
28/// Hook list can be a single hook or an array of hooks
29#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq)]
30#[serde(untagged)]
31pub enum HookList {
32    Single(Hook),
33    Multiple(Vec<Hook>),
34}
35
36impl HookList {
37    /// Convert to a vector of hooks
38    pub fn to_vec(&self) -> Vec<Hook> {
39        match self {
40            HookList::Single(hook) => vec![hook.clone()],
41            HookList::Multiple(hooks) => hooks.clone(),
42        }
43    }
44}
45
46/// Root Cuenv configuration structure
47#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Default)]
48pub struct Cuenv {
49    /// Configuration settings
50    #[serde(skip_serializing_if = "Option::is_none")]
51    pub config: Option<Config>,
52
53    /// Environment variables configuration
54    #[serde(skip_serializing_if = "Option::is_none")]
55    pub env: Option<Env>,
56
57    /// Hooks configuration
58    #[serde(skip_serializing_if = "Option::is_none")]
59    pub hooks: Option<Hooks>,
60
61    /// Tasks configuration
62    #[serde(default)]
63    pub tasks: HashMap<String, TaskDefinition>,
64}
65
66impl Cuenv {
67    /// Create a new empty Cuenv configuration
68    pub fn new() -> Self {
69        Self::default()
70    }
71
72    /// Get hooks to execute when entering environment
73    pub fn on_enter_hooks(&self) -> Vec<Hook> {
74        self.hooks
75            .as_ref()
76            .and_then(|h| h.on_enter.as_ref())
77            .map(|h| h.to_vec())
78            .unwrap_or_default()
79    }
80
81    /// Get hooks to execute when exiting environment
82    pub fn on_exit_hooks(&self) -> Vec<Hook> {
83        self.hooks
84            .as_ref()
85            .and_then(|h| h.on_exit.as_ref())
86            .map(|h| h.to_vec())
87            .unwrap_or_default()
88    }
89}