pep508_rs 0.9.2

A library for python dependency specifiers, better known as PEP 508
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
use std::str::FromStr;

use pep440_rs::{Version, VersionPattern, VersionSpecifier};

use crate::cursor::Cursor;
use crate::marker::MarkerValueExtra;
use crate::{
    ExtraName, ExtraOperator, MarkerExpression, MarkerOperator, MarkerTree, MarkerValue,
    MarkerValueVersion, MarkerWarningKind, Pep508Error, Pep508ErrorSource, Pep508Url, Reporter,
};

/// ```text
/// version_cmp   = wsp* <'<=' | '<' | '!=' | '==' | '>=' | '>' | '~=' | '==='>
/// marker_op     = version_cmp | (wsp* 'in') | (wsp* 'not' wsp+ 'in')
/// ```
/// The `wsp*` has already been consumed by the caller.
fn parse_marker_operator<T: Pep508Url>(
    cursor: &mut Cursor,
) -> Result<MarkerOperator, Pep508Error<T>> {
    let (start, len) = if cursor.peek_char().is_some_and(char::is_alphabetic) {
        // "in" or "not"
        cursor.take_while(|char| !char.is_whitespace() && char != '\'' && char != '"')
    } else {
        // A mathematical operator
        cursor.take_while(|char| matches!(char, '<' | '=' | '>' | '~' | '!'))
    };
    let operator = cursor.slice(start, len);
    if operator == "not" {
        // 'not' wsp+ 'in'
        match cursor.next() {
            None => {
                return Err(Pep508Error {
                    message: Pep508ErrorSource::String(
                        "Expected whitespace after 'not', found end of input".to_string(),
                    ),
                    start: cursor.pos(),
                    len: 1,
                    input: cursor.to_string(),
                });
            }
            Some((_, whitespace)) if whitespace.is_whitespace() => {}
            Some((pos, other)) => {
                return Err(Pep508Error {
                    message: Pep508ErrorSource::String(format!(
                        "Expected whitespace after `not`, found `{other}`"
                    )),
                    start: pos,
                    len: other.len_utf8(),
                    input: cursor.to_string(),
                });
            }
        };
        cursor.eat_whitespace();
        cursor.next_expect_char('i', cursor.pos())?;
        cursor.next_expect_char('n', cursor.pos())?;
        return Ok(MarkerOperator::NotIn);
    }
    MarkerOperator::from_str(operator).map_err(|_| Pep508Error {
        message: Pep508ErrorSource::String(format!(
            "Expected a valid marker operator (such as `>=` or `not in`), found `{operator}`"
        )),
        start,
        len,
        input: cursor.to_string(),
    })
}

/// Either a single or double quoted string or one of '`python_version`', '`python_full_version`',
/// '`os_name`', '`sys_platform`', '`platform_release`', '`platform_system`', '`platform_version`',
/// '`platform_machine`', '`platform_python_implementation`', '`implementation_name`',
/// '`implementation_version`', 'extra'
pub(crate) fn parse_marker_value<T: Pep508Url>(
    cursor: &mut Cursor,
) -> Result<MarkerValue, Pep508Error<T>> {
    // > User supplied constants are always encoded as strings with either ' or " quote marks. Note
    // > that backslash escapes are not defined, but existing implementations do support them. They
    // > are not included in this specification because they add complexity and there is no observable
    // > need for them today. Similarly we do not define non-ASCII character support: all the runtime
    // > variables we are referencing are expected to be ASCII-only.
    match cursor.peek() {
        None => Err(Pep508Error {
            message: Pep508ErrorSource::String(
                "Expected marker value, found end of dependency specification".to_string(),
            ),
            start: cursor.pos(),
            len: 1,
            input: cursor.to_string(),
        }),
        // It can be a string ...
        Some((start_pos, quotation_mark @ ('"' | '\''))) => {
            cursor.next();
            let (start, len) = cursor.take_while(|c| c != quotation_mark);
            let value = cursor.slice(start, len).to_string();
            cursor.next_expect_char(quotation_mark, start_pos)?;
            Ok(MarkerValue::QuotedString(value))
        }
        // ... or it can be a keyword
        Some(_) => {
            let (start, len) = cursor.take_while(|char| {
                !char.is_whitespace() && !['>', '=', '<', '!', '~', ')'].contains(&char)
            });
            let key = cursor.slice(start, len);
            MarkerValue::from_str(key).map_err(|_| Pep508Error {
                message: Pep508ErrorSource::String(format!(
                    "Expected a quoted string or a valid marker name, found `{key}`"
                )),
                start,
                len,
                input: cursor.to_string(),
            })
        }
    }
}

