2024-06-03 18:37:15 -04:00
|
|
|
use pep508_rs::StringVersion;
|
|
|
|
|
use uv_interpreter::{Interpreter, PythonVersion};
|
2024-01-03 11:20:45 -04:00
|
|
|
|
2024-03-12 17:11:50 -07:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2024-01-22 00:32:02 -06:00
|
|
|
pub struct PythonRequirement {
|
2024-01-03 11:20:45 -04:00
|
|
|
/// The installed version of Python.
|
2024-03-12 17:11:50 -07:00
|
|
|
installed: StringVersion,
|
2024-01-03 11:20:45 -04:00
|
|
|
/// The target version of Python; that is, the version of Python for which we are resolving
|
|
|
|
|
/// dependencies. This is typically the same as the installed version, but may be different
|
|
|
|
|
/// when specifying an alternate Python version for the resolution.
|
2024-06-03 18:37:15 -04:00
|
|
|
///
|
|
|
|
|
/// If `None`, the target version is the same as the installed version.
|
|
|
|
|
target: Option<StringVersion>,
|
2024-01-03 11:20:45 -04:00
|
|
|
}
|
|
|
|
|
|
2024-01-22 00:32:02 -06:00
|
|
|
impl PythonRequirement {
|
2024-06-03 18:37:15 -04:00
|
|
|
/// Create a [`PythonRequirement`] to resolve against both an [`Interpreter`] and a
|
|
|
|
|
/// [`PythonVersion`].
|
|
|
|
|
pub fn from_python_version(interpreter: &Interpreter, python_version: &PythonVersion) -> Self {
|
2024-01-03 11:20:45 -04:00
|
|
|
Self {
|
2024-03-12 17:11:50 -07:00
|
|
|
installed: interpreter.python_full_version().clone(),
|
2024-06-03 18:37:15 -04:00
|
|
|
target: Some(StringVersion {
|
|
|
|
|
string: python_version.to_string(),
|
|
|
|
|
version: python_version.python_full_version(),
|
|
|
|
|
}),
|
2024-01-03 11:20:45 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-03 18:37:15 -04:00
|
|
|
/// Create a [`PythonRequirement`] to resolve against an [`Interpreter`].
|
2024-06-02 21:33:18 -04:00
|
|
|
pub fn from_interpreter(interpreter: &Interpreter) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
installed: interpreter.python_full_version().clone(),
|
2024-06-03 18:37:15 -04:00
|
|
|
target: None,
|
2024-06-02 21:33:18 -04:00
|
|
|
}
|
2024-05-08 11:21:11 -04:00
|
|
|
}
|
|
|
|
|
|
2024-01-03 11:20:45 -04:00
|
|
|
/// Return the installed version of Python.
|
2024-03-12 17:11:50 -07:00
|
|
|
pub fn installed(&self) -> &StringVersion {
|
2024-01-22 00:32:02 -06:00
|
|
|
&self.installed
|
2024-01-03 11:20:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Return the target version of Python.
|
2024-06-03 18:37:15 -04:00
|
|
|
pub fn target(&self) -> Option<&StringVersion> {
|
|
|
|
|
self.target.as_ref()
|
2024-01-03 11:20:45 -04:00
|
|
|
}
|
|
|
|
|
}
|