wasm_tools/
wit.rs

1use anyhow::Result;
2use clap::Parser;
3use std::path::PathBuf;
4use wit_parser::{PackageId, Resolve};
5
6#[derive(Parser)]
7pub struct WitResolve {
8    /// Path to WIT files to load.
9    ///
10    /// This can be a directory containing `*.wit` files, a `*.wit` file itself,
11    /// or a `*.wasm` file which is a WIT package encoded as WebAssembly.
12    pub wit: PathBuf,
13
14    /// Features to enable when parsing the `wit` option.
15    ///
16    /// This flag enables the `@unstable` feature in WIT documents where the
17    /// items are otherwise hidden by default.
18    #[clap(long)]
19    pub features: Vec<String>,
20
21    /// Enable all features when parsing the `wit` option.
22    ///
23    /// This flag enables all `@unstable` features in WIT documents where the
24    /// items are otherwise hidden by default.
25    #[clap(long)]
26    pub all_features: bool,
27}
28
29impl WitResolve {
30    pub fn resolve_with_features(features: &[String], all_features: bool) -> Resolve {
31        let mut resolve = Resolve::default();
32        resolve.all_features = all_features;
33        for feature in features {
34            for f in feature.split_whitespace() {
35                for f in f.split(',').filter(|s| !s.is_empty()) {
36                    resolve.features.insert(f.to_string());
37                }
38            }
39        }
40        return resolve;
41    }
42
43    pub fn load(&self) -> Result<(Resolve, PackageId)> {
44        let mut resolve = Self::resolve_with_features(&self.features, self.all_features);
45        let (pkg_id, _) = resolve.push_path(&self.wit)?;
46        Ok((resolve, pkg_id))
47    }
48}