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
use std::collections::HashMap;

use serde::{Deserialize, Serialize};

use super::{
    CapabilityCpuV1, CapabilityFileSystemV1, CapabilityLoggingV1, CapabilityMemorySwapV1,
    CapabilityNetworkDnsV1, CapabilityNetworkGatewayV1, CapabilityNetworkV1,
    CapabilityPersistentMemoryV1, Merge,
};

#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
pub enum CapabilityV1 {
    #[serde(rename = "cpu")]
    Cpu(CapabilityCpuV1),
    #[serde(rename = "fs")]
    FileSystem(CapabilityFileSystemV1),
    #[serde(rename = "memory_swap")]
    MemorySwap(CapabilityMemorySwapV1),
    #[serde(rename = "memory_persistent")]
    MemoryPersistent(CapabilityPersistentMemoryV1),
    #[serde(rename = "network")]
    Network(CapabilityNetworkV1),
    #[serde(rename = "network_dns")]
    NetworkDns(CapabilityNetworkDnsV1),
    #[serde(rename = "network_gateway")]
    NetworkGateway(CapabilityNetworkGatewayV1),
}

#[derive(Serialize, Deserialize, Default, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
pub struct CapabilityMapV1 {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cpu: Option<CapabilityCpuV1>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fs: Option<CapabilityFileSystemV1>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub memory_swap: Option<CapabilityMemorySwapV1>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub memory_persistent: Option<CapabilityPersistentMemoryV1>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub network: Option<CapabilityNetworkV1>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub network_dns: Option<CapabilityNetworkDnsV1>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub network_gateway: Option<CapabilityNetworkGatewayV1>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub logging: Option<CapabilityLoggingV1>,

    /// Additional unknown capabilities.
    #[serde(flatten)]
    pub other: HashMap<String, serde_json::Value>,
}

impl Merge for CapabilityMapV1 {
    fn merge_extend(self, other: &Self) -> Self {
        Self {
            cpu: self.cpu.merge_extend(&other.cpu),
            fs: self.fs.merge_extend(&other.fs),
            memory_swap: self.memory_swap.merge_extend(&other.memory_swap),
            memory_persistent: self
                .memory_persistent
                .merge_extend(&other.memory_persistent),
            network: self.network.merge_extend(&other.network),
            network_dns: self.network_dns.merge_extend(&other.network_dns),
            network_gateway: self.network_gateway.merge_extend(&other.network_gateway),
            logging: self.logging.merge_extend(&other.logging),
            other: self.other,
        }
    }
}