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
//! # pep508_parser
//! 
//! PEP-508 parser by creating a Parsing Expression Grammar (PEG) for PEP-508 strings
//! See: https://peps.python.org/pep-0508/

use pest::Parser;
use pest_derive::Parser;
use semver::VersionReq;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum ParserError {
    #[error(transparent)]
    InvalidInput(#[from] pest::error::Error<Rule>),

    #[error("invalid version spec")]
    InvalidVersionSpec(#[from] semver::Error),
}
#[derive(Debug, PartialEq)]
pub struct Dependency {
    /// Name of the package
    pub name: String,
    /// Version limits
    pub version: VersionReq,
    /// Optional extras that expand the dependencies of the distribution to enable optional features
    pub extras: Option<Vec<String>>,
    /// Optional URI specification
    pub uri: Option<String>,
    /// Optional markers
    pub markers: Option<Vec<String>>,
}

#[derive(Parser)]
#[grammar = "pep508.pest"]
struct Pep508Parser;

/// Parse a PEP-508 string and return a Dependency
pub fn parse(input: &str) -> Result<Dependency, ParserError> {
    let main = Pep508Parser::parse(Rule::main, input)?
        .next()
        .unwrap()
        .into_inner()
        .next()
        .unwrap();
    match main.as_rule() {
        Rule::name_req | Rule::url_req => {
            let (mut name, mut extras_list, mut versionspec, mut uri, mut markers) =
                ("", vec![], Some("*"), None, vec![]);
            main.into_inner().flatten().for_each(|p| match p.as_rule() {
                Rule::name => name = p.as_span().as_str(),
                Rule::versionspec => versionspec = Some(p.as_span().as_str()),
                Rule::extras_list => {
                    extras_list = p
                        .as_span()
                        .as_str()
                        .replace(" ", "")
                        .split(",")
                        .map(str::to_string)
                        .collect()
                }
                Rule::marker_expr => markers.push(p.as_span().as_str().to_string()),
                Rule::URI_reference => uri = Some(p.as_span().as_str().to_string()),
                _ => {}
            });
            Ok(Dependency {
                name: name.to_string(),
                version: VersionReq::parse(versionspec.unwrap())?,
                extras: Some(extras_list.clone()),
                markers: Some(markers.clone()),
                uri: uri.clone(),
            })
        }
        _ => unreachable!("grammar is inconsistent with the parser"),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rstest::rstest;

    #[rstest]
    #[case("A")]
    #[case("A.B-C_D")]
    #[case("aa")]
    #[case("name")]
    #[case("name<=1")]
    #[case("name>=3")]
    #[case("name>=3,<2")]
    #[case("name@http://foo.com")]
    #[case("name [fred,bar] @ http://foo.com ; python_version=='2.7'")]
    #[case("name[quux, strange];python_version<'2.7' and platform_version=='2'")]
    #[case("name; os_name=='a' or os_name=='b'")]
    #[case("name; os_name=='a' and os_name=='b' or os_name=='c'")]
    #[case("name; os_name=='a' and (os_name=='b' or os_name=='c')")]
    #[case("name; os_name=='a' or os_name=='b' and os_name=='c'")]
    #[case("name; (os_name=='a' or os_name=='b') and os_name=='c'")]
    #[case("name[quux, strange];python_version<'2.7' and platform_version=='2'")]
    fn test_main(#[case] input: &str) {
        Pep508Parser::parse(Rule::main, input).unwrap();
    }

    #[test]
    fn test_parse_extras() {
        let parsed =
            parse("name[quux, strange];python_version<'2.7' and platform_version=='2'").unwrap();
        assert_eq!(
            parsed,
            Dependency {
                name: String::from("name"),
                version: VersionReq::parse("*").unwrap(),
                extras: Some(vec![String::from("quux"), String::from("strange")]),
                uri: None,
                markers: Some(vec![
                    String::from("python_version<'2.7'"),
                    String::from("platform_version=='2'")
                ])
            }
        );
    }

    #[test]
    fn test_uri_spec() {
        let parsed = parse("name [fred,bar] @ http://foo.com ; python_version=='2.7'").unwrap();
        assert_eq!(
            parsed,
            Dependency {
                name: String::from("name"),
                version: VersionReq::parse("*").unwrap(),
                extras: Some(vec![String::from("fred"), String::from("bar")]),
                uri: Some("http://foo.com".to_string()),
                markers: Some(vec![String::from("python_version=='2.7'")])
            }
        );
    }
}