feature-manifest 0.7.6

Document, validate, and render Cargo feature metadata.
Documentation
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
use std::collections::{BTreeMap, BTreeSet};
use std::fmt;
use std::path::PathBuf;
use std::str::FromStr;

use anyhow::{Result, bail};
use serde::{Deserialize, Serialize};

/// A workspace-aware view of one or more Cargo packages selected for analysis.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WorkspaceManifest {
    /// Root workspace manifest used as the base for relative output paths.
    pub root_manifest_path: PathBuf,
    /// Selected package manifests in deterministic display order.
    pub packages: Vec<FeatureManifest>,
}

impl WorkspaceManifest {
    /// Returns `true` when exactly one package was selected.
    pub fn is_single_package(&self) -> bool {
        self.packages.len() == 1
    }

    /// Returns the selected package names in display order.
    pub fn package_names(&self) -> Vec<&str> {
        self.packages
            .iter()
            .filter_map(|package| package.package_name.as_deref())
            .collect()
    }
}

/// A normalized view of Cargo features plus structured feature metadata.
#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct FeatureManifest {
    /// Path to the package manifest that was parsed.
    pub manifest_path: PathBuf,
    /// Cargo package name, when the manifest has a `[package]` section.
    pub package_name: Option<String>,
    /// Metadata table that was found, such as `feature-manifest`.
    pub metadata_table: Option<String>,
    /// Metadata layout used by the manifest.
    pub metadata_layout: MetadataLayout,
    /// Declared Cargo features keyed by feature name.
    pub features: BTreeMap<String, Feature>,
    /// Metadata entries that do not match a declared feature.
    pub metadata_only: BTreeMap<String, FeatureMetadata>,
    /// Raw typed members from the `default` feature set.
    pub default_members: Vec<FeatureRef>,
    /// Local feature names enabled by the `default` feature set.
    pub default_features: BTreeSet<String>,
    /// Feature groups declared in metadata.
    pub groups: Vec<FeatureGroup>,
    /// Dependencies known from the manifest or `cargo metadata`.
    pub dependencies: BTreeMap<String, DependencyInfo>,
    /// Manifest-defined lint level overrides.
    pub lint_overrides: BTreeMap<String, LintLevel>,
    /// Manifest-defined lint preset.
    pub lint_preset: Option<LintPreset>,
}

impl FeatureManifest {
    /// Returns the features in stable display order.
    pub fn ordered_features(&self) -> Vec<&Feature> {
        self.features.values().collect()
    }

    /// Returns the groups that contain the named feature.
    pub fn groups_for_feature(&self, feature_name: &str) -> Vec<&FeatureGroup> {
        self.groups
            .iter()
            .filter(|group| group.members.iter().any(|member| member == feature_name))
            .collect()
    }

    /// Returns the features that directly reference the named feature.
    pub fn reverse_dependencies(&self, feature_name: &str) -> Vec<&Feature> {
        self.features
            .values()
            .filter(|feature| {
                feature
                    .enables
                    .iter()
                    .any(|reference| reference.local_feature_name() == Some(feature_name))
            })
            .collect()
    }
}

/// A single Cargo feature and its associated metadata.
#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct Feature {
    /// Cargo feature name.
    pub name: String,
    /// Metadata associated with the feature, or defaults if missing.
    pub metadata: FeatureMetadata,
    /// Whether the feature had an explicit metadata entry.
    pub has_metadata: bool,
    /// Feature references activated by this feature.
    pub enables: Vec<FeatureRef>,
    /// Whether this feature is included in the default feature set.
    pub default_enabled: bool,
}

/// Dependency details relevant to feature validation.
#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct DependencyInfo {
    /// Dependency key used in `Cargo.toml`.
    pub key: String,
    /// Underlying package name after rename handling.
    pub package: String,
    /// Whether Cargo marks the dependency as optional.
    pub optional: bool,
}

/// Layout used for feature metadata inside `package.metadata`.
#[derive(Debug, Clone, Copy, Serialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum MetadataLayout {
    /// Feature metadata is stored directly under `[package.metadata.feature-manifest]`.
    Flat,
    /// Feature metadata is stored under `[package.metadata.feature-manifest.features]`.
    Structured,
}

impl fmt::Display for MetadataLayout {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Flat => formatter.write_str("flat"),
            Self::Structured => formatter.write_str("structured"),
        }
    }
}

impl FromStr for MetadataLayout {
    type Err = anyhow::Error;

    fn from_str(value: &str) -> Result<Self> {
        match value {
            "flat" => Ok(Self::Flat),
            "structured" => Ok(Self::Structured),
            _ => bail!("expected `flat` or `structured`, found `{value}`"),
        }
    }
}

/// Severity override policy for a specific lint code.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum LintLevel {
    /// Suppress the lint.
    Allow,
    /// Report the lint without failing validation.
    Warn,
    /// Report the lint as an error.
    Deny,
}

impl fmt::Display for LintLevel {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Allow => formatter.write_str("allow"),
            Self::Warn => formatter.write_str("warn"),
            Self::Deny => formatter.write_str("deny"),
        }
    }
}

impl FromStr for LintLevel {
    type Err = anyhow::Error;

    fn from_str(value: &str) -> Result<Self> {
        match value {
            "allow" => Ok(Self::Allow),
            "warn" | "warning" => Ok(Self::Warn),
            "deny" | "error" => Ok(Self::Deny),
            _ => bail!("expected `allow`, `warn`, or `deny`, found `{value}`"),
        }
    }
}

/// A named lint policy intended to make adoption and strict CI setup easier.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum LintPreset {
    /// Downgrade common rollout issues while adopting metadata.
    Adopt,
    /// Treat subjective warnings as release-blocking errors.
    Strict,
}

