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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
#![allow(missing_docs)] // delete when we move away from the `property` crate.

/// Specific component-level configuration and types.
pub mod components;

pub(crate) mod app_config;
mod cache;
pub(crate) mod common;
pub(crate) mod component_config;
pub(crate) mod configuration_tree;
pub(crate) mod import_cache;
pub(crate) mod lockdown_config;
pub(crate) mod permissions;
pub(crate) mod test_config;
pub(crate) mod types_config;

use std::collections::HashMap;
use std::path::{Path, PathBuf};

pub use app_config::*;
use asset_container::Asset;
pub use common::*;
pub use component_config::*;
pub use configuration_tree::*;
pub use lockdown_config::*;
pub use permissions::{Permissions, PermissionsBuilder};
pub use test_config::*;
use tracing::debug;
pub use types_config::*;
use wick_asset_reference::{AssetReference, FetchOptions};
use wick_interface_types::Field;
use wick_packet::validation::expect_configuration_matches;
use wick_packet::{Entity, RuntimeConfig};

use self::template_config::Renderable;
use crate::load::resolve_configuration;
use crate::lockdown::Lockdown;
use crate::{Error, Imports, RootConfig};

#[derive(Debug, Clone, property::Property)]
#[property(get(public), set(public), mut(public, suffix = "_mut"))]
/// A builder for [WickConfiguration].
pub struct UninitializedConfiguration {
  /// The manifest to use as a base.
  pub(crate) manifest: WickConfiguration,
  /// The root configuration to use when rendering internal configuration templates.
  pub(crate) root_config: Option<RuntimeConfig>,
  /// The environment this configuration can use when rendering internal configuration templates.
  pub(crate) env: Option<HashMap<String, String>>,
  /// The lockdown configuration that will validate a configuration is safe to run.
  pub(crate) lockdown_config: Option<LockdownConfiguration>,
}

impl UninitializedConfiguration {
  #[must_use]
  /// Create a new builder with the given manifest.
  pub const fn new(manifest: WickConfiguration) -> Self {
    Self {
      manifest,
      root_config: None,
      env: None,
      lockdown_config: None,
    }
  }

  /// Return the inner, uninitialized [WickConfiguration].
  #[must_use]
  #[allow(clippy::missing_const_for_fn)]
  pub fn into_inner(self) -> WickConfiguration {
    self.manifest
  }

  /// Build, initialize and return a [WickConfiguration].
  pub fn finish(mut self) -> Result<WickConfiguration, Error> {
    debug!(root_config=?self.root_config, env=?self.env.as_ref().map(|c|format!("{} variables",c.len())), "initializing configuration");

    expect_configuration_matches(
      self
        .manifest
        .source()
        .map_or("<unknown>", |p| p.to_str().unwrap_or("<invalid>")),
      self.root_config.as_ref(),
      self.manifest.config(),
    )
    .map_err(Error::ConfigurationInvalid)?;
    self.manifest.set_env(self.env);
    self.manifest.set_root_config(self.root_config);
    self.manifest.initialize()?;
    self.manifest.validate()?;
    Ok(self.manifest)
  }
}

impl Imports for UninitializedConfiguration {
  fn imports(&self) -> &[Binding<ImportDefinition>] {
    self.manifest.imports()
  }
}

impl Lockdown for UninitializedConfiguration {
  fn lockdown(&self, id: Option<&str>, lockdown: &LockdownConfiguration) -> Result<(), crate::lockdown::LockdownError> {
    self.manifest.lockdown(id, lockdown)?;
    Ok(())
  }
}

/// A catch-all enum for root-level Wick configurations.
#[derive(Debug, Clone, derive_asset_container::AssetManager, serde::Serialize)]
#[asset(asset(AssetReference))]
#[serde(untagged)]

pub enum WickConfiguration {
  /// A [component_config::ComponentConfiguration] configuration.
  Component(ComponentConfiguration),
  /// An [app_config::AppConfiguration] configuration.
  App(AppConfiguration),
  /// A [types_config::TypesConfiguration] configuration.
  Types(TypesConfiguration),
  /// A [test_config::TestConfiguration] configuration.
  Tests(TestConfiguration),
  /// A [lockdown_config::LockdownConfiguration] configuration.
  Lockdown(LockdownConfiguration),
}

