wick_config/config/
types_config.rs

1#![allow(missing_docs)] // delete when we move away from the `property` crate.
2use std::collections::HashMap;
3use std::path::{Path, PathBuf};
4
5use asset_container::{AssetManager, Assets};
6use wick_asset_reference::AssetReference;
7use wick_interface_types::{OperationSignatures, TypeDefinition};
8use wick_packet::RuntimeConfig;
9
10use super::common::package_definition::PackageConfig;
11use super::components::ComponentConfig;
12use super::template_config::Renderable;
13use super::OperationDefinition;
14use crate::config;
15use crate::error::ManifestError;
16
17#[derive(
18  Debug, Clone, derive_builder::Builder, derive_asset_container::AssetManager, property::Property, serde::Serialize,
19)]
20#[property(get(public), set(public), mut(public, suffix = "_mut"))]
21#[asset(asset(AssetReference))]
22#[must_use]
23/// A Wick types configuration.
24///
25/// A types configuration is a collection of shareable types and operation signatures used to generated
26/// code for components and other types.
27pub struct TypesConfiguration {
28  /// The name of the types configuration.
29  #[asset(skip)]
30  #[builder(setter(strip_option), default)]
31  #[serde(skip_serializing_if = "Option::is_none")]
32  pub(crate) name: Option<String>,
33
34  /// The source (i.e. url or file on disk) of the configuration.
35  #[asset(skip)]
36  #[property(skip)]
37  #[serde(skip_serializing_if = "Option::is_none")]
38  pub(crate) source: Option<PathBuf>,
39
40  /// Any metadata associated with the configuration.
41  #[asset(skip)]
42  #[builder(default)]
43  #[serde(skip_serializing_if = "Option::is_none")]
44  pub(crate) metadata: Option<config::Metadata>,
45
46  /// A list of types defined in this configuration.
47  #[asset(skip)]
48  #[serde(skip_serializing_if = "Vec::is_empty")]
49  pub(crate) types: Vec<TypeDefinition>,
50
51  /// A list of operation signatures defined in this configuration.
52  #[asset(skip)]
53  #[property(skip)]
54  #[serde(skip_serializing_if = "Vec::is_empty")]
55  pub(crate) operations: Vec<OperationDefinition>,
56
57  /// The package configuration for this configuration.
58  #[builder(default)]
59  #[serde(skip_serializing_if = "Option::is_none")]
60  pub(crate) package: Option<PackageConfig>,
61}
62
63impl TypesConfiguration {
64  /// Get a type by name
65  #[must_use]
66  pub fn get_type(&self, name: &str) -> Option<&TypeDefinition> {
67    self.types.iter().find(|t| t.name() == name)
68  }
69
70  /// Return the version of the application.
71  #[must_use]
72  pub fn version(&self) -> Option<&str> {
73    self.metadata.as_ref().map(|m| m.version.as_str())
74  }
75
76  /// Get the package files
77  #[must_use]
78  pub fn package_files(&self) -> Option<Assets<AssetReference>> {
79    // should return empty vec if package is None
80    self.package.as_ref().map(|p| p.assets())
81  }
82
83  /// Set the source location of the configuration.
84  pub fn set_source(&mut self, source: &Path) {
85    let source = source.to_path_buf();
86    self.source = Some(source);
87  }
88
89  pub(super) fn update_baseurls(&self) {
90    #[allow(clippy::expect_used)]
91    let mut source = self.source.clone().expect("No source set for this configuration");
92    // Source is (should be) a file, so pop the filename before setting the baseurl.
93    if !source.is_dir() {
94      source.pop();
95    }
96    self.set_baseurl(&source);
97  }
98
99  /// Return the environment variables for this configuration.
100  #[must_use]
101  pub const fn env(&self) -> Option<&HashMap<String, String>> {
102    None
103  }
104
105  /// Validate this configuration is good.
106  #[allow(clippy::missing_const_for_fn)]
107  pub fn validate(&self) -> Result<(), ManifestError> {
108    /* placeholder */
109    Ok(())
110  }
111}
112
113impl OperationSignatures for TypesConfiguration {
114  fn operation_signatures(&self) -> Vec<wick_interface_types::OperationSignature> {
115    self.operations.clone().into_iter().map(Into::into).collect()
116  }
117}
118
119impl ComponentConfig for TypesConfiguration {
120  type Operation = OperationDefinition;
121
122  fn operations(&self) -> &[Self::Operation] {
123    &self.operations
124  }
125
126  fn operations_mut(&mut self) -> &mut Vec<Self::Operation> {
127    &mut self.operations
128  }
129}
130
131impl Renderable for TypesConfiguration {
132  fn render_config(
133    &mut self,
134    _source: Option<&Path>,
135    _root_config: Option<&RuntimeConfig>,
136    _env: Option<&HashMap<String, String>>,
137  ) -> Result<(), ManifestError> {
138    Ok(())
139  }
140}