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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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))]
/// The kinds of collections that can operate in a flow.
#[must_use]
#[serde(rename_all = "kebab-case")]
pub enum ImportDefinition {
  /// A wick component.
  Component(config::ComponentDefinition),
  /// A type manifest.
  Types(config::components::TypesComponent),
}

crate::impl_from_for!(ImportDefinition, Component, config::ComponentDefinition);
crate::impl_from_for!(ImportDefinition, Types, config::components::TypesComponent);

impl ImportDefinition {
  /// Returns true if the definition is a reference to another component.
  #[must_use]
  pub const fn is_reference(&self) -> bool {
    if let ImportDefinition::Component(c) = self {
      return c.is_reference();
    }
    false
  }

  /// Initialize a new [ImportDefinition] for the specified [config::ComponentDefinition].
  pub const fn component(component: config::ComponentDefinition) -> Self {
    Self::Component(component)
  }

  /// Initialize a new [ImportDefinition] for the specified [config::components::TypesComponent].
  pub const fn types(component: config::components::TypesComponent) -> Self {
    Self::Types(component)
  }

  /// Get the configuration associated with this import.
  #[must_use]
  pub fn config(&self) -> Option<&RuntimeConfig> {
    match self {
      ImportDefinition::Component(v) => v.config().and_then(|v| v.value()),
      ImportDefinition::Types(_) => None,
    }
  }

  /// Returns any components this configuration provides to the implementation.
  #[must_use]
  pub fn provide(&self) -> Option<&HashMap<String, String>> {
    match self {
      ImportDefinition::Component(v) => v.provide(),
      ImportDefinition::Types(_) => None,
    }
  }

  /// Get the configuration kind for this import.
  #[must_use]
  pub const fn kind(&self) -> ImportKind {
    match self {
      ImportDefinition::Component(_) => ImportKind::Component,
      ImportDefinition::Types(_) => ImportKind::Types,
    }
  }
}

/// The kind of import an [ImportDefinition] is.
#[derive(Debug, Clone, Copy)]
pub enum ImportKind {
  /// A component import.
  Component,
  /// A types import.
  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(()),
    }
  }
}