impl WickConfiguration {
  /// Fetch a configuration and all referenced assets from a path.
  ///
  /// # Example
  ///
  /// ```rust
  /// # tokio_test::block_on(async {
  /// use wick_config::WickConfiguration;
  /// use wick_asset_reference::FetchOptions;
  ///
  /// let opts = FetchOptions::default();
  /// let env : HashMap<String,String> = std::env::vars().collect();
  /// let root_config = None;
  ///
  /// let manifest = WickConfiguration::fetch_tree("path/to/manifest.yaml", root_config, env, opts).await?;
  /// # Ok::<_,anyhow::Error>(())
  /// # });
  /// ```
  pub async fn fetch_tree(
    path: impl Into<AssetReference> + Send,
    root_config: Option<RuntimeConfig>,
    root_env: Option<HashMap<String, String>>,
    options: FetchOptions,
  ) -> Result<ConfigurationTreeNode<WickConfiguration>, Error> {
    let mut config = Self::fetch(path, options.clone()).await?;
    let source = config.manifest.source().map(ToOwned::to_owned);
    config
      .manifest
      .render_config(source.as_deref(), root_config.as_ref(), root_env.as_ref())?;

    let renderer = |runtime_config: Option<RuntimeConfig>, mut child: UninitializedConfiguration| {
      child.set_root_config(runtime_config);
      child.finish()
    };

    let children = fetch_children(&config, options, &renderer).await?;
    Ok(ConfigurationTreeNode::new(
      Entity::LOCAL.into(),
      config.finish()?,
      children,
    ))
  }

  /// Fetch a configuration and all referenced assets from a path.
  ///
  /// # Example
  ///
  /// ```rust
  /// # tokio_test::block_on(async {
  /// use wick_config::WickConfiguration;
  /// use wick_asset_reference::FetchOptions;
  ///
  /// let opts = FetchOptions::default();
  ///
  /// let manifest = WickConfiguration::fetch_uninitialized_tree("path/to/manifest.yaml", opts).await?;
  /// # Ok::<_,anyhow::Error>(())
  /// # });
  /// ```
  pub async fn fetch_uninitialized_tree(
    path: impl Into<AssetReference> + Send,
    options: FetchOptions,
  ) -> Result<ConfigurationTreeNode<UninitializedConfiguration>, Error> {
    let config = Self::fetch(path, options.clone()).await?;

    let children = fetch_children(&config, options, &|_, b| Ok(b)).await?;

    Ok(ConfigurationTreeNode::new(Entity::LOCAL.into(), config, children))
  }

  /// Fetch a configuration from a path.
  ///
  /// # Example
  ///
  /// ```rust
  /// # tokio_test::block_on(async {
  /// use wick_config::WickConfiguration;
  /// use wick_asset_reference::FetchOptions;
  ///
  /// let opts = FetchOptions::default();
  ///
  /// let manifest = WickConfiguration::fetch("path/to/manifest.yaml", opts).await?;
  /// # Ok::<_,anyhow::Error>(())
  /// # });
  /// ```
  ///
  pub async fn fetch(
    asset: impl Into<AssetReference> + Send,
    options: FetchOptions,
  ) -> Result<UninitializedConfiguration, Error> {
    let asset: AssetReference = asset.into();

    let cache_result = cache::CONFIG_CACHE.lock().get(&asset).cloned();

    let config = if let Some(config) = cache_result {
      tracing::trace!(cache_hit = true, asset=%asset.location(), "config::fetch");
      config
    } else {
      tracing::trace!(cache_hit = false, asset=%asset.location(), "config::fetch");
      let bytes = asset.fetch(options.clone()).await?;
      let source = asset.path().unwrap_or_else(|e| PathBuf::from(format!("<ERROR:{}>", e)));
      let config = WickConfiguration::load_from_bytes(&bytes, &Some(source))?;
      config.manifest.update_baseurls();
      match &config.manifest {
        WickConfiguration::Component(c) => {
          c.setup_cache(options).await?;
        }
        WickConfiguration::App(c) => {
          c.setup_cache(options).await?;
        }
        WickConfiguration::Types(_) => {}
        WickConfiguration::Tests(_) => {}
        WickConfiguration::Lockdown(_) => {}
      }

      cache::CONFIG_CACHE.lock().insert(asset.clone(), config.clone());
      config
    };

    Ok(config)
  }

