Skip to content

Commit

Permalink
Merge pull request #14 from tdejager/feat/uv-origin-to-pep508
Browse files Browse the repository at this point in the history
feat: add origin to pep508 type
  • Loading branch information
konstin committed May 10, 2024
2 parents 63866e7 13fa31e commit b4abf5e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 15,7 @@

#![warn(missing_docs)]

use origin::RequirementOrigin;
#[cfg(feature = "pyo3")]
use pep440_rs::PyVersion;
use pep440_rs::{Version, VersionSpecifier, VersionSpecifiers};
Expand Down Expand Up @@ -46,6 47,7 @@ pub use verbatim_url::{split_scheme, VerbatimUrl};

mod marker;
mod normalize;
mod origin;
mod verbatim_url;

/// Error with a span attached. Not that those aren't `String` but `Vec<char>` indices.
Expand Down Expand Up @@ -137,6 139,19 @@ pub struct Requirement {
/// `requests [security,tests] >= 2.8.1, == 2.8.* ; python_version > "3.8"`.
/// Those are a nested and/or tree
pub marker: Option<MarkerTree>,
/// The source file containing the requirement.
pub origin: Option<RequirementOrigin>,
}

impl Requirement {
/// Set the source file containing the requirement.
#[must_use]
pub fn with_origin(self, origin: RequirementOrigin) -> Self {
Self {
origin: Some(origin),
..self
}
}
}

impl Display for Requirement {
Expand Down Expand Up @@ -993,6 1008,7 @@ fn parse(cursor: &mut Cursor, working_dir: Option<&Path>) -> Result<Requirement,
extras,
version_or_url: requirement_kind,
marker,
origin: None,
})
}

Expand Down Expand Up @@ -1126,6 1142,7 @@ mod tests {
operator: MarkerOperator::LessThan,
r_value: MarkerValue::QuotedString("2.7".to_string()),
})),
origin: None,
};
assert_eq!(requests, expected);
}
Expand Down Expand Up @@ -1291,6 1308,7 @@ mod tests {
extras: vec![],
marker: None,
version_or_url: Some(VersionOrUrl::Url(VerbatimUrl::from_str(url).unwrap())),
origin: None,
};
assert_eq!(pip_url, expected);
}
Expand Down
22 changes: 22 additions & 0 deletions src/origin.rs
Original file line number Diff line number Diff line change
@@ -0,0 1,22 @@
use std::path::{Path, PathBuf};

use crate::normalize::PackageName;

/// The origin of a dependency, e.g., a `-r requirements.txt` file.
#[derive(Hash, Debug, Clone, Eq, PartialEq, PartialOrd, Ord)]
pub enum RequirementOrigin {
/// The requirement was provided via a standalone file (e.g., a `requirements.txt` file).
File(PathBuf),
/// The requirement was provided via a local project (e.g., a `pyproject.toml` file).
Project(PathBuf, PackageName),
}

impl RequirementOrigin {
/// Returns the path of the requirement origin.
pub fn path(&self) -> &Path {
match self {
RequirementOrigin::File(path) => path.as_path(),
RequirementOrigin::Project(path, _) => path.as_path(),
}
}
}

0 comments on commit b4abf5e

Please sign in to comment.