impl fmt::Display for LintPreset {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Adopt => formatter.write_str("adopt"),
            Self::Strict => formatter.write_str("strict"),
        }
    }
}

impl FromStr for LintPreset {
    type Err = anyhow::Error;

    fn from_str(value: &str) -> Result<Self> {
        match value {
            "adopt" => Ok(Self::Adopt),
            "strict" => Ok(Self::Strict),
            _ => bail!("expected `adopt` or `strict`, found `{value}`"),
        }
    }
}

/// A typed reference inside a feature definition.
#[derive(Debug, Clone, Serialize, PartialEq, Eq, PartialOrd, Ord)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum FeatureRef {
    /// Reference to another local Cargo feature.
    Feature {
        /// Referenced feature name.
        name: String,
    },
    /// Reference to an optional dependency using `dep:name`.
    Dependency {
        /// Dependency key.
        name: String,
    },
    /// Reference to a dependency feature such as `tokio/rt`.
    DependencyFeature {
        /// Dependency key.
        dependency: String,
        /// Dependency feature name.
        feature: String,
        /// Whether the reference uses Cargo's weak `name?/feature` syntax.
        weak: bool,
    },
    /// Reference syntax that could not be classified.
    Unknown {
        /// Original reference text.
        raw: String,
    },
}

impl FeatureRef {
    /// Parses a raw Cargo feature entry into a typed reference.
    pub fn parse(raw: &str) -> Self {
        if let Some(name) = raw.strip_prefix("dep:") {
            return Self::Dependency {
                name: name.to_owned(),
            };
        }

        if let Some((dependency, feature)) = raw.split_once("?/") {
            return Self::DependencyFeature {
                dependency: dependency.to_owned(),
                feature: feature.to_owned(),
                weak: true,
            };
        }

        if let Some((dependency, feature)) = raw.split_once('/') {
            return Self::DependencyFeature {
                dependency: dependency.to_owned(),
                feature: feature.to_owned(),
                weak: false,
            };
        }

        if raw.trim().is_empty() {
            return Self::Unknown {
                raw: raw.to_owned(),
            };
        }

        Self::Feature {
            name: raw.to_owned(),
        }
    }

    /// Returns the local feature name when this is a local feature reference.
    pub fn local_feature_name(&self) -> Option<&str> {
        match self {
            Self::Feature { name } => Some(name.as_str()),
            _ => None,
        }
    }

    /// Returns the dependency key for dependency-based references.
    pub fn dependency_name(&self) -> Option<&str> {
        match self {
            Self::Dependency { name } => Some(name.as_str()),
            Self::DependencyFeature { dependency, .. } => Some(dependency.as_str()),
            _ => None,
        }
    }

    /// Returns the reference in Cargo feature syntax.
    pub fn raw(&self) -> String {
        self.to_string()
    }
}

impl fmt::Display for FeatureRef {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Feature { name } => formatter.write_str(name),
            Self::Dependency { name } => write!(formatter, "dep:{name}"),
            Self::DependencyFeature {
                dependency,
                feature,
                weak,
            } => {
                if *weak {
                    write!(formatter, "{dependency}?/{feature}")
                } else {
                    write!(formatter, "{dependency}/{feature}")
                }
            }
            Self::Unknown { raw } => formatter.write_str(raw),
        }
    }
}

/// A logical grouping of related features.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct FeatureGroup {
    /// Group identifier shown in reports and generated output.
    pub name: String,
    /// Feature names that belong to the group.
    pub members: Vec<String>,
    #[serde(default)]
    /// Whether multiple default-enabled members should be reported as an error.
    pub mutually_exclusive: bool,
    /// Optional group description shown in generated output.
    pub description: Option<String>,
}

/// Additional author-provided metadata for a feature.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct FeatureMetadata {
    /// Description shown in generated docs.
    pub description: Option<String>,
    /// Optional family label such as `runtime`, `tls`, or `serialization`.
    pub category: Option<String>,
    /// Version or release label where the feature became available.
    pub since: Option<String>,
    /// URL for feature-specific documentation.
    pub docs: Option<String>,
    /// URL for an issue tracking unstable or planned work.
    pub tracking_issue: Option<String>,
    #[serde(default)]
    /// Prerequisite labels or related feature names.
    pub requires: Vec<String>,
    #[serde(default = "default_public")]
    /// Whether the feature should appear in public generated output.
    pub public: bool,
    #[serde(default)]
    /// Whether the feature is experimental.
    pub unstable: bool,
    #[serde(default)]
    /// Whether the feature is deprecated.
    pub deprecated: bool,
    #[serde(default)]
    /// Acknowledges intentional default enablement of private/deprecated/unstable features.
    pub allow_default: bool,
    /// Extra context appended to Markdown and explain output.
    pub note: Option<String>,
}

impl Default for FeatureMetadata {
    fn default() -> Self {
        Self {
            description: None,
            category: None,
            since: None,
            docs: None,
            tracking_issue: None,
            requires: Vec::new(),
            public: true,
            unstable: false,
            deprecated: false,
            allow_default: false,
            note: None,
        }
    }
}

impl FeatureMetadata {
    /// Returns a human-readable list of status labels for display output.
    pub fn status_labels(&self) -> Vec<&'static str> {
        let mut labels = Vec::new();
        if self.deprecated {
            labels.push("deprecated");
        }
        if self.unstable {
            labels.push("unstable");
        }
        if !self.public {
            labels.push("private");
        }
        if labels.is_empty() {
            labels.push("stable");
        }
        labels
    }
}

fn default_public() -> bool {
    true
}