  /// Load a configuration from raw bytes. Pass in an optional source to track where the bytes came from.
  ///
  /// # Example
  ///
  /// ```rust
  /// # tokio_test::block_on(async {
  /// use wick_config::WickConfiguration;
  /// use std::path::PathBuf;
  ///
  /// let path = PathBuf::from("path/to/manifest.yaml");
  ///
  /// let bytes = std::fs::read(&path)?;
  ///
  /// let manifest = WickConfiguration::load_from_bytes(&bytes, &Some(path))?;
  /// # Ok::<_,anyhow::Error>(())
  /// # });
  /// ```
  pub fn load_from_bytes(bytes: &[u8], source: &Option<PathBuf>) -> Result<UninitializedConfiguration, Error> {
    let string = &String::from_utf8(bytes.to_vec()).map_err(|_| Error::Utf8)?;

    resolve_configuration(string, source)
  }

  /// Load a configuration from a string. Pass in an optional source to track where the string came from.
  ///
  /// # Example
  ///
  /// ```rust
  /// # tokio_test::block_on(async {
  /// use std::path::PathBuf;
  /// use wick_config::WickConfiguration;
  ///
  /// let path = PathBuf::from("path/to/manifest.yaml");
  ///
  /// let string = std::fs::read_to_string(&path)?;
  ///
  /// let manifest = WickConfiguration::from_yaml(&string, &Some(path))?;
  /// # Ok::<_,anyhow::Error>(())
  /// # });
  /// ```
  ///
  pub fn from_yaml(src: &str, source: &Option<PathBuf>) -> Result<UninitializedConfiguration, Error> {
    resolve_configuration(src, source)
  }

  /// Convert a WickConfiguration into V1 configuration yaml source.
  ///
  /// # Example
  ///
  /// ```rust
  /// # tokio_test::block_on(async {
  /// use wick_config::WickConfiguration;
  /// use wick_asset_reference::FetchOptions;
  ///
  /// let opts = FetchOptions::default();
  ///
  /// let manifest = WickConfiguration::fetch_all("path/to/manifest.yaml", opts).await?;
  /// let manifest = manifest.finish()?;
  ///
  /// let v1_yaml = manifest.into_v1_yaml()?;
  /// # Ok::<_,anyhow::Error>(())
  /// # });
  /// ```
  #[cfg(feature = "v1")]
  pub fn into_v1_yaml(self) -> Result<String, Error> {
    Ok(serde_yaml::to_string(&self.into_v1()?).unwrap())
  }

  /// Convert a WickConfiguration into a V1 configuration JSON value.
  ///
  /// # Example
  ///
  /// ```rust
  /// # tokio_test::block_on(async {
  /// use wick_config::WickConfiguration;
  /// use wick_asset_reference::FetchOptions;
  ///
  /// let opts = FetchOptions::default();
  ///
  /// let manifest = WickConfiguration::fetch_all("path/to/manifest.yaml", opts).await?;
  /// let manifest = manifest.finish()?;
  ///
  /// let v1_json = manifest.into_v1_json()?;
  /// # Ok::<_,anyhow::Error>(())
  /// # });
  /// ```
  #[cfg(feature = "v1")]
  pub fn into_v1_json(self) -> Result<serde_json::Value, Error> {
    Ok(serde_json::to_value(&self.into_v1()?).unwrap())
  }

