Files
addr2line
adler
aho_corasick
arrayvec
atty
backtrace
bitflags
camino
cargo_metadata
cargo_nextest
cargo_platform
cfg_expr
cfg_if
chrono
clap
clap_derive
color_eyre
config
crossbeam_channel
crossbeam_deque
crossbeam_epoch
crossbeam_utils
ctrlc
datatest_stable
debug_ignore
duct
either
enable_ansi_support
env_logger
eyre
fixedbitset
gimli
guppy
guppy_workspace_hack
hashbrown
humantime
humantime_serde
indent_write
indenter
indexmap
is_ci
itertools
itoa
lazy_static
lexical_core
libc
log
memchr
memoffset
miniz_oxide
nested
nextest_metadata
nextest_runner
nix
nom
num_cpus
num_integer
num_traits
object
once_cell
os_pipe
os_str_bytes
owo_colors
pathdiff
petgraph
proc_macro2
proc_macro_error
proc_macro_error_attr
quick_junit
quick_xml
quote
rayon
rayon_core
regex
regex_syntax
rustc_demangle
ryu
same_file
scopeguard
semver
serde
serde_derive
serde_json
shared_child
shellwords
smallvec
static_assertions
strip_ansi_escapes
strsim
structopt
structopt_derive
supports_color
syn
target_lexicon
target_spec
termcolor
textwrap
time
toml
twox_hash
unicode_xid
utf8parse
vte
vte_generate_state_changes
walkdir
  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
// Copyright (c) The cargo-guppy Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0

//! A sorted, deduplicated list of features from a single package.

use crate::{
    graph::{feature::FeatureId, PackageMetadata},
    sorted_set::SortedSet,
    PackageId,
};
use std::{fmt, slice, vec};

/// A sorted, deduplicated list of features from a single package.
///
/// This provides a convenient way to query and print out lists of features.
///
/// Returned by methods on `FeatureSet`.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct FeatureList<'g> {
    package: PackageMetadata<'g>,
    features: SortedSet<&'g str>,
    has_base: bool,
}

impl<'g> FeatureList<'g> {
    /// Creates a new `FeatureList` from a package and an iterator over features.
    pub fn new(
        package: PackageMetadata<'g>,
        features: impl IntoIterator<Item = Option<&'g str>>,
    ) -> Self {
        let mut has_base = false;
        let features = features
            .into_iter()
            .filter_map(|feature| match feature {
                Some(feature) => Some(feature),
                None => {
                    has_base = true;
                    None
                }
            })
            .collect();
        Self {
            package,
            features,
            has_base,
        }
    }

    /// Returns the package corresponding to this feature list.
    pub fn package(&self) -> &PackageMetadata<'g> {
        &self.package
    }

    /// Returns true if this feature list contains this feature.
    pub fn contains(&self, feature: impl AsRef<str>) -> bool {
        self.features.contains(&feature.as_ref())
    }

    /// Returns true if this feature list contains the "base" feature.
    ///
    /// The "base" feature represents the package with no features enabled.
    pub fn has_base(&self) -> bool {
        self.has_base
    }

    /// Returns the list of features as a slice.
    ///
    /// This slice is guaranteed to be sorted and unique.
    pub fn features(&self) -> &[&'g str] {
        self.features.as_slice()
    }

    /// Returns a borrowed iterator over feature IDs.
    pub fn iter<'a>(&'a self) -> Iter<'g, 'a> {
        self.into_iter()
    }

    /// Returns a pretty-printer over the list of features.
    pub fn display_features<'a>(&'a self) -> DisplayFeatures<'g, 'a> {
        DisplayFeatures(self.features())
    }

    /// Returns a vector of feature names.
    ///
    /// The vector is guaranteed to be sorted and unique.
    pub fn into_features(self) -> Vec<&'g str> {
        self.features.into_inner().into_vec()
    }
}

impl<'g> IntoIterator for FeatureList<'g> {
    type Item = FeatureId<'g>;
    type IntoIter = IntoIter<'g>;

    fn into_iter(self) -> Self::IntoIter {
        IntoIter::new(self)
    }
}

impl<'a, 'g> IntoIterator for &'a FeatureList<'g> {
    type Item = FeatureId<'g>;
    type IntoIter = Iter<'g, 'a>;

    fn into_iter(self) -> Self::IntoIter {
        Iter::new(self)
    }
}

/// An owned iterator over a `FeatureList`.
pub struct IntoIter<'g> {
    package_id: &'g PackageId,
    has_base: bool,
    iter: vec::IntoIter<&'g str>,
}

impl<'g> IntoIter<'g> {
    /// Creates a new iterator.
    pub fn new(feature_list: FeatureList<'g>) -> Self {
        Self {
            package_id: feature_list.package.id(),
            has_base: feature_list.has_base,
            iter: feature_list.into_features().into_iter(),
        }
    }
}

impl<'g> Iterator for IntoIter<'g> {
    type Item = FeatureId<'g>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.has_base {
            self.has_base = false;
            return Some(FeatureId::base(self.package_id));
        }
        self.iter
            .next()
            .map(|feature| FeatureId::new(self.package_id, feature))
    }
}

/// A borrowed iterator over a `FeatureList`.
pub struct Iter<'g, 'a> {
    package_id: &'g PackageId,
    has_base: bool,
    iter: slice::Iter<'a, &'g str>,
}

impl<'g, 'a> Iter<'g, 'a> {
    /// Creates a new iterator.
    pub fn new(feature_list: &'a FeatureList<'g>) -> Self {
        Self {
            package_id: feature_list.package.id(),
            has_base: feature_list.has_base,
            iter: feature_list.features().iter(),
        }
    }
}

impl<'g, 'a> Iterator for Iter<'g, 'a> {
    type Item = FeatureId<'g>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.has_base {
            self.has_base = false;
            return Some(FeatureId::base(self.package_id));
        }
        self.iter
            .next()
            .map(|feature| FeatureId::new(self.package_id, feature))
    }
}

/// A pretty-printer for a list of features.
///
/// Returned by `FeatureList::display_filters`.
#[derive(Clone, Copy, Debug)]
pub struct DisplayFeatures<'g, 'a>(&'a [&'g str]);

impl<'g, 'a> fmt::Display for DisplayFeatures<'g, 'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0.join(", "))
    }
}