edge-schema 0.1.0

Shared schema types for Wasmer Edge.
Documentation
use bytesize::ByteSize;
use serde::{Deserialize, Serialize};

use crate::NetworkId;

use super::Merge;

#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
pub struct NetworkTokenV1 {
    pub network_id: NetworkId,
}

/// Instance network configuration.
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
pub struct CapabilityNetworkV1 {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub enabled: Option<bool>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub network_id: Option<NetworkId>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub ingress: Option<NetworkIngressV1>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub egress: Option<NetworkEgressV1>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub vpn: Option<NetworkVpnV1>,
}

impl Merge for CapabilityNetworkV1 {
    fn merge_extend(self, other: &Self) -> Self {
        Self {
            enabled: self.enabled.merge_extend(&other.enabled),
            network_id: self.network_id,
            ingress: self.ingress.merge_extend(&other.ingress),
            egress: self.egress.merge_extend(&other.egress),
            vpn: self.vpn.merge_extend(&other.vpn),
        }
    }
}

#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
pub struct NetworkIngressV1 {
    pub enabled: Option<bool>,
    #[schemars(with = "String")]
    pub maximum_bandwidth_per_second: Option<ByteSize>,
}

impl Merge for NetworkIngressV1 {
    fn merge_extend(self, other: &Self) -> Self {
        Self {
            enabled: self.enabled.merge_extend(&other.enabled),
            maximum_bandwidth_per_second: self
                .maximum_bandwidth_per_second
                .merge_extend(&other.maximum_bandwidth_per_second),
        }
    }
}

#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
pub struct NetworkFilterV1 {
    /// List of domain names that the VPN connection is allowed to invoke
    #[serde(skip_serializing_if = "Option::is_none")]
    pub domains: Option<Vec<String>>,
}

impl Merge for NetworkFilterV1 {
    fn merge_extend(self, other: &Self) -> Self {
        Self {
            domains: self.domains.merge_extend(&other.domains),
        }
    }
}

#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
pub struct NetworkEgressV1 {
    pub enabled: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub filter: Option<NetworkFilterV1>,
    #[schemars(with = "String")]
    pub maximum_bandwidth_per_second: Option<ByteSize>,
}

impl Merge for NetworkEgressV1 {
    fn merge_extend(self, other: &Self) -> Self {
        Self {
            enabled: self.enabled.merge_extend(&other.enabled),
            filter: self.filter.merge_extend(&other.filter),
            maximum_bandwidth_per_second: self
                .maximum_bandwidth_per_second
                .merge_extend(&other.maximum_bandwidth_per_second),
        }
    }
}

#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
pub struct NetworkVpnV1 {
    pub enabled: Option<bool>,
    pub public: Option<bool>,
    pub can_egress: Option<bool>,
    #[schemars(with = "String")]
    pub maximum_bandwidth_per_second: Option<ByteSize>,
}

impl NetworkVpnV1 {
    pub fn is_enabled(&self) -> bool {
        self.enabled.unwrap_or(true)
    }

    pub fn is_public(&self) -> bool {
        self.public.unwrap_or(true)
    }

    pub fn can_egress(&self) -> bool {
        self.can_egress.unwrap_or(true)
    }
}

impl Merge for NetworkVpnV1 {
    fn merge_extend(self, other: &Self) -> Self {
        Self {
            enabled: self.enabled.merge_extend(&other.enabled),
            public: self.public.merge_extend(&other.public),
            can_egress: self.can_egress.merge_extend(&other.public),
            maximum_bandwidth_per_second: self
                .maximum_bandwidth_per_second
                .merge_extend(&other.maximum_bandwidth_per_second),
        }
    }
}