Struct browserslist::Distrib

source ·
pub struct Distrib(_, _);
Expand description

Representation of browser name (or node) and its version.

When converting it to string, it will be formatted as the output of browserslist. For example:

use browserslist::{Opts, resolve};

let distrib = &resolve(["firefox 93"], &Opts::new()).unwrap()[0];

assert_eq!(distrib.name(), "firefox");
assert_eq!(distrib.version(), "93");

Implementations§

Return browser name, or node.

use browserslist::{Opts, resolve};

let distrib = &resolve(["firefox 93"], &Opts::new()).unwrap()[0];

assert_eq!(distrib.name(), "firefox");
Examples found in repository?
src/lib.rs (line 143)
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
pub fn resolve<I, S>(queries: I, opts: &Opts) -> Result<Vec<Distrib>, Error>
where
    S: AsRef<str>,
    I: IntoIterator<Item = S>,
{
    let query = queries
        .into_iter()
        .enumerate()
        .fold(String::new(), |mut s, (i, query)| {
            if i > 0 {
                s.push_str(", ");
            }
            s.push_str(query.as_ref());
            s
        });

    let mut distribs = parse_browserslist_query(&query)?
        .1
        .into_iter()
        .enumerate()
        .try_fold(vec![], |mut distribs, (i, current)| {
            if i == 0 && current.negated {
                return Err(Error::NotAtFirst(current.raw.to_string()));
            }

            let mut dist = queries::query(current.atom, opts)?;
            if current.negated {
                distribs.retain(|distrib| !dist.contains(distrib));
            } else if current.is_and {
                distribs.retain(|distrib| dist.contains(distrib));
            } else {
                distribs.append(&mut dist);
            }

            Ok::<_, Error>(distribs)
        })?;

    distribs.sort_by(|a, b| match a.name().cmp(b.name()) {
        Ordering::Equal => {
            let version_a = a.version().split('-').next().unwrap();
            let version_b = b.version().split('-').next().unwrap();
            version_b
                .parse::<semver::Version>()
                .unwrap_or_default()
                .cmp(&version_a.parse().unwrap_or_default())
        }
        ord => ord,
    });
    distribs.dedup();

    Ok(distribs)
}

Return version string.

use browserslist::{Opts, resolve};

let distrib = &resolve(["firefox 93"], &Opts::new()).unwrap()[0];

assert_eq!(distrib.version(), "93");
Examples found in repository?
src/lib.rs (line 145)
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
pub fn resolve<I, S>(queries: I, opts: &Opts) -> Result<Vec<Distrib>, Error>
where
    S: AsRef<str>,
    I: IntoIterator<Item = S>,
{
    let query = queries
        .into_iter()
        .enumerate()
        .fold(String::new(), |mut s, (i, query)| {
            if i > 0 {
                s.push_str(", ");
            }
            s.push_str(query.as_ref());
            s
        });

    let mut distribs = parse_browserslist_query(&query)?
        .1
        .into_iter()
        .enumerate()
        .try_fold(vec![], |mut distribs, (i, current)| {
            if i == 0 && current.negated {
                return Err(Error::NotAtFirst(current.raw.to_string()));
            }

            let mut dist = queries::query(current.atom, opts)?;
            if current.negated {
                distribs.retain(|distrib| !dist.contains(distrib));
            } else if current.is_and {
                distribs.retain(|distrib| dist.contains(distrib));
            } else {
                distribs.append(&mut dist);
            }

            Ok::<_, Error>(distribs)
        })?;

    distribs.sort_by(|a, b| match a.name().cmp(b.name()) {
        Ordering::Equal => {
            let version_a = a.version().split('-').next().unwrap();
            let version_b = b.version().split('-').next().unwrap();
            version_b
                .parse::<semver::Version>()
                .unwrap_or_default()
                .cmp(&version_a.parse().unwrap_or_default())
        }
        ord => ord,
    });
    distribs.dedup();

    Ok(distribs)
}

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Deserialize this value from the given Serde deserializer. Read more
Formats the value using the given formatter. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.