/// ```text
/// marker_var:l marker_op:o marker_var:r
/// ```
pub(crate) fn parse_marker_key_op_value<T: Pep508Url>(
    cursor: &mut Cursor,
    reporter: &mut impl Reporter,
) -> Result<Option<MarkerExpression>, Pep508Error<T>> {
    cursor.eat_whitespace();
    let l_value = parse_marker_value(cursor)?;
    cursor.eat_whitespace();
    // "not in" and "in" must be preceded by whitespace. We must already have matched a whitespace
    // when we're here because other `parse_marker_key` would have pulled the characters in and
    // errored
    let operator = parse_marker_operator(cursor)?;
    cursor.eat_whitespace();
    let r_value = parse_marker_value(cursor)?;

    // Convert a `<marker_value> <marker_op> <marker_value>` expression into its
    // typed equivalent.
    let expr = match l_value {
        // Either:
        // - `<version key> <version op> <quoted PEP 440 version>`
        // - `<version key> in <list of quoted PEP 440 versions>` and ("not in")
        MarkerValue::MarkerEnvVersion(key) => {
            let MarkerValue::QuotedString(value) = r_value else {
                reporter.report(
                    MarkerWarningKind::Pep440Error,
                    format!(
                        "Expected double quoted PEP 440 version to compare with {key},
                        found {r_value}, will be ignored"
                    ),
                );

                return Ok(None);
            };

            // Check for `in` and `not in` expressions
            if let Some(expr) = parse_version_in_expr(key.clone(), operator, &value, reporter) {
                return Ok(Some(expr));
            }

            // Otherwise, it's a normal version expression
            parse_version_expr(key.clone(), operator, &value, reporter)
        }
        // The only sound choice for this is `<env key> <op> <string>`
        MarkerValue::MarkerEnvString(key) => {
            let value = match r_value {
                MarkerValue::Extra
                | MarkerValue::MarkerEnvVersion(_)
                | MarkerValue::MarkerEnvString(_) => {
                    reporter.report(
                        MarkerWarningKind::MarkerMarkerComparison,
                        "Comparing two markers with each other doesn't make any sense,
                            will be ignored"
                            .to_string(),
                    );

                    return Ok(None);
                }
                MarkerValue::QuotedString(r_string) => r_string,
            };

            if operator == MarkerOperator::TildeEqual {
                reporter.report(
                    MarkerWarningKind::LexicographicComparison,
                    "Can't compare strings with `~=`, will be ignored".to_string(),
                );

                return Ok(None);
            }

            Some(MarkerExpression::String {
                key,
                operator,
                value,
            })
        }
        // `extra == '...'`
        MarkerValue::Extra => {
            let value = match r_value {
                MarkerValue::MarkerEnvVersion(_)
                | MarkerValue::MarkerEnvString(_)
                | MarkerValue::Extra => {
                    reporter.report(
                        MarkerWarningKind::ExtraInvalidComparison,
                        "Comparing extra with something other than a quoted string is wrong,
                            will be ignored"
                            .to_string(),
                    );

                    return Ok(None);
                }
                MarkerValue::QuotedString(value) => value,
            };

            parse_extra_expr(operator, &value, reporter)
        }
        // This is either MarkerEnvVersion, MarkerEnvString or Extra inverted
        MarkerValue::QuotedString(l_string) => {
            match r_value {
                // The only sound choice for this is `<quoted PEP 440 version> <version op>` <version key>
                MarkerValue::MarkerEnvVersion(key) => {
                    parse_inverted_version_expr(&l_string, operator, key.clone(), reporter)
                }
                // '...' == <env key>
                MarkerValue::MarkerEnvString(key) => Some(MarkerExpression::String {
                    key,
                    // Invert the operator to normalize the expression order.
                    operator: operator.invert(),
                    value: l_string,
                }),
                // `'...' == extra`
                MarkerValue::Extra => parse_extra_expr(operator, &l_string, reporter),
                // `'...' == '...'`, doesn't make much sense
                MarkerValue::QuotedString(_) => {
                    // Not even pypa/packaging 22.0 supports this
                    // https://github.com/pypa/packaging/issues/632
                    reporter.report(
                        MarkerWarningKind::StringStringComparison,
                        format!(
                            "Comparing two quoted strings with each other doesn't make sense:
                            '{l_string}' {operator} {r_value}, will be ignored"
                        ),
                    );

                    None
                }
            }
        }
    };

    Ok(expr)
}

