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
//! Module for component metadata representation in `Cargo.toml`.
use anyhow::{bail, Context, Result};
use cargo_component_core::registry::{Dependency, RegistryPackage};
use cargo_metadata::Package;
use semver::{Version, VersionReq};
use serde::{
de::{self, value::MapAccessDeserializer},
Deserialize,
};
use serde_json::from_value;
use std::{
borrow::Cow,
collections::HashMap,
path::{Path, PathBuf},
str::FromStr,
time::SystemTime,
};
use url::Url;
use warg_protocol::registry::PackageName;
/// The default directory to look for a target WIT file.
pub const DEFAULT_WIT_DIR: &str = "wit";
/// The supported ownership model for generated types.
#[derive(Default, Debug, Clone, Copy, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Ownership {
/// Generated types will be composed entirely of owning fields, regardless
/// of whether they are used as parameters to imports or not.
#[default]
Owning,
/// Generated types used as parameters to imports will be "deeply
/// borrowing", i.e. contain references rather than owned values when
/// applicable.
Borrowing,
/// Generate "duplicate" type definitions for a single WIT type, if necessary.
/// For example if it's used as both an import and an export, or if it's used
/// both as a parameter to an import and a return value from an import.
BorrowingDuplicateIfNecessary,
}
impl FromStr for Ownership {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"owning" => Ok(Self::Owning),
"borrowing" => Ok(Self::Borrowing),
"borrowing-duplicate-if-necessary" => Ok(Self::BorrowingDuplicateIfNecessary),
_ => Err(format!(
"unrecognized ownership: `{s}`; \
expected `owning`, `borrowing`, or `borrowing-duplicate-if-necessary`"
)),
}
}
}
/// Configuration for bindings generation.
#[derive(Default, Debug, Clone, Deserialize)]
#[serde(default)]
pub struct Bindings {
/// The path to the type that implements the target world.
///
/// If `None`, a type named `Component` will be used.
pub implementor: Option<String>,
/// A map of resource names to implementing types.
///
/// An example would be "foo:bar/baz/res" => "MyResource".
pub resources: HashMap<String, String>,
/// The ownership model for generated types.
pub ownership: Ownership,
/// Additional derives to apply to generated binding types.
pub derives: Vec<String>,
/// If true, code generation should qualify any features that depend on
/// `std` with `cfg(feature = "std")`.
pub std_feature: bool,
}
/// The target of a component.
///
/// The target defines the world of the component being developed.
#[derive(Debug, Clone)]
pub enum Target {
/// The target is a world from a registry package.
Package {
/// The name of the target package (e.g. `wasi:http`).
name: PackageName,
/// The registry package being targeted.
package: RegistryPackage,
/// The name of the world being targeted.
///
/// [Resolve::select_world][select-world] will be used
/// to select world.
///
/// [select-world]: https://docs.rs/wit-parser/latest/wit_parser/struct.Resolve.html#method.select_world
world: Option<String>,
},
/// The target is a world from a local wit document.
Local {
/// The path to the wit document defining the world.
///
/// Defaults to the `wit` directory.
path: Option<PathBuf>,
/// The name of the world being targeted.
///
/// [Resolve::select_world][select-world] will be used
/// to select world.
///
/// [select-world]: https://docs.rs/wit-parser/latest/wit_parser/struct.Resolve.html#method.select_world
world: Option<String>,
/// The dependencies of the wit document being targeted.
dependencies: HashMap<PackageName, Dependency>,
},
}
impl Target {
/// Gets the dependencies of the target.
pub fn dependencies(&self) -> Cow<HashMap<PackageName, Dependency>> {
match self {
Self::Package { name, package, .. } => Cow::Owned(HashMap::from_iter([(
name.clone(),
Dependency::Package(package.clone()),
)])),
Self::Local { dependencies, .. } => Cow::Borrowed(dependencies),
}
}
/// Gets the target world, if any.
pub fn world(&self) -> Option<&str> {
match self {
Self::Package { world, .. } | Self::Local { world, .. } => world.as_deref(),
}
}
}
impl Default for Target {
fn default() -> Self {
Self::Local {
path: None,
world: None,
dependencies: HashMap::new(),
}
}
}
impl FromStr for Target {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self> {
let (name, version) = match s.split_once('@') {
Some((name, version)) => (
name,
version
.parse()
.with_context(|| format!("invalid target version `{version}`"))?,
),
None => bail!("expected target format `<package-name>[/<world>]@<version>`"),
};
let (name, world) = match name.split_once('/') {
Some((name, world)) => {
wit_parser::validate_id(world)
.with_context(|| format!("invalid target world name `{world}`"))?;
(name, Some(world.to_string()))
}
None => (name, None),
};
Ok(Self::Package {
name: name.parse()?,
package: RegistryPackage {
name: None,
version,
registry: None,
},
world,
})
}
}
impl<'de> Deserialize<'de> for Target {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct Visitor;
impl<'de> de::Visitor<'de> for Visitor {
type Value = Target;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(formatter, "a string or a table")
}
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
Target::from_str(s).map_err(de::Error::custom)
}
fn visit_map<A>(self, map: A) -> Result<Self::Value, A::Error>
where
A: de::MapAccess<'de>,
{
#[derive(Default, Deserialize)]
#[serde(default, deny_unknown_fields)]
struct Entry {
package: Option<String>,
version: Option<VersionReq>,
world: Option<String>,
registry: Option<String>,
path: Option<PathBuf>,
dependencies: HashMap<PackageName, Dependency>,
}
let entry = Entry::deserialize(MapAccessDeserializer::new(map))?;
match (entry.path, entry.package) {
(None, Some(package)) => {
for (present, name) in [(!entry.dependencies.is_empty(), "dependencies")] {
if present {
return Err(de::Error::custom(
format!("cannot specify both `{name}` and `package` fields in a target entry"),
));
}
}
Ok(Target::Package {
name: package.parse().map_err(de::Error::custom)?,
package: RegistryPackage {
name: None,
version: entry
.version
.ok_or_else(|| de::Error::missing_field("version"))?,
registry: entry.registry,
},
world: entry.world,
})
}
(path, None) => {
for (present, name) in [
(entry.version.is_some(), "version"),
(entry.registry.is_some(), "registry"),
] {
if present {
return Err(de::Error::custom(
format!("cannot specify both `{name}` and `path` fields in a target entry"),
));
}
}
Ok(Target::Local {
path,
world: entry.world,
dependencies: entry.dependencies,
})
}
(Some(_), Some(_)) => Err(de::Error::custom(
"cannot specify both `path` and `package` fields in a target entry",
)),
}
}
}
deserializer.deserialize_any(Visitor)
}
}
/// Represents the `package.metadata.component` section in `Cargo.toml`.
#[derive(Default, Debug, Clone, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct ComponentSection {
/// The package name of the component, for publishing.
pub package: Option<PackageName>,
/// The world targeted by the component.
pub target: Target,
/// The path to the WASI adapter to use.
pub adapter: Option<PathBuf>,
/// The dependencies of the component.
pub dependencies: HashMap<PackageName, Dependency>,
/// The registries to use for the component.
pub registries: HashMap<String, Url>,
/// The configuration for bindings generation.
pub bindings: Bindings,
/// Whether to use the built-in `wasi:http/proxy` adapter for the component.
///
/// This should only be `true` when `adapter` is None.
pub proxy: bool,
}
/// Represents cargo metadata for a WebAssembly component.
#[derive(Debug, Clone)]
pub struct ComponentMetadata {
/// The crate name.
pub name: String,
/// The version of the crate.
pub version: Version,
/// The path to the cargo manifest file.
pub manifest_path: PathBuf,
/// The last modified time of the manifest file.
pub modified_at: SystemTime,
/// The component section in `Cargo.toml`.
pub section: ComponentSection,
}
impl ComponentMetadata {
/// Creates a new component metadata for the given cargo package.
///
/// Returns `Ok(None)` if the package does not have a `component` section.
pub fn from_package(package: &Package) -> Result<Option<Self>> {
log::debug!(
"searching for component metadata in manifest `{path}`",
path = package.manifest_path
);
let mut section: ComponentSection = match package.metadata.get("component").cloned() {
Some(component) => from_value(component).with_context(|| {
format!(
"failed to deserialize component metadata from `{path}`",
path = package.manifest_path
)
})?,
None => {
log::debug!(
"manifest `{path}` has no component metadata",
path = package.manifest_path
);
return Ok(None);
}
};
let manifest_dir = package
.manifest_path
.parent()
.map(|p| p.as_std_path())
.with_context(|| {
format!(
"manifest path `{path}` has no parent directory",
path = package.manifest_path
)
})?;
let modified_at = crate::last_modified_time(package.manifest_path.as_std_path())?;
// Make all paths stored in the metadata relative to the manifest directory.
if let Target::Local {
path, dependencies, ..
} = &mut section.target
{
if let Some(path) = path {
*path = manifest_dir.join(path.as_path());
}
for dependency in dependencies.values_mut() {
if let Dependency::Local(path) = dependency {
*path = manifest_dir.join(path.as_path());
}
}
}
for dependency in section.dependencies.values_mut() {
if let Dependency::Local(path) = dependency {
*path = manifest_dir.join(path.as_path());
}
}
if let Some(adapter) = section.adapter.as_mut() {
*adapter = manifest_dir.join(adapter.as_path());
}
Ok(Some(Self {
name: package.name.clone(),
version: package.version.clone(),
manifest_path: package.manifest_path.clone().into(),
modified_at,
section,
}))
}
/// Gets the path to a local target.
///
/// Returns `None` if the target is a registry package or
/// if a path is not specified and the default path does not exist.
pub fn target_path(&self) -> Option<Cow<Path>> {
match &self.section.target {
Target::Local {
path: Some(path), ..
} => Some(path.into()),
Target::Local { path: None, .. } => {
let path = self.manifest_path.parent().unwrap().join(DEFAULT_WIT_DIR);
if path.exists() {
Some(path.into())
} else {
None
}
}
Target::Package { .. } => None,
}
}
}