Skip to main content

wasi_virt_layer_cli/
feature_extractor.rs

1use clap::ArgMatches;
2
3/// Extracts CLI arguments into internal feature options for VFS module generation and dependencies.
4
5pub fn extract_features(
6    matches: &ArgMatches,
7    wasm_count: usize,
8) -> (
9    crate::args::VfsBuildOptions,
10    Box<[crate::args::VfsBuildOptions]>,
11) {
12    let mut vfs_opts = crate::args::VfsBuildOptions::default();
13    let mut target_opts =
14        vec![crate::args::VfsBuildOptions::default(); wasm_count].into_boxed_slice();
15
16    let wasm_indices: Vec<usize> = matches
17        .indices_of("wasm")
18        .map(|i| i.collect())
19        .unwrap_or_default();
20
21    let features_indices: Vec<usize> = matches
22        .indices_of("features")
23        .map(|i| i.collect())
24        .unwrap_or_default();
25
26    let features_values: Vec<String> = matches
27        .get_many::<String>("features")
28        .map(|i| i.cloned().collect())
29        .unwrap_or_default();
30
31    let no_default_indices: Vec<usize> = if matches.get_count("no_default_features") > 0 {
32        matches
33            .indices_of("no_default_features")
34            .map(|i| i.collect())
35            .unwrap_or_default()
36    } else {
37        vec![]
38    };
39
40    let no_opt_indices: Vec<usize> = if matches.get_count("no_opt") > 0 {
41        matches
42            .indices_of("no_opt")
43            .map(|i| i.collect())
44            .unwrap_or_default()
45    } else {
46        vec![]
47    };
48
49    let no_opt_all_indices: Vec<usize> = if matches.get_count("no_opt_all") > 0 {
50        matches
51            .indices_of("no_opt_all")
52            .map(|i| i.collect())
53            .unwrap_or_default()
54    } else {
55        vec![]
56    };
57
58    // Distribute features
59    for (idx, val) in features_indices
60        .into_iter()
61        .zip(features_values.into_iter())
62    {
63        let mut target_wasm_idx = None;
64        for (w_i, w_idx) in wasm_indices.iter().enumerate() {
65            if *w_idx < idx {
66                target_wasm_idx = Some(w_i);
67            } else {
68                break;
69            }
70        }
71
72        if let Some(w_i) = target_wasm_idx {
73            target_opts[w_i].features.push(val);
74        } else {
75            vfs_opts.features.push(val);
76        }
77    }
78
79    // Distribute no_default_features
80    for idx in no_default_indices {
81        let mut target_wasm_idx = None;
82        for (w_i, w_idx) in wasm_indices.iter().enumerate() {
83            if *w_idx < idx {
84                target_wasm_idx = Some(w_i);
85            } else {
86                break;
87            }
88        }
89
90        if let Some(w_i) = target_wasm_idx {
91            target_opts[w_i].no_default_features += 1;
92        } else {
93            vfs_opts.no_default_features += 1;
94        }
95    }
96
97    // Distribute no_opt
98    for idx in no_opt_indices {
99        let mut target_wasm_idx = None;
100        for (w_i, w_idx) in wasm_indices.iter().enumerate() {
101            if *w_idx < idx {
102                target_wasm_idx = Some(w_i);
103            } else {
104                break;
105            }
106        }
107
108        if let Some(w_i) = target_wasm_idx {
109            target_opts[w_i].no_opt += 1;
110        } else {
111            vfs_opts.no_opt += 1;
112        }
113    }
114
115    // Distribute no_opt_all
116    for idx in no_opt_all_indices {
117        let mut target_wasm_idx = None;
118        for (w_i, w_idx) in wasm_indices.iter().enumerate() {
119            if *w_idx < idx {
120                target_wasm_idx = Some(w_i);
121            } else {
122                break;
123            }
124        }
125
126        if let Some(w_i) = target_wasm_idx {
127            target_opts[w_i].no_opt_all += 1;
128        } else {
129            vfs_opts.no_opt_all += 1;
130        }
131    }
132
133    (vfs_opts, target_opts)
134}