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
//! Returns the list of enable features when building your crate.
//!
//! The [`list_enabled_as_string`] function lists enabled features during the Cargo build process,
//! in a format that can be directly saved in build artifacts, which can be then included
//! elsewhere in the program and read at run time.
//!
//! If you don’t like the output format provided by `list_enabled_as_string`, the [`list_enabled`]
//! function will provide the list of enabled features as a `Vec<String>`.
//!
//! Other functions are made available in case you prefer obtaining intermediate data ([`list_all`])
//! or need a custom path to `Cargo.toml` ([`list_enabled_as_string_with_path`] and [`list_enabled_with_path`]),
//! but they’re probably not what you’re looking for.
//!
//! # Examples
//!
//! See the example included with the [`list_enabled_as_string`] function and the
//! [example crate](https://framagit.org/dder/list-features/-/tree/master/example_crate)
use HashSet;
use ;
use Write;
/// Returns the list of enabled features as a `Vec<String>`.
///
/// Reads from `std::env::vars` and filters the features based on those listed in `Cargo.toml`.
/// This function should only be called in build scripts or code executed during a Cargo build process, as
/// the required `CARGO_FEATURE_*` environment variables will be missing otherwise.
///
/// Unless its output format doesn’t suit you, you’ll probably want to use [`list_enabled_as_string`] instead.
///
/// See also [`list_enabled_with_path`].
///
/// # Panics
///
/// Panics if the `Cargo.toml` file cannot be read.
///
/// # Returns
///
/// A `Vec<String>` containing the names of the enabled features, ordered with `default` first and then sorted alphabetically.
/// Returns the list of enabled features as a `Vec<String>`.
///
/// Same as [`list_enabled`] but allows specifying a custom path to `Cargo.toml`.
///
/// # Panics
///
/// Panics if the specified file cannot be read.
///
/// # Arguments
///
/// * `cargo_toml_path` - Path to the `Cargo.toml` file
///
/// # Returns
///
/// A `Vec<String>` containing the names of the enabled features, ordered with `default` first and then sorted alphabetically.
/// Generates a constant declaration containing enabled Cargo features.
///
/// It’s a wrapper around [`list_enabled`] that provides a `String` that should be usable as is in an output file of the build script.
/// This function should only be called in build scripts or code executed during a Cargo build process, as
/// the required `CARGO_FEATURE_*` environment variables will be missing otherwise.
///
/// See also [`list_enabled_as_string_with_path`].
///
/// # Panics
///
/// Panics if the `Cargo.toml` file cannot be read.
///
/// # Arguments
///
/// * `const_name` - Name of the constant to generate.
///
/// # Returns
/// A `String` containing the code for the constant declaration, like:
/// ```
/// String::from(r#"pub const CONST_NAME: &[&str] = &[
/// "feature1",
/// "feature2",
/// ];"#);
/// ```
///
/// # Examples
///
/// ```ignore
/// // in build.rs
/// let out_dir = std::env::var("OUT_DIR").unwrap();
/// let file_path = format!("{out_dir}/build_info.rs");
/// let features = list_features::list_enabled_as_string("ENABLED_FEATURES");
/// std::fs::write(file_path, features).unwrap();
///
/// // in main.rs
/// include!(concat!(env!("OUT_DIR"), "/build_info.rs"));
/// for feature in ENABLED_FEATURES {
/// println!(output, " {feature}");
/// }
/// ```
/// Generates a constant declaration containing enabled Cargo features.
///
/// Same as [`list_enabled_as_string`] but allows specifying a custom path to `Cargo.toml`.
///
/// # Panics
///
/// Panics if the specified file cannot be read.
///
/// # Arguments
/// * `const_name` - Name of the constant to generate
/// * `cargo_toml_path` - Path to the `Cargo.toml` file
/// Parses a `Cargo.toml` file and returns the set of declared feature names.
///
/// Only the `[features]` section is considered. While it should be able handle reasonable edge cases, this function also tries to
/// keep things simple and is not a replacement for a full parser such as the [toml crate](https://crates.io/crates/toml).
///
/// # Arguments
///
/// * `cargo_toml_path` - Path to the `Cargo.toml` file used as the source for the available features list.
///
/// # Returns
///
/// A `HashSet<String>` containing the names of the declared features.
// Core parser logic that works on any line iterator.
// Returns the list of enabled features that are present in `all_features`.
//
// This reads from `std::env::vars` and filters against the provided set.
// It should only be called in build scripts or code executed during a Cargo build.