/// Creates an instance of [`MarkerExpression::VersionIn`] with the given values.
///
/// Some important caveats apply here.
///
/// While the specification defines this operation as a substring search, for versions, we use a
/// version-aware match so we can perform algebra on the expressions. This means that some markers
/// will not be evaluated according to the specification, but these marker expressions are
/// relatively rare so the trade-off is acceptable.
///
/// The following limited expression is supported:
///
/// ```text
/// [not] in '<version> [additional versions]'
/// ```
///
/// where the version is PEP 440 compliant. Arbitrary whitespace is allowed between versions.
///
/// Returns `None` if the [`MarkerOperator`] is not relevant.
/// Reports a warning if an invalid version is encountered, and returns `None`.
fn parse_version_in_expr(
    key: MarkerValueVersion,
    operator: MarkerOperator,
    value: &str,
    reporter: &mut impl Reporter,
) -> Option<MarkerExpression> {
    if !matches!(operator, MarkerOperator::In | MarkerOperator::NotIn) {
        return None;
    }
    let negated = matches!(operator, MarkerOperator::NotIn);

    let mut cursor = Cursor::new(value);
    let mut versions = Vec::new();

    // Parse all of the values in the list as versions
    loop {
        // Allow arbitrary whitespace between versions
        cursor.eat_whitespace();

        let (start, len) = cursor.take_while(|c| !c.is_whitespace());
        if len == 0 {
            break;
        }

        let version = match Version::from_str(cursor.slice(start, len)) {
            Ok(version) => version,
            Err(err) => {
                reporter.report(
                    MarkerWarningKind::Pep440Error,
                    format!(
                        "Expected PEP 440 versions to compare with {key}, found {value},
                        will be ignored: {err}"
                    ),
                );

                return None;
            }
        };

        versions.push(version);
    }

    Some(MarkerExpression::VersionIn {
        key,
        versions,
        negated,
    })
}

/// Creates an instance of [`MarkerExpression::Version`] with the given values.
///
/// Reports a warning on failure, and returns `None`.
fn parse_version_expr(
    key: MarkerValueVersion,
    marker_operator: MarkerOperator,
    value: &str,
    reporter: &mut impl Reporter,
) -> Option<MarkerExpression> {
    let pattern = match value.parse::<VersionPattern>() {
        Ok(pattern) => pattern,
        Err(err) => {
            reporter.report(
                MarkerWarningKind::Pep440Error,
                format!(
                    "Expected PEP 440 version to compare with {key}, found {value},
                    will be ignored: {err}"
                ),
            );

            return None;
        }
    };

    let Some(operator) = marker_operator.to_pep440_operator() else {
        reporter.report(
            MarkerWarningKind::Pep440Error,
            format!(
                "Expected PEP 440 version operator to compare {key} with `{version}`,
                    found `{marker_operator}`, will be ignored",
                version = pattern.version()
            ),
        );

        return None;
    };

    let specifier = match VersionSpecifier::from_pattern(operator, pattern) {
        Ok(specifier) => specifier,
        Err(err) => {
            reporter.report(
                MarkerWarningKind::Pep440Error,
                format!("Invalid operator/version combination: {err}"),
            );
            return None;
        }
    };

    Some(MarkerExpression::Version { key, specifier })
}