  #[cfg(feature = "v1")]
  fn into_v1(self) -> Result<crate::v1::WickConfig, Error> {
    match self {
      WickConfiguration::Component(c) => Ok(crate::v1::WickConfig::ComponentConfiguration(c.try_into()?)),
      WickConfiguration::App(c) => Ok(crate::v1::WickConfig::AppConfiguration(c.try_into()?)),
      WickConfiguration::Types(c) => Ok(crate::v1::WickConfig::TypesConfiguration(c.try_into()?)),
      WickConfiguration::Tests(c) => Ok(crate::v1::WickConfig::TestConfiguration(c.try_into()?)),
      WickConfiguration::Lockdown(c) => Ok(crate::v1::WickConfig::LockdownConfiguration(c.try_into()?)),
    }
  }

  /// Get the name (if any) associated with the inner configuration.
  #[must_use]
  pub fn name(&self) -> Option<&str> {
    match self {
      WickConfiguration::Component(v) => v.name().map(|s| s.as_str()),
      WickConfiguration::App(v) => Some(v.name()),
      WickConfiguration::Types(v) => v.name().map(|s| s.as_str()),
      WickConfiguration::Tests(v) => v.name().map(|s| s.as_str()),
      WickConfiguration::Lockdown(_) => None,
    }
  }

  /// Get the metadata (if any) associated with the inner configuration.
  #[must_use]
  pub fn metadata(&self) -> Option<&Metadata> {
    match self {
      WickConfiguration::Component(v) => v.metadata(),
      WickConfiguration::App(v) => v.metadata(),
      WickConfiguration::Types(v) => v.metadata(),
      WickConfiguration::Tests(_v) => None,
      WickConfiguration::Lockdown(_v) => None,
    }
  }

  /// Validate this configuration is good.
  pub fn validate(&self) -> Result<(), Error> {
    match self {
      WickConfiguration::Component(v) => v.validate(),
      WickConfiguration::App(v) => v.validate(),
      WickConfiguration::Types(v) => v.validate(),
      WickConfiguration::Tests(v) => v.validate(),
      WickConfiguration::Lockdown(v) => v.validate(),
    }
  }

  /// Get the runtime configuration (if any) associated with the inner configuration.
  #[must_use]
  fn config(&self) -> &[Field] {
    match self {
      WickConfiguration::Component(v) => v.config(),
      WickConfiguration::App(_v) => Default::default(),
      WickConfiguration::Types(_v) => Default::default(),
      WickConfiguration::Tests(_v) => Default::default(),
      WickConfiguration::Lockdown(_v) => Default::default(),
    }
  }

  /// Set the environment variables for a [WickConfiguration].
  fn set_env(&mut self, env: Option<HashMap<String, String>>) -> &mut Self {
    match self {
      WickConfiguration::App(ref mut v) => {
        v.env = env;
      }
      WickConfiguration::Component(_) => (),
      WickConfiguration::Types(_) => (),
      WickConfiguration::Tests(_) => (),
      WickConfiguration::Lockdown(v) => v.env = env,
    }
    self
  }

  /// Get the kind of the inner configuration.
  pub const fn kind(&self) -> ConfigurationKind {
    match self {
      WickConfiguration::Component(_) => ConfigurationKind::Component,
      WickConfiguration::App(_) => ConfigurationKind::App,
      WickConfiguration::Types(_) => ConfigurationKind::Types,
      WickConfiguration::Tests(_) => ConfigurationKind::Tests,
      WickConfiguration::Lockdown(_) => ConfigurationKind::Lockdown,
    }
  }

  /// Get the resources for the configuration, if any
  #[must_use]
  pub fn resources(&self) -> &[Binding<ResourceDefinition>] {
    match self {
      WickConfiguration::Component(c) => c.resources(),
      WickConfiguration::App(c) => c.resources(),
      WickConfiguration::Types(_) => &[],
      WickConfiguration::Tests(_) => &[],
      WickConfiguration::Lockdown(_) => &[],
    }
  }

