Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ md-5 = "0.10.6"
memchr = "2.7.4"
memmap2 = "0.9.5"
netrc-rs = "0.1.2"
nom = "7.1.3"
nom = "8.0.0"
nom-language = "0.1.0"
num_cpus = "1.16.0"
opendal = { version = "0.53.3", default-features = false }
once_cell = "1.21.3"
Expand Down
1 change: 1 addition & 0 deletions crates/rattler_conda_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ hex = { workspace = true }
itertools = { workspace = true }
lazy-regex = { workspace = true }
nom = { workspace = true }
nom-language = { workspace = true }
purl = { workspace = true, features = ["serde"] }
rattler_digest = { workspace = true, default-features = false, features = [
"serde",
Expand Down
48 changes: 27 additions & 21 deletions crates/rattler_conda_types/src/match_spec/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use nom::{
error::{context, ContextError, ParseError},
multi::{separated_list0, separated_list1},
sequence::{delimited, preceded, separated_pair, terminated},
Finish, IResult,
Finish, IResult, Parser,
};
use rattler_digest::{parse_digest_from_hex, Md5, Sha256};
use smallvec::SmallVec;
Expand Down Expand Up @@ -150,13 +150,13 @@ type BracketVec<'a> = SmallVec<[(&'a str, &'a str); 2]>;
/// A parse combinator to filter whitespace if front and after another parser.
fn whitespace_enclosed<'a, F, O, E: ParseError<&'a str>>(
mut inner: F,
) -> impl FnMut(&'a str) -> IResult<&'a str, O, E>
) -> impl Parser<&'a str, Output = O, Error = E>
where
F: FnMut(&'a str) -> IResult<&'a str, O, E>,
F: Parser<&'a str, Output = O, Error = E>,
{
move |input: &'a str| {
let (input, _) = multispace0(input)?;
let (input, o2) = inner(input)?;
let (input, o2) = inner.parse(input)?;
multispace0(input).map(|(i, _)| (i, o2))
}
}
Expand All @@ -168,7 +168,8 @@ fn parse_bracket_list(input: &str) -> Result<BracketVec<'_>, ParseMatchSpecError
whitespace_enclosed(context(
"key",
take_while(|c: char| c.is_alphanumeric() || c == '_' || c == '-'),
))(input)
))
.parse(input)
}

/// Parses a value in a bracket string.
Expand All @@ -181,22 +182,23 @@ fn parse_bracket_list(input: &str) -> Result<BracketVec<'_>, ParseMatchSpecError
delimited(char('['), take_until("]"), char(']')),
take_till1(|c| c == ',' || c == ']' || c == '\'' || c == '"'),
)),
))(input)
))
.parse(input)
}

/// Parses a `key=value` pair
fn parse_key_value(input: &str) -> IResult<&str, (&str, &str)> {
separated_pair(parse_key, char('='), parse_value)(input)
separated_pair(parse_key, char('='), parse_value).parse(input)
}

/// Parses a list of `key=value` pairs separated by commas
fn parse_key_value_list(input: &str) -> IResult<&str, Vec<(&str, &str)>> {
separated_list0(whitespace_enclosed(char(',')), parse_key_value)(input)
separated_list0(whitespace_enclosed(char(',')), parse_key_value).parse(input)
}

/// Parses an entire bracket string
fn parse_bracket_list(input: &str) -> IResult<&str, Vec<(&str, &str)>> {
delimited(char('['), parse_key_value_list, char(']'))(input)
delimited(char('['), parse_key_value_list, char(']')).parse(input)
}

match parse_bracket_list(input).finish() {
Expand Down Expand Up @@ -240,14 +242,15 @@ pub fn parse_extras(input: &str) -> Result<Vec<String>, ParseMatchSpecError> {
multispace0,
take_while1(|c: char| c.is_alphanumeric() || c == '_' || c == '-'),
multispace0,
)(i)
)
.parse(i)
}

fn parse_features(i: &str) -> IResult<&str, Vec<String>> {
separated_list1(char(','), map(parse_feature_name, |s: &str| s.to_string()))(i)
separated_list1(char(','), map(parse_feature_name, |s: &str| s.to_string())).parse(i)
}

match all_consuming(parse_features)(input).finish() {
match all_consuming(parse_features).parse(input).finish() {
Ok((_remaining, features)) => Ok(features),
Err(_e) => Err(ParseMatchSpecError::InvalidBracket),
}
Expand Down Expand Up @@ -392,7 +395,7 @@ fn split_version_and_build(
) -> impl FnMut(&'a str) -> IResult<&'a str, &'a str, E> {
move |input: &'a str| {
if strictness == Lenient {
alt((parse_special_equality, recognize_constraint))(input)
alt((parse_special_equality, recognize_constraint)).parse(input)
} else {
recognize_constraint(input)
}
Expand All @@ -406,7 +409,8 @@ fn split_version_and_build(
alt((
delimited(tag("("), parse_version_group(strictness), tag(")")),
maybe_recognize_lenient_constraint(strictness),
))(input)
))
.parse(input)
}
}

Expand All @@ -417,7 +421,8 @@ fn split_version_and_build(
recognize(separated_list1(
whitespace_enclosed(one_of(",|")),
parse_version_constraint_or_group(strictness),
))(input)
))
.parse(input)
}
}

Expand All @@ -437,17 +442,18 @@ fn split_version_and_build(
recognize(preceded(
tag("="),
alt((version_followed_by_glob, just_star)),
))(input)
))
.parse(input)
}

