#![allow(missing_docs)] use std::collections::HashMap;
use std::path::Path;
use wick_packet::RuntimeConfig;
use crate::config::template_config::Renderable;
use crate::config::TemplateConfig;
use crate::error::ManifestError;
#[derive(Debug, Clone, serde::Serialize)]
#[must_use]
pub enum ResourceRestriction {
Volume(VolumeRestriction),
Url(UrlRestriction),
TcpPort(PortRestriction),
UdpPort(PortRestriction),
}
impl Renderable for ResourceRestriction {
fn render_config(
&mut self,
source: Option<&Path>,
root_config: Option<&RuntimeConfig>,
env: Option<&HashMap<String, String>>,
) -> Result<(), ManifestError> {
match self {
Self::Volume(restriction) => restriction.render_config(source, root_config, env),
Self::Url(restriction) => restriction.render_config(source, root_config, env),
Self::TcpPort(restriction) | Self::UdpPort(restriction) => restriction.render_config(source, root_config, env),
}
}
}
#[derive(Debug, Clone, property::Property, serde::Serialize)]
#[property(get(public), set(private), mut(disable))]
pub struct VolumeRestriction {
pub(crate) components: Vec<String>,
pub(crate) allow: TemplateConfig<String>,
}
impl VolumeRestriction {
#[must_use]
pub fn new_from_template(components: Vec<String>, allow: impl Into<String>) -> Self {
Self {
components,
allow: TemplateConfig::new_template(allow.into()),
}
}
}
impl Renderable for VolumeRestriction {
fn render_config(
&mut self,
source: Option<&Path>,
root_config: Option<&RuntimeConfig>,
env: Option<&HashMap<String, String>>,
) -> Result<(), ManifestError> {
self.allow.set_value(self.allow.render(source, root_config, env)?);
Ok(())
}
}
#[derive(Debug, Clone, property::Property, serde::Serialize)]
#[property(get(public), set(private), mut(disable))]
pub struct UrlRestriction {
pub(crate) components: Vec<String>,
pub(crate) allow: TemplateConfig<String>,
}
impl UrlRestriction {
#[must_use]
pub fn new_from_template(components: Vec<String>, allow: impl Into<String>) -> Self {
Self {
components,
allow: TemplateConfig::new_template(allow.into()),
}
}
}
impl Renderable for UrlRestriction {
fn render_config(
&mut self,
source: Option<&Path>,
root_config: Option<&RuntimeConfig>,
env: Option<&HashMap<String, String>>,
) -> Result<(), ManifestError> {
self.allow.set_value(self.allow.render(source, root_config, env)?);
Ok(())
}
}
#[derive(Debug, Clone, property::Property, serde::Serialize)]
#[property(get(public), set(private), mut(disable))]
pub struct PortRestriction {
pub(crate) components: Vec<String>,
pub(crate) address: TemplateConfig<String>,
pub(crate) port: TemplateConfig<String>,
}
impl PortRestriction {
#[must_use]
pub fn new_from_template(components: Vec<String>, address: impl Into<String>, port: impl Into<String>) -> Self {
Self {
components,
address: TemplateConfig::new_template(address.into()),
port: TemplateConfig::new_template(port.into()),
}
}
}
impl Renderable for PortRestriction {
fn render_config(
&mut self,
source: Option<&Path>,
root_config: Option<&RuntimeConfig>,
env: Option<&HashMap<String, String>>,
) -> Result<(), ManifestError> {
self.address.set_value(self.address.render(source, root_config, env)?);
self.port.set_value(self.port.render(source, root_config, env)?);
Ok(())
}
}