  /// Get the version (if any) associated with the inner configuration.
  #[must_use]
  pub fn version(&self) -> Option<&str> {
    match self {
      WickConfiguration::Component(v) => v.version(),
      WickConfiguration::App(v) => v.version(),
      WickConfiguration::Types(v) => v.version(),
      WickConfiguration::Tests(_) => None,
      WickConfiguration::Lockdown(_) => None,
    }
  }

  /// Get the package configuration (if any) associated with the inner configuration.
  #[must_use]
  pub fn package(&self) -> Option<&PackageConfig> {
    match self {
      WickConfiguration::Component(v) => v.package(),
      WickConfiguration::App(v) => v.package(),
      WickConfiguration::Types(v) => v.package(),
      WickConfiguration::Tests(_) => None,
      WickConfiguration::Lockdown(_) => None,
    }
  }

  /// Unwrap the inner [ComponentConfiguration], returning an error if it is anything else.
  #[allow(clippy::missing_const_for_fn)]
  pub fn try_component_config(self) -> Result<ComponentConfiguration, Error> {
    match self {
      WickConfiguration::Component(v) => Ok(v),
      _ => Err(Error::UnexpectedConfigurationKind(
        ConfigurationKind::Component,
        self.kind(),
      )),
    }
  }

  /// Unwrap the inner [AppConfiguration], returning an error if it is anything else.
  #[allow(clippy::missing_const_for_fn)]
  pub fn try_app_config(self) -> Result<AppConfiguration, Error> {
    match self {
      WickConfiguration::App(v) => Ok(v),
      _ => Err(Error::UnexpectedConfigurationKind(ConfigurationKind::App, self.kind())),
    }
  }

  /// Unwrap the inner [TestConfiguration], returning an error if it is anything else.
  #[allow(clippy::missing_const_for_fn)]
  pub fn try_test_config(self) -> Result<TestConfiguration, Error> {
    match self {
      WickConfiguration::Tests(v) => Ok(v),
      _ => Err(Error::UnexpectedConfigurationKind(
        ConfigurationKind::Tests,
        self.kind(),
      )),
    }
  }

  /// Unwrap the inner [TypesConfiguration], returning an error if it is anything else.
  #[allow(clippy::missing_const_for_fn)]
  pub fn try_types_config(self) -> Result<TypesConfiguration, Error> {
    match self {
      WickConfiguration::Types(v) => Ok(v),
      _ => Err(Error::UnexpectedConfigurationKind(
        ConfigurationKind::Types,
        self.kind(),
      )),
    }
  }

  /// Unwrap the inner [LockdownConfiguration], returning an error if it is anything else.
  #[allow(clippy::missing_const_for_fn)]
  pub fn try_lockdown_config(self) -> Result<LockdownConfiguration, Error> {
    match self {
      WickConfiguration::Lockdown(v) => Ok(v),
      _ => Err(Error::UnexpectedConfigurationKind(
        ConfigurationKind::Lockdown,
        self.kind(),
      )),
    }
  }

  /// Initialize the configuration.
  fn initialize(&mut self) -> Result<&Self, Error> {
    match self {
      WickConfiguration::Component(v) => {
        v.initialize()?;
      }
      WickConfiguration::App(v) => {
        v.initialize()?;
      }
      WickConfiguration::Types(_) => (),
      WickConfiguration::Tests(v) => {
        v.initialize()?;
      }
      WickConfiguration::Lockdown(v) => {
        v.initialize()?;
      }
    }
    self.update_baseurls();
    Ok(self)
  }

  /// Set the source of the configuration if it is not already set on load.
  pub fn set_source(&mut self, src: &Path) {
    match self {
      WickConfiguration::Component(v) => v.set_source(src),
      WickConfiguration::App(v) => v.set_source(src),
      WickConfiguration::Types(v) => v.set_source(src),
      WickConfiguration::Tests(v) => v.set_source(src),
      WickConfiguration::Lockdown(v) => v.set_source(src),
    }
  }

  fn update_baseurls(&self) {
    match self {
      WickConfiguration::Component(v) => v.update_baseurls(),
      WickConfiguration::App(v) => v.update_baseurls(),
      WickConfiguration::Types(v) => v.update_baseurls(),
      WickConfiguration::Tests(v) => v.update_baseurls(),
      WickConfiguration::Lockdown(v) => v.update_baseurls(),
    }
  }

