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
//! Data types and structure used when managing links on a wasmCloud lattice

use serde::{Deserialize, Serialize};

use crate::{
    ComponentId, KnownConfigName, LatticeTarget, LinkName, Result, WitInterface, WitNamespace,
    WitPackage,
};

/// A link definition between a source and target component (component or provider) on a given
/// interface.
///
/// An [`Link`] connects one component's import to another
/// component's export, specifying the configuration each component needs in order to execute
/// the request, and represents an operator's intent to allow the source to invoke the target.
///
/// This link definition is *distinct* from the one in `wasmcloud_core`, in that it is
/// represents a link at the point in time *before* it's configuration is fully resolved
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Eq, Serialize, Hash)]
#[non_exhaustive]
pub struct Link {
    /// Source identifier for the link
    pub(crate) source_id: ComponentId,
    /// Target for the link, which can be a unique identifier or (future) a routing group
    pub(crate) target: LatticeTarget,
    /// Name of the link. Not providing this is equivalent to specifying "default"
    #[serde(default = "default_link_name")]
    pub(crate) name: LinkName,
    /// WIT namespace of the link operation, e.g. `wasi` in `wasi:keyvalue/readwrite.get`
    pub(crate) wit_namespace: WitNamespace,
    /// WIT package of the link operation, e.g. `keyvalue` in `wasi:keyvalue/readwrite.get`
    pub(crate) wit_package: WitPackage,
    /// WIT Interfaces to be used for the link, e.g. `readwrite`, `atomic`, etc.
    pub(crate) interfaces: Vec<WitInterface>,
    /// List of named configurations to provide to the source upon request
    #[serde(default)]
    pub(crate) source_config: Vec<KnownConfigName>,
    /// List of named configurations to provide to the target upon request
    #[serde(default)]
    pub(crate) target_config: Vec<KnownConfigName>,
}

impl Link {
    #[must_use]
    pub fn source_id(&self) -> &str {
        &self.source_id
    }

    #[must_use]
    pub fn target(&self) -> &str {
        &self.target
    }

    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }

    #[must_use]
    pub fn wit_namespace(&self) -> &str {
        &self.wit_namespace
    }

    #[must_use]
    pub fn wit_package(&self) -> &str {
        &self.wit_package
    }

    #[must_use]
    pub fn interfaces(&self) -> &Vec<String> {
        &self.interfaces
    }

    #[must_use]
    pub fn source_config(&self) -> &Vec<String> {
        &self.source_config
    }

    #[must_use]
    pub fn target_config(&self) -> &Vec<String> {
        &self.target_config
    }

    #[must_use]
    pub fn builder() -> LinkBuilder {
        LinkBuilder::default()
    }
}

/// Builder that produces [`Link`]s
#[derive(Clone, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub struct LinkBuilder {
    source_id: Option<ComponentId>,
    target: Option<LatticeTarget>,
    name: Option<LinkName>,
    wit_namespace: Option<WitNamespace>,
    wit_package: Option<WitPackage>,
    interfaces: Option<Vec<WitInterface>>,
    source_config: Option<Vec<KnownConfigName>>,
    target_config: Option<Vec<KnownConfigName>>,
}

impl LinkBuilder {
    #[must_use]
    pub fn source_id(mut self, v: &str) -> Self {
        self.source_id = Some(v.into());
        self
    }

    #[must_use]
    pub fn target(mut self, v: &str) -> Self {
        self.target = Some(v.into());
        self
    }

    #[must_use]
    pub fn name(mut self, v: &str) -> Self {
        self.name = Some(v.into());
        self
    }

    #[must_use]
    pub fn wit_namespace(mut self, v: &str) -> Self {
        self.wit_namespace = Some(v.into());
        self
    }

    #[must_use]
    pub fn wit_package(mut self, v: &str) -> Self {
        self.wit_package = Some(v.into());
        self
    }

    #[must_use]
    pub fn interfaces(mut self, v: Vec<String>) -> Self {
        self.interfaces = Some(v);
        self
    }

    #[must_use]
    pub fn source_config(mut self, v: Vec<String>) -> Self {
        self.source_config = Some(v);
        self
    }

    #[must_use]
    pub fn target_config(mut self, v: Vec<String>) -> Self {
        self.target_config = Some(v);
        self
    }

    pub fn build(self) -> Result<Link> {
        Ok(Link {
            source_id: self
                .source_id
                .ok_or_else(|| "source id is required for creating links".to_string())?,
            target: self
                .target
                .ok_or_else(|| "target is required for creating links".to_string())?,
            name: self
                .name
                .ok_or_else(|| "name is required for creating links".to_string())?,
            wit_namespace: self
                .wit_namespace
                .ok_or_else(|| "WIT namespace is required for creating links".to_string())?,
            wit_package: self
                .wit_package
                .ok_or_else(|| "WIT package is required for creating links".to_string())?,
            interfaces: self.interfaces.unwrap_or_default(),
            source_config: self.source_config.unwrap_or_default(),
            target_config: self.target_config.unwrap_or_default(),
        })
    }
}

/// Helper function to provide a default link name
pub(crate) fn default_link_name() -> LinkName {
    "default".to_string()
}

#[cfg(test)]
mod tests {

    use super::Link;

    #[test]
    fn link_builder() {
        assert_eq!(
            Link {
                source_id: "source_id".into(),
                target: "target".into(),
                name: "name".into(),
                wit_namespace: "wit_namespace".into(),
                wit_package: "wit_package".into(),
                interfaces: vec!["i".into()],
                source_config: vec!["sc".into()],
                target_config: vec!["tc".into()]
            },
            Link::builder()
                .source_id("source_id")
                .target("target")
                .name("name")
                .wit_namespace("wit_namespace")
                .wit_package("wit_package")
                .interfaces(vec!["i".into()])
                .source_config(vec!["sc".into()])
                .target_config(vec!["tc".into()])
                .build()
                .unwrap()
        );
    }
}