/// Creates an instance of [`MarkerExpression::Version`] from an inverted expression.
///
/// Reports a warning on failure, and returns `None`.
fn parse_inverted_version_expr(
    value: &str,
    marker_operator: MarkerOperator,
    key: MarkerValueVersion,
    reporter: &mut impl Reporter,
) -> Option<MarkerExpression> {
    // Invert the operator to normalize the expression order.
    let marker_operator = marker_operator.invert();

    // Not star allowed here, `'3.*' == python_version` is not a valid PEP 440 comparison.
    let version = match value.parse::<Version>() {
        Ok(version) => version,
        Err(err) => {
            reporter.report(
                MarkerWarningKind::Pep440Error,
                format!(
                    "Expected PEP 440 version to compare with {key}, found {value},
                    will be ignored: {err}"
                ),
            );

            return None;
        }
    };

    let Some(operator) = marker_operator.to_pep440_operator() else {
        reporter.report(
            MarkerWarningKind::Pep440Error,
            format!(
                "Expected PEP 440 version operator to compare {key} with `{version}`,
                    found `{marker_operator}`, will be ignored"
            ),
        );

        return None;
    };

    let specifier = match VersionSpecifier::from_version(operator, version) {
        Ok(specifier) => specifier,
        Err(err) => {
            reporter.report(
                MarkerWarningKind::Pep440Error,
                format!("Invalid operator/version combination: {err}"),
            );
            return None;
        }
    };

    Some(MarkerExpression::Version { key, specifier })
}

/// Creates an instance of [`MarkerExpression::Extra`] with the given values, falling back to
/// [`MarkerExpression::Arbitrary`] on failure.
fn parse_extra_expr(
    operator: MarkerOperator,
    value: &str,
    reporter: &mut impl Reporter,
) -> Option<MarkerExpression> {
    let name = match ExtraName::from_str(value) {
        Ok(name) => MarkerValueExtra::Extra(name),
        Err(err) => {
            reporter.report(
                MarkerWarningKind::ExtraInvalidComparison,
                format!("Expected extra name (found `{value}`): {err}"),
            );
            MarkerValueExtra::Arbitrary(value.to_string())
        }
    };

    if let Some(operator) = ExtraOperator::from_marker_operator(operator) {
        return Some(MarkerExpression::Extra { operator, name });
    }

    reporter.report(
        MarkerWarningKind::ExtraInvalidComparison,
        "Comparing extra with something other than a quoted string is wrong,
        will be ignored"
            .to_string(),
    );

    None
}

/// ```text
/// marker_expr   = marker_var:l marker_op:o marker_var:r -> (o, l, r)
///               | wsp* '(' marker:m wsp* ')' -> m
/// ```
fn parse_marker_expr<T: Pep508Url>(
    cursor: &mut Cursor,
    reporter: &mut impl Reporter,
) -> Result<Option<MarkerTree>, Pep508Error<T>> {
    cursor.eat_whitespace();
    if let Some(start_pos) = cursor.eat_char('(') {
        let marker = parse_marker_or(cursor, reporter)?;
        cursor.next_expect_char(')', start_pos)?;
        Ok(marker)
    } else {
        Ok(parse_marker_key_op_value(cursor, reporter)?.map(MarkerTree::expression))
    }
}