  /// Get the source of the configuration.
  #[must_use]
  pub fn source(&self) -> Option<&Path> {
    match self {
      WickConfiguration::Component(v) => v.source.as_deref(),
      WickConfiguration::App(v) => v.source.as_deref(),
      WickConfiguration::Types(v) => v.source.as_deref(),
      WickConfiguration::Tests(v) => v.source.as_deref(),
      WickConfiguration::Lockdown(v) => v.source.as_deref(),
    }
  }
}

impl Renderable for WickConfiguration {
  fn render_config(
    &mut self,
    source: Option<&Path>,
    root_config: Option<&RuntimeConfig>,
    env: Option<&HashMap<String, String>>,
  ) -> Result<(), crate::error::ManifestError> {
    match self {
      WickConfiguration::Component(c) => c.render_config(source, root_config, env),
      WickConfiguration::App(c) => c.render_config(source, root_config, env),
      WickConfiguration::Types(c) => c.render_config(source, root_config, env),
      WickConfiguration::Tests(c) => c.render_config(source, root_config, env),
      WickConfiguration::Lockdown(c) => c.render_config(source, root_config, env),
    }
  }
}

impl Lockdown for WickConfiguration {
  fn lockdown(&self, id: Option<&str>, lockdown: &LockdownConfiguration) -> Result<(), crate::lockdown::LockdownError> {
    match self {
      WickConfiguration::Component(v) => v.lockdown(id, lockdown),
      WickConfiguration::App(v) => v.lockdown(id, lockdown),
      WickConfiguration::Types(_) => Ok(()),
      WickConfiguration::Tests(_) => Ok(()),
      WickConfiguration::Lockdown(_) => Ok(()),
    }
  }
}

impl Imports for WickConfiguration {
  fn imports(&self) -> &[Binding<ImportDefinition>] {
    match self {
      WickConfiguration::Component(c) => c.import(),
      WickConfiguration::App(c) => c.import(),
      WickConfiguration::Types(_) => &[],
      WickConfiguration::Tests(_) => &[],
      WickConfiguration::Lockdown(_) => &[],
    }
  }
}

impl RootConfig for WickConfiguration {
  fn root_config(&self) -> Option<&RuntimeConfig> {
    match self {
      WickConfiguration::App(v) => v.root_config.as_ref(),
      WickConfiguration::Component(v) => v.root_config.as_ref(),
      WickConfiguration::Types(_) => None,
      WickConfiguration::Tests(_) => None,
      WickConfiguration::Lockdown(_) => None,
    }
  }

  fn set_root_config(&mut self, config: Option<RuntimeConfig>) {
    match self {
      WickConfiguration::App(v) => {
        v.root_config = config;
      }
      WickConfiguration::Component(v) => {
        v.root_config = config;
      }
      WickConfiguration::Types(_) => (),
      WickConfiguration::Tests(_) => (),
      WickConfiguration::Lockdown(_) => (),
    }
  }
}

#[derive(Debug, Clone, Copy)]
/// The kind of configuration loaded.
#[must_use]

pub enum ConfigurationKind {
  /// An [app_config::AppConfiguration] configuration.
  App,
  /// A [component_config::ComponentConfiguration] configuration.
  Component,
  /// A [types_config::TypesConfiguration] configuration.
  Types,
  /// A [test_config::TestConfiguration] configuration.
  Tests,
  /// A [lockdown_config::LockdownConfiguration] configuration.
  Lockdown,
}

impl std::fmt::Display for ConfigurationKind {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match self {
      ConfigurationKind::App => write!(f, "wick/app"),
      ConfigurationKind::Component => write!(f, "wick/component"),
      ConfigurationKind::Types => write!(f, "wick/types"),
      ConfigurationKind::Tests => write!(f, "wick/tests"),
      ConfigurationKind::Lockdown => write!(f, "wick/lockdown"),
    }
  }
}