Skip to main content

wasi_virt_layer_cli/
feature_extractor.rs

1use clap::ArgMatches;
2
3pub fn extract_features(
4    matches: &ArgMatches,
5    wasm_count: usize,
6) -> (
7    crate::args::VfsBuildOptions,
8    Box<[crate::args::VfsBuildOptions]>,
9) {
10    let mut vfs_opts = crate::args::VfsBuildOptions::default();
11    let mut target_opts =
12        vec![crate::args::VfsBuildOptions::default(); wasm_count].into_boxed_slice();
13
14    let wasm_indices: Vec<usize> = matches
15        .indices_of("wasm")
16        .map(|i| i.collect())
17        .unwrap_or_default();
18
19    let features_indices: Vec<usize> = matches
20        .indices_of("features")
21        .map(|i| i.collect())
22        .unwrap_or_default();
23
24    let features_values: Vec<String> = matches
25        .get_many::<String>("features")
26        .map(|i| i.cloned().collect())
27        .unwrap_or_default();
28
29    let no_default_indices: Vec<usize> = matches
30        .indices_of("no_default_features")
31        .map(|i| i.collect())
32        .unwrap_or_default();
33
34    // Distribute features
35    for (idx, val) in features_indices
36        .into_iter()
37        .zip(features_values.into_iter())
38    {
39        let mut target_wasm_idx = None;
40        for (w_i, w_idx) in wasm_indices.iter().enumerate() {
41            if *w_idx < idx {
42                target_wasm_idx = Some(w_i);
43            } else {
44                break;
45            }
46        }
47
48        if let Some(w_i) = target_wasm_idx {
49            target_opts[w_i].features.push(val);
50        } else {
51            vfs_opts.features.push(val);
52        }
53    }
54
55    // Distribute no_default_features
56    for idx in no_default_indices {
57        let mut target_wasm_idx = None;
58        for (w_i, w_idx) in wasm_indices.iter().enumerate() {
59            if *w_idx < idx {
60                target_wasm_idx = Some(w_i);
61            } else {
62                break;
63            }
64        }
65
66        if let Some(w_i) = target_wasm_idx {
67            target_opts[w_i].no_default_features += 1;
68        } else {
69            vfs_opts.no_default_features += 1;
70        }
71    }
72
73    (vfs_opts, target_opts)
74}