wick_config/config/common/
bindings.rs

1#![allow(missing_docs)] // delete when we move away from the `property` crate.
2
3use std::collections::HashMap;
4use std::path::Path;
5
6use asset_container::AssetManager;
7use wick_asset_reference::AssetReference;
8use wick_interface_types::OperationSignatures;
9use wick_packet::RuntimeConfig;
10
11use super::template_config::Renderable;
12
13#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize)]
14pub struct BoundIdentifier {
15  id: String,
16}
17
18impl std::fmt::Display for BoundIdentifier {
19  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20    self.id.fmt(f)
21  }
22}
23
24impl BoundIdentifier {
25  pub fn new<T: Into<String>>(id: T) -> Self {
26    Self { id: id.into() }
27  }
28
29  #[must_use]
30  pub fn id(&self) -> &str {
31    &self.id
32  }
33}
34
35impl From<&str> for BoundIdentifier {
36  fn from(id: &str) -> Self {
37    Self::new(id)
38  }
39}
40
41impl From<String> for BoundIdentifier {
42  fn from(id: String) -> Self {
43    Self::new(id)
44  }
45}
46
47#[derive(Debug, Clone, PartialEq, serde::Serialize)]
48/// A binding between an identifier and a target.
49pub struct Binding<T> {
50  /// The namespace to reference the collection's components on.
51  pub(crate) id: BoundIdentifier,
52  /// The kind/type of the collection.
53  pub(crate) kind: T,
54}
55
56impl<T> Renderable for Binding<T>
57where
58  T: Renderable,
59{
60  fn render_config(
61    &mut self,
62    source: Option<&Path>,
63    root_config: Option<&RuntimeConfig>,
64    env: Option<&HashMap<String, String>>,
65  ) -> Result<(), crate::error::ManifestError> {
66    self.kind.render_config(source, root_config, env)
67  }
68}
69
70impl<T> AssetManager for Binding<T>
71where
72  T: AssetManager<Asset = AssetReference>,
73{
74  type Asset = AssetReference;
75
76  fn assets(&self) -> asset_container::Assets<Self::Asset> {
77    self.kind.assets()
78  }
79
80  fn set_baseurl(&self, baseurl: &Path) {
81    self.kind.set_baseurl(baseurl);
82  }
83}
84
85impl<T> Binding<T> {
86  /// Create a new [Binding<ImportDefinition>] with specified name and [ImportDefinition].
87  pub fn new<K: Into<String>, INTO: Into<T>>(name: K, kind: INTO) -> Self {
88    Self {
89      id: BoundIdentifier::new(name),
90      kind: kind.into(),
91    }
92  }
93
94  /// Get the ID for the binding.
95  #[must_use]
96  pub fn id(&self) -> &str {
97    self.id.id()
98  }
99
100  /// Get the [BoundIdentifier] for the binding.
101  #[must_use]
102  pub const fn binding(&self) -> &BoundIdentifier {
103    &self.id
104  }
105
106  /// Get the kind for the binding.
107  #[must_use]
108  pub const fn kind(&self) -> &T {
109    &self.kind
110  }
111}
112
113impl<T> OperationSignatures for Binding<T>
114where
115  T: OperationSignatures,
116{
117  fn operation_signatures(&self) -> Vec<wick_interface_types::OperationSignature> {
118    self.kind.operation_signatures()
119  }
120}