/// ```text
/// marker_and    = marker_expr:l wsp* 'and' marker_expr:r -> ('and', l, r)
///               | marker_expr:m -> m
/// ```
fn parse_marker_and<T: Pep508Url>(
    cursor: &mut Cursor,
    reporter: &mut impl Reporter,
) -> Result<Option<MarkerTree>, Pep508Error<T>> {
    parse_marker_op(cursor, "and", MarkerTree::and, parse_marker_expr, reporter)
}

/// ```text
/// marker_or     = marker_and:l wsp* 'or' marker_and:r -> ('or', l, r)
///                   | marker_and:m -> m
/// ```
fn parse_marker_or<T: Pep508Url>(
    cursor: &mut Cursor,
    reporter: &mut impl Reporter,
) -> Result<Option<MarkerTree>, Pep508Error<T>> {
    parse_marker_op(
        cursor,
        "or",
        MarkerTree::or,
        |cursor, reporter| parse_marker_and(cursor, reporter),
        reporter,
    )
}

/// Parses both `marker_and` and `marker_or`
#[allow(clippy::type_complexity)]
fn parse_marker_op<T: Pep508Url, R: Reporter>(
    cursor: &mut Cursor,
    op: &str,
    apply: fn(&mut MarkerTree, MarkerTree),
    parse_inner: fn(&mut Cursor, &mut R) -> Result<Option<MarkerTree>, Pep508Error<T>>,
    reporter: &mut R,
) -> Result<Option<MarkerTree>, Pep508Error<T>> {
    let mut tree = None;

    // marker_and or marker_expr
    let first_element = parse_inner(cursor, reporter)?;

    if let Some(expression) = first_element {
        match tree {
            Some(ref mut tree) => apply(tree, expression),
            None => tree = Some(expression),
        }
    }

    loop {
        // wsp*
        cursor.eat_whitespace();
        // ('or' marker_and) or ('and' marker_or)
        let (start, len) = cursor.peek_while(|c| !c.is_whitespace());
        match cursor.slice(start, len) {
            value if value == op => {
                cursor.take_while(|c| !c.is_whitespace());

                if let Some(expression) = parse_inner(cursor, reporter)? {
                    match tree {
                        Some(ref mut tree) => apply(tree, expression),
                        None => tree = Some(expression),
                    }
                }
            }
            _ => return Ok(tree),
        }
    }
}

/// ```text
/// marker        = marker_or^
/// ```
pub(crate) fn parse_markers_cursor<T: Pep508Url>(
    cursor: &mut Cursor,
    reporter: &mut impl Reporter,
) -> Result<Option<MarkerTree>, Pep508Error<T>> {
    let marker = parse_marker_or(cursor, reporter)?;
    cursor.eat_whitespace();
    if let Some((pos, unexpected)) = cursor.next() {
        // If we're here, both parse_marker_or and parse_marker_and returned because the next
        // character was neither "and" nor "or"
        return Err(Pep508Error {
            message: Pep508ErrorSource::String(format!(
                "Unexpected character '{unexpected}', expected 'and', 'or' or end of input"
            )),
            start: pos,
            len: cursor.remaining(),
            input: cursor.to_string(),
        });
    };

    Ok(marker)
}

/// Parses markers such as `python_version < '3.8'` or
/// `python_version == "3.10" and (sys_platform == "win32" or (os_name == "linux" and implementation_name == 'cpython'))`
pub(crate) fn parse_markers<T: Pep508Url>(
    markers: &str,
    reporter: &mut impl Reporter,
) -> Result<MarkerTree, Pep508Error<T>> {
    let mut chars = Cursor::new(markers);

    // If the tree consisted entirely of arbitrary expressions
    // that were ignored, it evaluates to true.
    parse_markers_cursor(&mut chars, reporter).map(|result| result.unwrap_or(MarkerTree::TRUE))
}