use std::collections::HashMap;
use std::path::Path;
use wick_packet::RuntimeConfig;
use super::template_config::Renderable;
use crate::config::{self};
use crate::error::ManifestError;
#[derive(Debug, Clone, PartialEq, derive_asset_container::AssetManager, serde::Serialize)]
#[asset(asset(config::AssetReference))]
#[must_use]
#[serde(rename_all = "kebab-case")]
pub enum ImportDefinition {
Component(config::ComponentDefinition),
Types(config::components::TypesComponent),
}
crate::impl_from_for!(ImportDefinition, Component, config::ComponentDefinition);
crate::impl_from_for!(ImportDefinition, Types, config::components::TypesComponent);
impl ImportDefinition {
#[must_use]
pub const fn is_reference(&self) -> bool {
if let ImportDefinition::Component(c) = self {
return c.is_reference();
}
false
}
pub const fn component(component: config::ComponentDefinition) -> Self {
Self::Component(component)
}
pub const fn types(component: config::components::TypesComponent) -> Self {
Self::Types(component)
}
#[must_use]
pub fn config(&self) -> Option<&RuntimeConfig> {
match self {
ImportDefinition::Component(v) => v.config().and_then(|v| v.value()),
ImportDefinition::Types(_) => None,
}
}
#[must_use]
pub fn provide(&self) -> Option<&HashMap<String, String>> {
match self {
ImportDefinition::Component(v) => v.provide(),
ImportDefinition::Types(_) => None,
}
}
#[must_use]
pub const fn kind(&self) -> ImportKind {
match self {
ImportDefinition::Component(_) => ImportKind::Component,
ImportDefinition::Types(_) => ImportKind::Types,
}
}
}
#[derive(Debug, Clone, Copy)]
pub enum ImportKind {
Component,
Types,
}
impl std::fmt::Display for ImportKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ImportKind::Component => write!(f, "component"),
ImportKind::Types => write!(f, "types"),
}
}
}
impl Renderable for ImportDefinition {
fn render_config(
&mut self,
source: Option<&Path>,
root_config: Option<&RuntimeConfig>,
env: Option<&HashMap<String, String>>,
) -> Result<(), ManifestError> {
match self {
ImportDefinition::Component(v) => v.render_config(source, root_config, env),
ImportDefinition::Types(_) => Ok(()),
}
}
}