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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
use std::collections::HashMap;
use std::path::{Path, PathBuf};

use asset_container::{Asset, AssetFlags};
use url::Url;
use wick_asset_reference::AssetReference;
use wick_packet::RuntimeConfig;

use super::template_config::Renderable;
use crate::config::TemplateConfig;
use crate::error::ManifestError;

crate::impl_from_for!(ResourceDefinition, TcpPort);
crate::impl_from_for!(ResourceDefinition, UdpPort);
crate::impl_from_for!(ResourceDefinition, Volume);
crate::impl_from_for!(ResourceDefinition, Url, UrlResource);

/// A resource type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ResourceKind {
  TcpPort,
  UdpPort,
  Url,
  Volume,
}

impl std::fmt::Display for ResourceKind {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match self {
      Self::TcpPort => write!(f, "TcpPort"),
      Self::UdpPort => write!(f, "UdpPort"),
      Self::Url => write!(f, "Url"),
      Self::Volume => write!(f, "Volume"),
    }
  }
}

#[derive(Debug, Clone, derive_asset_container::AssetManager, serde::Serialize, PartialEq, Hash, Eq)]
#[asset(asset(AssetReference))]
/// Normalized representation of a resource definition.
#[serde(rename_all = "kebab-case")]
pub enum ResourceDefinition {
  /// A TCP port.
  #[asset(skip)]
  TcpPort(TcpPort),
  /// A UDP port.
  #[asset(skip)]
  UdpPort(UdpPort),
  /// A URL resource.
  #[asset(skip)]
  Url(UrlResource),
  /// A filesystem or network volume.
  Volume(Volume),
}

impl Renderable for ResourceDefinition {
  fn render_config(
    &mut self,
    source: Option<&Path>,
    root_config: Option<&RuntimeConfig>,
    env: Option<&HashMap<String, String>>,
  ) -> Result<(), ManifestError> {
    match self {
      ResourceDefinition::TcpPort(v) => v.render_config(source, root_config, env),
      ResourceDefinition::UdpPort(v) => v.render_config(source, root_config, env),
      ResourceDefinition::Url(v) => v.render_config(source, root_config, env),
      ResourceDefinition::Volume(v) => v.render_config(source, root_config, env),
    }
  }
}

impl ResourceDefinition {
  #[must_use]
  pub const fn kind(&self) -> ResourceKind {
    match self {
      ResourceDefinition::TcpPort(_) => ResourceKind::TcpPort,
      ResourceDefinition::UdpPort(_) => ResourceKind::UdpPort,
      ResourceDefinition::Url(_) => ResourceKind::Url,
      ResourceDefinition::Volume(_) => ResourceKind::Volume,
    }
  }

  pub fn try_tcpport(self) -> Result<TcpPort, ManifestError> {
    self.try_into()
  }

  pub fn try_udpport(self) -> Result<UdpPort, ManifestError> {
    self.try_into()
  }

  pub fn try_url(self) -> Result<UrlResource, ManifestError> {
    self.try_into()
  }

  pub fn try_volume(self) -> Result<Volume, ManifestError> {
    self.try_into()
  }
}

impl TryFrom<String> for UrlResource {
  type Error = crate::Error;

  fn try_from(value: String) -> Result<Self, Self::Error> {
    url::Url::parse(&value)
      .map_err(|_| Self::Error::InvalidUrl(value.clone()))
      .map(Self::new)
  }
}

#[derive(Debug, Clone, PartialEq, Hash, Eq, derive_builder::Builder, serde::Serialize)]
/// A filesystem or network volume.
#[must_use]
pub struct Volume {
  path: TemplateConfig<AssetReference>,
}

impl Volume {
  pub fn new(template: String) -> Self {
    let template = TemplateConfig::new_template(template);
    Self { path: template }
  }

  #[cfg(feature = "v1")]
  pub(crate) fn unrender(&self) -> Result<String, ManifestError> {
    self.path.unrender()
  }

  pub fn path(&self) -> Result<PathBuf, ManifestError> {
    if let Some(path) = &self.path.value {
      Ok(path.path()?)
    } else {
      Err(ManifestError::UnrenderedConfiguration(format!(
        "{:?}",
        self.path.template
      )))
    }
  }
}

impl Renderable for Volume {
  fn render_config(
    &mut self,
    source: Option<&Path>,
    root_config: Option<&RuntimeConfig>,
    env: Option<&HashMap<String, String>>,
  ) -> Result<(), ManifestError> {
    self.path.set_value(self.path.render(source, root_config, env)?);
    Ok(())
  }
}

impl asset_container::AssetManager for Volume {
  type Asset = AssetReference;