fn parse_version_and_build_separator<'a, E: ParseError<&'a str> + ContextError<&'a str>>(
strictness: ParseStrictness,
) -> impl FnMut(&'a str) -> IResult<&'a str, &'a str, E> {
move |input: &'a str| {
if strictness == Lenient {
terminated(parse_version_group(strictness), opt(one_of(" =")))(input)
terminated(parse_version_group(strictness), opt(one_of(" ="))).parse(input)
} else {
terminated(parse_version_group(strictness), space0)(input)
terminated(parse_version_group(strictness), space0).parse(input)
}
}
}
Expand All @@ -470,9 +476,9 @@ fn split_version_and_build(
build_string.is_empty().not().then_some(build_string),
))
}
Err(nom::error::VerboseError { .. }) => Err(ParseMatchSpecError::InvalidVersionAndBuild(
input.to_string(),
)),
Err(nom_language::error::VerboseError { .. }) => Err(
ParseMatchSpecError::InvalidVersionAndBuild(input.to_string()),
),
}
}
/// Parse version and build string.
Expand Down
62 changes: 37 additions & 25 deletions crates/rattler_conda_types/src/package/has_prefix.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
use crate::{package::paths::FileMode, package::PackageFile};
use nom::{
branch::alt,
bytes::complete::{tag, tag_no_case, take_till1},
character::complete::multispace1,
combinator::{all_consuming, map, value},
sequence::{preceded, terminated, tuple},
IResult,
};
use std::{
borrow::Cow,
hint::black_box,
Expand All @@ -15,6 +6,17 @@ use std::{
sync::OnceLock,
};

use nom::{
branch::alt,
bytes::complete::{tag, tag_no_case, take_till1},
character::complete::multispace1,
combinator::{all_consuming, map, value},
sequence::{preceded, terminated},
IResult, Parser,
};

use crate::package::{paths::FileMode, PackageFile};

/// Representation of an entry in `info/has_prefix`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HasPrefixEntry {
Expand All @@ -29,7 +31,8 @@ pub struct HasPrefixEntry {
/// Representation of the `info/has_prefix` file in older package archives.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HasPrefix {
/// A list of files in the package that contain the `prefix` (and need prefix replacement).
/// A list of files in the package that contain the `prefix` (and need
/// prefix replacement).
pub files: Vec<HasPrefixEntry>,
}

Expand All @@ -48,11 +51,13 @@ impl PackageFile for HasPrefix {
}
}

/// Returns the default placeholder path. Although this is just a constant it is constructed at
/// runtime. This ensures that the string itself is not present in the binary when compiled. The
/// reason we want that is that conda-build (and friends) tries to replace this placeholder in the
/// binary to point to the actual path in the installed conda environment. In this case we don't
/// want to that so we deliberately break up the string and reconstruct it at runtime.
/// Returns the default placeholder path. Although this is just a constant it is
/// constructed at runtime. This ensures that the string itself is not present
/// in the binary when compiled. The reason we want that is that conda-build
/// (and friends) tries to replace this placeholder in the binary to point to
/// the actual path in the installed conda environment. In this case we don't
/// want to that so we deliberately break up the string and reconstruct it at
/// runtime.
fn placeholder_string() -> &'static str {
static PLACEHOLDER: OnceLock<String> = OnceLock::new();
PLACEHOLDER
Expand All @@ -70,22 +75,24 @@ impl FromStr for HasPrefixEntry {
type Err = std::io::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
/// Parses `<prefix> <file_mode> <path>` and fails if there is more input.
/// Parses `<prefix> <file_mode> <path>` and fails if there is more
/// input.
fn prefix_file_mode_path(buf: &str) -> IResult<&str, HasPrefixEntry> {
all_consuming(map(
tuple((
(
possibly_quoted_string,
multispace1,
file_mode,
multispace1,
possibly_quoted_string,
)),
),
|(prefix, _, file_mode, _, path)| HasPrefixEntry {
prefix: Cow::Owned(prefix.into_owned()),
file_mode,
relative_path: PathBuf::from(&*path),
},
))(buf)
))
.parse(buf)
}

/// Parses "<path>" and fails if there is more input.
Expand All @@ -94,23 +101,26 @@ impl FromStr for HasPrefixEntry {
prefix: Cow::Borrowed(placeholder_string()),
file_mode: FileMode::Text,
relative_path: PathBuf::from(&*path),
}))(buf)
}))
.parse(buf)
}

/// Parses "text|binary" as a [`FileMode`]
fn file_mode(buf: &str) -> IResult<&str, FileMode> {
alt((
value(FileMode::Text, tag_no_case("text")),
value(FileMode::Binary, tag_no_case("binary")),
))(buf)
))
.parse(buf)
}

/// Parses either a quoted or an unquoted string.
fn possibly_quoted_string(buf: &str) -> IResult<&str, Cow<'_, str>> {
alt((
map(quoted_string, Cow::Owned),
map(take_till1(char::is_whitespace), Cow::Borrowed),
))(buf)
))
.parse(buf)
}

/// Parses a quoted string and delimited '\"'
Expand All @@ -132,20 +142,22 @@ impl FromStr for HasPrefixEntry {
}

let qs = preceded(tag("\""), in_quotes);
terminated(qs, tag("\""))(buf)
terminated(qs, tag("\"")).parse(buf)
}

alt((prefix_file_mode_path, only_path))(s)
alt((prefix_file_mode_path, only_path))
.parse(s)
.map(|(_, res)| res)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string()))
}
}

#[cfg(test)]
mod test {
use std::{borrow::Cow, path::PathBuf, str::FromStr};

use super::*;
use crate::package::FileMode;
use std::{borrow::Cow, path::PathBuf, str::FromStr};

#[test]
fn test_placeholder() {
Expand Down
Loading
Loading