-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathmetadata.rs
More file actions
474 lines (436 loc) · 16.6 KB
/
metadata.rs
File metadata and controls
474 lines (436 loc) · 16.6 KB
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
//! Module for component metadata representation in `Cargo.toml`.
use std::{
borrow::Cow,
collections::HashMap,
path::{Path, PathBuf},
str::FromStr,
time::SystemTime,
};
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 url::Url;
use wasm_pkg_client::PackageRef;
/// 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(Debug, Clone, Deserialize)]
#[serde(default)]
pub struct Bindings {
/// The path where bindings will be generated (default to `src/bindings.rs`).
pub path: PathBuf,
/// Whether or not to run `rustfmt` on the bindings; defaults to true.
pub format: bool,
/// 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,
/// If true, code generation should pass borrowed string arguments as
/// `&[u8]` instead of `&str`. Strings are still required to be valid
/// UTF-8, but this avoids the need for Rust code to do its own UTF-8
/// validation if it doesn't already have a `&str`
pub raw_strings: bool,
/// Names of functions to skip generating bindings for.
pub skip: Vec<String>,
/// If true, generate stub implementations for any exported functions,
/// interfaces, and/or resources.
pub stubs: bool,
/// Optionally prefix any export names with the specified value.
///
/// This is useful to avoid name conflicts when testing.
pub export_prefix: Option<String>,
/// Remapping of interface names to rust module names.
pub with: HashMap<String, String>,
/// Add the specified suffix to the name of the custome section containing
/// the component type.
pub type_section_suffix: Option<String>,
/// Disable a workaround used to prevent libc ctors/dtors from being invoked
/// too much.
pub disable_run_ctors_once_workaround: bool,
/// Changes the default module used in the generated `export!` macro to
/// something other than `self`.
pub default_bindings_module: Option<String>,
/// Alternative name to use for the `export!` macro if one is generated.
pub export_macro_name: Option<String>,
/// Ensures that the `export!` macro will be defined as `pub` so it is a
/// candidate for being exported outside of the crate.
pub pub_export_macro: bool,
/// Whether to generate unused structures, not generated by default (false)
pub generate_unused_types: bool,
}
impl Default for Bindings {
fn default() -> Self {
Self {
path: PathBuf::from("src/bindings.rs"),
format: true,
ownership: Default::default(),
derives: Default::default(),
std_feature: false,
raw_strings: Default::default(),
skip: Default::default(),
stubs: Default::default(),
export_prefix: Default::default(),
with: Default::default(),
type_section_suffix: Default::default(),
disable_run_ctors_once_workaround: Default::default(),
default_bindings_module: Default::default(),
export_macro_name: Default::default(),
pub_export_macro: Default::default(),
generate_unused_types: Default::default(),
}
}
}
/// 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: PackageRef,
/// 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<PackageRef, Dependency>,
},
}
impl Target {
/// Gets the dependencies of the target.
pub fn dependencies(&self) -> Cow<HashMap<PackageRef, 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<PackageRef, 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<PackageRef>,
/// 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<PackageRef, 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,
/// Whether the component section was present in `Cargo.toml`.
pub section_present: bool,
}
impl ComponentMetadata {
/// Creates a new component metadata for the given cargo package.
pub fn from_package(package: &Package) -> Result<Self> {
log::debug!(
"searching for component metadata in manifest `{path}`",
path = package.manifest_path
);
let mut section_present = false;
let mut section: ComponentSection = match package.metadata.get("component").cloned() {
Some(component) => {
section_present = true;
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
);
Default::default()
}
};
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(Self {
name: package.name.clone(),
version: package.version.clone(),
manifest_path: package.manifest_path.clone().into(),
modified_at,
section,
section_present,
})
}
/// Gets the target package name.
///
/// Returns `None` if the target is not a registry package.
pub fn target_package(&self) -> Option<&PackageRef> {
match &self.section.target {
Target::Package { name, .. } => Some(name),
_ => None,
}
}
/// 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,
}
}
/// Gets the target world.
///
/// Returns `None` if there is no target world.
pub fn target_world(&self) -> Option<&str> {
self.section.target.world()
}
}