  fn assets(&self) -> asset_container::Assets<Self::Asset> {
    asset_container::Assets::new(
      self
        .path
        .value
        .as_ref()
        .map_or_else(Vec::new, |v| vec![std::borrow::Cow::Borrowed(v)]),
      self.get_asset_flags(),
    )
  }

  fn set_baseurl(&self, baseurl: &Path) {
    if let Some(path) = &self.path.value {
      path.update_baseurl(baseurl);
      match self.path() {
        Ok(p) => {
          if !p.is_dir() {
            tracing::warn!(%path,"volume path is not a directory");
          }
        }
        Err(e) => {
          tracing::warn!(%path,error=%e,"volume path could not be resolved");
        }
      }
    }
  }

  fn get_asset_flags(&self) -> u32 {
    AssetFlags::Lazy.bits()
  }
}

#[derive(Debug, Clone, PartialEq, Hash, Eq, property::Property, serde::Serialize)]
/// A URL resource.
#[must_use]
#[property(get(public), set(private), mut(disable))]
pub struct UrlResource {
  /// The URL
  pub(crate) url: TemplateConfig<Url>,
}

impl UrlResource {
  /// Create a new URL Resource.
  pub const fn new(url: Url) -> Self {
    Self {
      url: TemplateConfig::new_value(url),
    }
  }

  /// Get the URL.
  #[must_use]
  #[allow(clippy::missing_const_for_fn)]
  pub fn into_inner(self) -> TemplateConfig<Url> {
    self.url
  }
}

impl std::fmt::Display for UrlResource {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    write!(f, "{}", self.url)
  }
}

impl Renderable for UrlResource {
  fn render_config(
    &mut self,
    source: Option<&Path>,
    root_config: Option<&RuntimeConfig>,
    env: Option<&HashMap<String, String>>,
  ) -> Result<(), ManifestError> {
    self.url.set_value(self.url.render(source, root_config, env)?);
    Ok(())
  }
}

#[derive(Debug, Clone, PartialEq, Hash, Eq, property::Property, serde::Serialize)]
/// Normalized representation of a TCP port configuration.
#[property(get(public), set(private), mut(disable))]
pub struct TcpPort {
  /// The port number.
  pub(crate) port: TemplateConfig<u16>,
  /// The address to bind to.
  pub(crate) host: TemplateConfig<String>,
}

impl TcpPort {
  /// Create a new TCP port configuration.
  pub fn new<T: Into<String>>(host: T, port: u16) -> Self {
    Self {
      port: TemplateConfig::new_value(port),
      host: TemplateConfig::new_value(host.into()),
    }
  }

  /// Get the address and port as a string.
  #[must_use]
  pub fn address(&self) -> String {
    format!("{}:{}", self.host, self.port)
  }
}

impl Renderable for TcpPort {
  fn render_config(
    &mut self,
    source: Option<&Path>,
    root_config: Option<&RuntimeConfig>,
    env: Option<&HashMap<String, String>>,
  ) -> Result<(), ManifestError> {
    self.port.set_value(self.port.render(source, root_config, env)?);
    self.host.set_value(self.host.render(source, root_config, env)?);
    Ok(())
  }
}

#[derive(Debug, Clone, PartialEq, Hash, Eq, property::Property, serde::Serialize)]
/// Normalized representation of a UDP port configuration.
#[property(get(public), set(private), mut(disable))]
pub struct UdpPort {
  /// The port number.
  pub(crate) port: TemplateConfig<u16>,
  /// The address to bind to.
  pub(crate) host: TemplateConfig<String>,
}

impl UdpPort {
  /// Create a new UDP port configuration.
  pub fn new<T: Into<String>>(host: T, port: u16) -> Self {
    Self {
      port: TemplateConfig::new_value(port),
      host: TemplateConfig::new_value(host.into()),
    }
  }

  /// Get the address and port as a string.
  #[must_use]
  pub fn address(&self) -> String {
    format!("{}:{}", self.host, self.port)
  }
}

impl Renderable for UdpPort {
  fn render_config(
    &mut self,
    source: Option<&Path>,
    root_config: Option<&RuntimeConfig>,
    env: Option<&HashMap<String, String>>,
  ) -> Result<(), ManifestError> {
    self.port.set_value(self.port.render(source, root_config, env)?);
    self.host.set_value(self.host.render(source, root_config, env)?);
    Ok(())
  }
}

#[cfg(test)]
mod test {
  use anyhow::Result;

  use super::*;

  #[test]
  fn try_into_urlresource() -> Result<()> {
    let urlstr = "postgres://postgres:password!12345@nas.glhf.lan:55432/wick_test".to_owned();
    let url_resource: UrlResource = urlstr.clone().try_into()?;
    let url: Url = urlstr.parse()?;

    assert_eq!(url_resource.url.value_unchecked().as_str(), &urlstr);
    assert_eq!(url_resource.url.value_unchecked(), &url);

    Ok(())
  }
}