Skip to content

Commit

Permalink
Fix warnings and run cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
simonrw authored and David-OConnor committed Aug 27, 2020
1 parent c281f78 commit 860408d
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 143 deletions.
270 changes: 134 additions & 136 deletions src/dep_parser.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
use std::io::ErrorKind;
use std::str::FromStr;

use nom::{AsChar, InputTakeAtPosition, IResult};
use nom::branch::alt;
use nom::bytes::complete::{tag, take};
use nom::character::complete::{digit1, space0, space1};
use nom::combinator::{flat_map, map, map_parser, map_res, opt, value};
use nom::multi::{many0, many_m_n, separated_list};
use nom::sequence::{delimited, preceded, separated_pair, terminated, tuple};
use nom::multi::separated_list;
use nom::sequence::{delimited, preceded, separated_pair, tuple};
use nom::{AsChar, IResult, InputTakeAtPosition};

use crate::dep_types::{Constraint, DependencyError, Extras, Req, ReqType, Version, VersionModifier};
use crate::install::download_and_install_package;
use crate::dep_types::{Constraint, Extras, Req, ReqType, Version, VersionModifier};
use crate::util::Os;

enum ExtrasPart {
Expand All @@ -19,18 +17,19 @@ enum ExtrasPart {
PythonVersion(Constraint),
}


pub fn parse_req(input: &str) -> IResult<&str, Req> {
// eg saturn = ">=0.3.4", as in pyproject.toml
map(alt((
separated_pair(
parse_package_name,
tuple((space0, tag("="), space0)),
delimited(quote, parse_constraints, quote),
),
map(parse_package_name, |x| (x, vec![]))
)),
|(name, constraints)| Req::new(name.to_string(), constraints))(input)
map(
alt((
separated_pair(
parse_package_name,
tuple((space0, tag("="), space0)),
delimited(quote, parse_constraints, quote),
),
map(parse_package_name, |x| (x, vec![])),
)),
|(name, constraints)| Req::new(name.to_string(), constraints),
)(input)
}

pub fn parse_req_pypi_fmt(input: &str) -> IResult<&str, Req> {
Expand All @@ -39,98 +38,104 @@ pub fn parse_req_pypi_fmt(input: &str) -> IResult<&str, Req> {
// wildcard, so we don't accidentally match a semicolon here if a
// set of parens appears later. The non-greedy ? in the version-matching
// expression's important as well, in some cases of extras.
map(alt((
tuple((
tuple((parse_package_name, opt(parse_install_with_extras))),
alt((
preceded(space0, delimited(tag("("), parse_constraints, tag(")"))),
preceded(space1, parse_constraints),
map(
alt((
tuple((
tuple((parse_package_name, opt(parse_install_with_extras))),
alt((
preceded(space0, delimited(tag("("), parse_constraints, tag(")"))),
preceded(space1, parse_constraints),
)),
opt(preceded(tuple((space0, tag(";"), space0)), parse_extras)),
)),
opt(preceded(tuple((space0, tag(";"), space0)), parse_extras)),
map(
tuple((
tuple((parse_package_name, opt(parse_install_with_extras))),
opt(preceded(tuple((space0, tag(";"), space0)), parse_extras)),
)),
|(x, y)| (x, vec![], y),
),
)),
map(tuple((
tuple((parse_package_name, opt(parse_install_with_extras))),
opt(preceded(tuple((space0, tag(";"), space0)), parse_extras)),
)), |(x, y)| (x, vec![], y)),
)),
|((name, install_with_extras), constraints, extras_opt)| {
let mut r = if let Some(extras) = extras_opt {
let mut r = if let Some(extras) = extras_opt {
Req::new_with_extras(name.to_string(), constraints, extras)
} else {
Req::new(name.to_string(), constraints)
};
r.install_with_extras = install_with_extras;
r
})(input)
},
)(input)
}

pub fn parse_pip_str(input: &str) -> IResult<&str, Req> {
map(
tuple((parse_package_name, opt(parse_constraint))),
|(name, constraint)| {
Req::new(name.to_string(), constraint.into_iter().collect())
}
|(name, constraint)| Req::new(name.to_string(), constraint.into_iter().collect()),
)(input)
}

pub fn parse_wh_py_vers(input: &str) -> IResult<&str, Vec<Constraint>> {
alt((
map(tag("any"),
|_| vec![Constraint::new(
ReqType::Gte, Version::new(2, 0, 0))],
),
map(parse_version,
|v| vec![Constraint::new(ReqType::Caret, v)]
),
map(tag("any"), |_| {
vec![Constraint::new(ReqType::Gte, Version::new(2, 0, 0))]
}),
map(parse_version, |v| vec![Constraint::new(ReqType::Caret, v)]),
separated_list(tag("."), parse_wh_py_ver),
))(input)
}

fn parse_wh_py_ver(input: &str) -> IResult<&str, Constraint> {
map(tuple((
alt((tag("cp"), tag("py"), tag("pp"))),
alt((tag("2"), tag("3"), tag("4"))),
opt(map_parser(take(1u8), digit1)),
)), |(_, major, minor): (_, &str, Option<&str>)| {
let major: u32 = major.parse().unwrap();
match minor {
Some(mi) => {
Constraint::new(
map(
tuple((
alt((tag("cp"), tag("py"), tag("pp"))),
alt((tag("2"), tag("3"), tag("4"))),
opt(map_parser(take(1u8), digit1)),
)),
|(_, major, minor): (_, &str, Option<&str>)| {
let major: u32 = major.parse().unwrap();
match minor {
Some(mi) => Constraint::new(
ReqType::Exact,
Version::new_short(major, mi.parse().unwrap()))
},
Nome => {
if major == 2 {
Constraint::new(ReqType::Lte, Version::new_short(2, 10))
} else {
Constraint::new(ReqType::Gte, Version::new_short(3, 0))
Version::new_short(major, mi.parse().unwrap()),
),
None => {
if major == 2 {
Constraint::new(ReqType::Lte, Version::new_short(2, 10))
} else {
Constraint::new(ReqType::Gte, Version::new_short(3, 0))
}
}
}
}
})(input)
},
)(input)
}

fn quote(input: &str) -> IResult<&str, &str> {
alt((
tag("\""),
tag("'"),
))(input)
alt((tag("\""), tag("'")))(input)
}

fn parse_install_with_extras(input: &str) -> IResult<&str, Vec<String>> {
map(
delimited(tag("["), separated_list(tag(","), parse_package_name), tag("]")),
|extras| extras.iter().map(|x| x.to_string()).collect()
delimited(
tag("["),
separated_list(tag(","), parse_package_name),
tag("]"),
),
|extras| extras.iter().map(|x| x.to_string()).collect(),
)(input)
}

pub fn parse_extras(input: &str) -> IResult<&str, Extras> {
map(
separated_list(delimited(space0, tag("and"), space0), delimited(
opt(preceded(tag("("), space0)),
parse_extra_part,
opt(preceded(space0, tag(")"))),
)),
separated_list(
delimited(space0, tag("and"), space0),
delimited(
opt(preceded(tag("("), space0)),
parse_extra_part,
opt(preceded(space0, tag(")"))),
),
),
|ps| {
let mut extra = None;
let mut sys_platform = None;
Expand All @@ -149,54 +154,53 @@ pub fn parse_extras(input: &str) -> IResult<&str, Extras> {
sys_platform,
python_version,
}
})(input)
},
)(input)
}

fn parse_extra_part(input: &str) -> IResult<&str, ExtrasPart> {
flat_map(alt((
tag("extra"),
tag("sys_platform"),
tag("python_version"),
)), |type_| {
move |input: &str| {
match type_ {
"extra" => { map(
flat_map(
alt((tag("extra"), tag("sys_platform"), tag("python_version"))),
|type_| {
move |input: &str| match type_ {
"extra" => map(
preceded(
separated_pair(space0, tag("=="), space0),
delimited(quote, parse_package_name, quote)
delimited(quote, parse_package_name, quote),
),
|x| ExtrasPart::Extra(x.to_string()))(input)
},
"sys_platform" => { map(
tuple((delimited(space0, tag("=="), space0),
delimited(quote, parse_package_name, quote)
)), |(r, o)| {
ExtrasPart::SysPlatform(ReqType::Exact, Os::from_str(o).unwrap())
})(input) },
"python_version" => {
map(tuple((
|x| ExtrasPart::Extra(x.to_string()),
)(input),
"sys_platform" => map(
tuple((
delimited(space0, tag("=="), space0),
delimited(quote, parse_package_name, quote),
)),
|(_, o)| ExtrasPart::SysPlatform(ReqType::Exact, Os::from_str(o).unwrap()),
)(input),
"python_version" => map(
tuple((
delimited(space0, parse_req_type, space0),
delimited(quote, parse_version, quote)
)), |(r, v)| ExtrasPart::PythonVersion(
Constraint::new(r, v)
))(input) },
_ => panic!("Found unexpected")
delimited(quote, parse_version, quote),
)),
|(r, v)| ExtrasPart::PythonVersion(Constraint::new(r, v)),
)(input),
_ => panic!("Found unexpected"),
}
}

})(input)
},
)(input)
}

pub fn parse_constraints(input: &str) -> IResult<&str, Vec<Constraint>> {
separated_list(tuple((space0, tag(","), space0)), parse_constraint)(input)
}

pub fn parse_constraint(input: &str) -> IResult<&str, Constraint> {
map(alt((
value((Some(ReqType::Gte), Version::new(0, 0, 0)), tag("*")),
tuple((opt(parse_req_type), parse_version)),
)),
|(r, v)| Constraint::new(r.unwrap_or(ReqType::Exact), v)
map(
alt((
value((Some(ReqType::Gte), Version::new(0, 0, 0)), tag("*")),
tuple((opt(parse_req_type), parse_version)),
)),
|(r, v)| Constraint::new(r.unwrap_or(ReqType::Exact), v),
)(input)
}

Expand All @@ -217,17 +221,20 @@ pub fn parse_version(input: &str) -> IResult<&str, Version> {
}

pub fn parse_req_type(input: &str) -> IResult<&str, ReqType> {
map_res(alt((
tag("=="),
tag(">="),
tag("<="),
tag(">"),
tag("<"),
tag("!="),
tag("^"),
tag("~="),
tag("~"),
)), |x| ReqType::from_str(x))(input)
map_res(
alt((
tag("=="),
tag(">="),
tag("<="),
tag(">"),
tag("<"),
tag("!="),
tag("^"),
tag("~="),
tag("~"),
)),
|x| ReqType::from_str(x),
)(input)
}

fn parse_package_name(input: &str) -> IResult<&str, &str> {
Expand All @@ -244,36 +251,29 @@ fn is_package_char(c: char) -> bool {
}

fn parse_digit_or_wildcard(input: &str) -> IResult<&str, u32> {
map(
alt((digit1, value("0", tag("*")))),
|digit: &str| digit.parse().unwrap(),
)(input)
map(alt((digit1, value("0", tag("*")))), |digit: &str| {
digit.parse().unwrap()
})(input)
}

fn parse_modifier(input: &str) -> IResult<&str, Option<(VersionModifier, u32)>> {
opt(
map(
tuple((opt(tag(".")), parse_modifier_version, digit1)),
|(_, version_modifier, n)| (version_modifier, n.parse().unwrap())
)
)(input)
opt(map(
tuple((opt(tag(".")), parse_modifier_version, digit1)),
|(_, version_modifier, n)| (version_modifier, n.parse().unwrap()),
))(input)
}

fn parse_modifier_version(input: &str) -> IResult<&str, VersionModifier> {
map(alt((
tag("a"),
tag("b"),
tag("rc"),
tag("dep"),
)), |x| {
match x {
map(
alt((tag("a"), tag("b"), tag("rc"), tag("dep"))),
|x| match x {
"a" => VersionModifier::Alpha,
"b" => VersionModifier::Beta,
"rc" => VersionModifier::ReleaseCandidate,
"dep" => VersionModifier::Dep,
_ => panic!("not execute this code"),
}
})(input)
},
)(input)
}

#[cfg(test)]
Expand All @@ -285,9 +285,7 @@ mod tests {
use super::*;

#[test]
fn dummy_test() {

}
fn dummy_test() {}

#[rstest(input, expected,
case("*", Ok(("", Constraint::new(ReqType::Gte, Version::new(0, 0, 0))))),
Expand Down Expand Up @@ -439,4 +437,4 @@ mod tests {
fn test_parse_req_pypi(input: &str, expected: IResult<&str, Req>) {
assert_eq!(parse_req_pypi_fmt(input), expected);
}
}
}
Loading

0 comments on commit 860408d

Please sign in to comment.