Skip to main content

uv_resolver/lock/export/
pylock_toml.rs

1use std::borrow::Cow;
2use std::ffi::OsStr;
3use std::path::{Path, PathBuf};
4use std::str::FromStr;
5use std::sync::Arc;
6
7use jiff::Timestamp;
8use jiff::civil::{Date, DateTime, Time};
9use jiff::tz::{Offset, TimeZone};
10use petgraph::graph::NodeIndex;
11use serde::Deserialize;
12use toml_edit::{Array, ArrayOfTables, Item, Table, value};
13use url::Url;
14
15use uv_cache_key::RepositoryUrl;
16use uv_configuration::{
17    BuildOptions, DependencyGroupsWithDefaults, EditableMode, ExtrasSpecificationWithDefaults,
18    InstallOptions,
19};
20use uv_distribution_filename::{
21    BuildTag, DistExtension, ExtensionError, SourceDistExtension, SourceDistFilename,
22    SourceDistFilenameError, WheelFilename, WheelFilenameError,
23};
24use uv_distribution_types::{
25    BuiltDist, DirectUrlBuiltDist, DirectUrlSourceDist, DirectorySourceDist, Dist, Edge,
26    FileLocation, GitDirectorySourceDist, IndexUrl, Name, Node, PathBuiltDist, PathSourceDist,
27    RegistryBuiltDist, RegistryBuiltWheel, RegistrySourceDist, RemoteSource, RequiresPython,
28    Resolution, ResolvedDist, SourceDist, ToUrlError, UrlString,
29};
30use uv_fs::{PortablePathBuf, normalize_path, try_relative_to_if};
31use uv_git::{RepositoryReference, ResolvedRepositoryReference};
32use uv_git_types::{GitLfs, GitOid, GitReference, GitUrl, GitUrlParseError};
33use uv_normalize::{ExtraName, GroupName, PackageName};
34use uv_pep440::Version;
35use uv_pep508::{MarkerEnvironment, MarkerTree, VerbatimUrl};
36use uv_platform_tags::{TagCompatibility, TagPriority, Tags};
37use uv_pypi_types::{HashDigests, Hashes, ParsedGitDirectoryUrl, VcsKind};
38use uv_redacted::DisplaySafeUrl;
39use uv_small_str::SmallString;
40
41use crate::lock::export::ExportableRequirements;
42use crate::lock::{Source, WheelTagHint, each_element_on_its_line_array, is_wheel_unreachable};
43use crate::{Installable, LockError, ResolverOutput};
44
45#[derive(Debug, thiserror::Error)]
46pub enum PylockTomlErrorKind {
47    #[error("Package `{0}` requires Python {2}, but the target Python version is {1}")]
48    IncompatibleRequiresPython(PackageName, Version, RequiresPython),
49    #[error(
50        "Package `{0}` includes both a registry (`packages.wheels`) and a directory source (`packages.directory`)"
51    )]
52    WheelWithDirectory(PackageName),
53    #[error(
54        "Package `{0}` includes both a registry (`packages.wheels`) and a VCS source (`packages.vcs`)"
55    )]
56    WheelWithVcs(PackageName),
57    #[error(
58        "Package `{0}` includes both a registry (`packages.wheels`) and an archive source (`packages.archive`)"
59    )]
60    WheelWithArchive(PackageName),
61    #[error(
62        "Package `{0}` includes both a registry (`packages.sdist`) and a directory source (`packages.directory`)"
63    )]
64    SdistWithDirectory(PackageName),
65    #[error(
66        "Package `{0}` includes both a registry (`packages.sdist`) and a VCS source (`packages.vcs`)"
67    )]
68    SdistWithVcs(PackageName),
69    #[error(
70        "Package `{0}` includes both a registry (`packages.sdist`) and an archive source (`packages.archive`)"
71    )]
72    SdistWithArchive(PackageName),
73    #[error(
74        "Package `{0}` includes both a directory (`packages.directory`) and a VCS source (`packages.vcs`)"
75    )]
76    DirectoryWithVcs(PackageName),
77    #[error(
78        "Package `{0}` includes both a directory (`packages.directory`) and an archive source (`packages.archive`)"
79    )]
80    DirectoryWithArchive(PackageName),
81    #[error(
82        "Package `{0}` includes both a VCS (`packages.vcs`) and an archive source (`packages.archive`)"
83    )]
84    VcsWithArchive(PackageName),
85    #[error(
86        "Package `{0}` must include one of: `wheels`, `directory`, `archive`, `sdist`, or `vcs`"
87    )]
88    MissingSource(PackageName),
89    #[error("Package `{0}` uses a Git archive, which pylock.toml export does not support")]
90    GitArchiveUnsupported(PackageName),
91    #[error("Package `{0}` does not include a compatible wheel for the current platform")]
92    MissingWheel(PackageName),
93    #[error("`packages.wheel` entry for `{0}` must have a `path` or `url`")]
94    WheelMissingPathUrl(PackageName),
95    #[error("`packages.sdist` entry for `{0}` must have a `path` or `url`")]
96    SdistMissingPathUrl(PackageName),
97    #[error("`packages.archive` entry for `{0}` must have a `path` or `url`")]
98    ArchiveMissingPathUrl(PackageName),
99    #[error("`packages.vcs` entry for `{0}` must have a `url` or `path`")]
100    VcsMissingPathUrl(PackageName),
101    #[error("URL must end in a valid wheel filename: `{0}`")]
102    UrlMissingFilename(DisplaySafeUrl),
103    #[error("Path must end in a valid wheel filename: `{0}`")]
104    PathMissingFilename(Box<Path>),
105    #[error("Failed to convert path to URL")]
106    PathToUrl,
107    #[error("Failed to convert URL to path")]
108    UrlToPath,
109    #[error(
110        "Package `{0}` can't be installed because it doesn't have a source distribution or wheel for the current platform"
111    )]
112    NeitherSourceDistNorWheel(PackageName),
113    #[error(
114        "Package `{0}` can't be installed because it is marked as both `--no-binary` and `--no-build`"
115    )]
116    NoBinaryNoBuild(PackageName),
117    #[error(
118        "Package `{0}` can't be installed because it is marked as `--no-binary` but has no source distribution"
119    )]
120    NoBinary(PackageName),
121    #[error(
122        "Package `{0}` can't be installed because it is marked as `--no-build` but has no binary distribution"
123    )]
124    NoBuild(PackageName),
125    #[error(
126        "Package `{0}` can't be installed because the binary distribution is incompatible with the current platform"
127    )]
128    IncompatibleWheelOnly(PackageName),
129    #[error(
130        "Package `{0}` can't be installed because it is marked as `--no-binary` but is itself a binary distribution"
131    )]
132    NoBinaryWheelOnly(PackageName),
133    #[error(transparent)]
134    WheelFilename(#[from] WheelFilenameError),
135    #[error(transparent)]
136    SourceDistFilename(#[from] SourceDistFilenameError),
137    #[error(transparent)]
138    ToUrl(#[from] ToUrlError),
139    #[error(transparent)]
140    GitUrlParse(#[from] GitUrlParseError),
141    #[error(transparent)]
142    LockError(#[from] LockError),
143    #[error(transparent)]
144    Extension(#[from] ExtensionError),
145    #[error(transparent)]
146    Jiff(#[from] jiff::Error),
147    #[error(transparent)]
148    Io(#[from] std::io::Error),
149    #[error(transparent)]
150    Deserialize(#[from] toml::de::Error),
151}
152
153#[derive(Debug)]
154pub struct PylockTomlError {
155    kind: Box<PylockTomlErrorKind>,
156    hint: Option<WheelTagHint>,
157}
158
159impl std::error::Error for PylockTomlError {
160    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
161        self.kind.source()
162    }
163}
164
165impl std::fmt::Display for PylockTomlError {
166    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
167        write!(f, "{}", self.kind)
168    }
169}
170
171impl uv_errors::Hint for PylockTomlError {
172    fn hints(&self) -> uv_errors::Hints<'_> {
173        if let Some(hint) = &self.hint {
174            uv_errors::Hints::from(hint.to_string())
175        } else {
176            uv_errors::Hints::none()
177        }
178    }
179}
180
181impl<E> From<E> for PylockTomlError
182where
183    PylockTomlErrorKind: From<E>,
184{
185    fn from(err: E) -> Self {
186        Self {
187            kind: Box::new(PylockTomlErrorKind::from(err)),
188            hint: None,
189        }
190    }
191}
192
193#[derive(Debug, serde::Serialize, serde::Deserialize)]
194#[serde(rename_all = "kebab-case")]
195pub struct PylockToml {
196    #[serde(deserialize_with = "deserialize_lock_version")]
197    lock_version: Version,
198    created_by: String,
199    #[serde(skip_serializing_if = "Option::is_none")]
200    pub requires_python: Option<RequiresPython>,
201    #[serde(skip_serializing_if = "Vec::is_empty", default)]
202    pub extras: Vec<ExtraName>,
203    #[serde(skip_serializing_if = "Vec::is_empty", default)]
204    pub dependency_groups: Vec<GroupName>,
205    #[serde(skip_serializing_if = "Vec::is_empty", default)]
206    pub default_groups: Vec<GroupName>,
207    #[serde(skip_serializing_if = "Vec::is_empty", default)]
208    pub packages: Vec<PylockTomlPackage>,
209    #[serde(skip_serializing_if = "Vec::is_empty", default)]
210    attestation_identities: Vec<PylockTomlAttestationIdentity>,
211}
212
213fn deserialize_lock_version<'de, D>(deserializer: D) -> Result<Version, D::Error>
214where
215    D: serde::Deserializer<'de>,
216{
217    let version = Version::deserialize(deserializer)?;
218    if version.release().first() != Some(&1) {
219        return Err(serde::de::Error::custom(format_args!(
220            "unsupported lock version (`{version}`, but only major version 1 is supported)"
221        )));
222    }
223
224    Ok(version)
225}
226
227#[derive(Debug, serde::Serialize, serde::Deserialize)]
228#[serde(rename_all = "kebab-case")]
229pub struct PylockTomlPackage {
230    pub name: PackageName,
231    #[serde(skip_serializing_if = "Option::is_none")]
232    pub version: Option<Version>,
233    #[serde(skip_serializing_if = "Option::is_none")]
234    pub index: Option<DisplaySafeUrl>,
235    #[serde(
236        skip_serializing_if = "uv_pep508::marker::ser::is_empty",
237        serialize_with = "uv_pep508::marker::ser::serialize",
238        default
239    )]
240    marker: MarkerTree,
241    #[serde(skip_serializing_if = "Option::is_none")]
242    requires_python: Option<RequiresPython>,
243    #[serde(skip_serializing_if = "Vec::is_empty", default)]
244    dependencies: Vec<PylockTomlDependency>,
245    #[serde(skip_serializing_if = "Option::is_none")]
246    vcs: Option<PylockTomlVcs>,
247    #[serde(skip_serializing_if = "Option::is_none")]
248    directory: Option<PylockTomlDirectory>,
249    #[serde(skip_serializing_if = "Option::is_none")]
250    archive: Option<PylockTomlArchive>,
251    #[serde(skip_serializing_if = "Option::is_none")]
252    sdist: Option<PylockTomlSdist>,
253    #[serde(skip_serializing_if = "Option::is_none")]
254    wheels: Option<Vec<PylockTomlWheel>>,
255}
256
257#[derive(Debug, serde::Serialize, serde::Deserialize)]
258#[serde(rename_all = "kebab-case")]
259#[expect(clippy::empty_structs_with_brackets)]
260struct PylockTomlDependency {}
261
262#[derive(Debug, serde::Serialize, serde::Deserialize)]
263#[serde(rename_all = "kebab-case")]
264struct PylockTomlDirectory {
265    path: PortablePathBuf,
266    #[serde(skip_serializing_if = "Option::is_none")]
267    editable: Option<bool>,
268    #[serde(skip_serializing_if = "Option::is_none")]
269    subdirectory: Option<PortablePathBuf>,
270}
271
272#[derive(Debug, serde::Serialize, serde::Deserialize)]
273#[serde(rename_all = "kebab-case")]
274struct PylockTomlVcs {
275    r#type: VcsKind,
276    #[serde(skip_serializing_if = "Option::is_none")]
277    url: Option<DisplaySafeUrl>,
278    #[serde(skip_serializing_if = "Option::is_none")]
279    path: Option<PortablePathBuf>,
280    #[serde(skip_serializing_if = "Option::is_none")]
281    requested_revision: Option<String>,
282    commit_id: GitOid,
283    #[serde(skip_serializing_if = "Option::is_none")]
284    subdirectory: Option<PortablePathBuf>,
285}
286
287#[derive(Debug, serde::Serialize, serde::Deserialize)]
288#[serde(rename_all = "kebab-case")]
289struct PylockTomlArchive {
290    #[serde(skip_serializing_if = "Option::is_none")]
291    url: Option<DisplaySafeUrl>,
292    #[serde(skip_serializing_if = "Option::is_none")]
293    path: Option<PortablePathBuf>,
294    #[serde(skip_serializing_if = "Option::is_none")]
295    size: Option<u64>,
296    #[serde(
297        skip_serializing_if = "Option::is_none",
298        serialize_with = "timestamp_to_toml_datetime",
299        deserialize_with = "timestamp_from_toml_datetime",
300        default
301    )]
302    upload_time: Option<Timestamp>,
303    #[serde(skip_serializing_if = "Option::is_none")]
304    subdirectory: Option<PortablePathBuf>,
305    hashes: Hashes,
306}
307
308#[derive(Debug, serde::Serialize, serde::Deserialize)]
309#[serde(rename_all = "kebab-case")]
310struct PylockTomlSdist {
311    #[serde(skip_serializing_if = "Option::is_none")]
312    name: Option<SmallString>,
313    #[serde(skip_serializing_if = "Option::is_none")]
314    url: Option<DisplaySafeUrl>,
315    #[serde(skip_serializing_if = "Option::is_none")]
316    path: Option<PortablePathBuf>,
317    #[serde(
318        skip_serializing_if = "Option::is_none",
319        serialize_with = "timestamp_to_toml_datetime",
320        deserialize_with = "timestamp_from_toml_datetime",
321        default
322    )]
323    upload_time: Option<Timestamp>,
324    #[serde(skip_serializing_if = "Option::is_none")]
325    size: Option<u64>,
326    hashes: Hashes,
327}
328
329#[derive(Debug, serde::Serialize, serde::Deserialize)]
330#[serde(rename_all = "kebab-case")]
331struct PylockTomlWheel {
332    #[serde(skip_serializing_if = "Option::is_none")]
333    name: Option<WheelFilename>,
334    #[serde(skip_serializing_if = "Option::is_none")]
335    url: Option<DisplaySafeUrl>,
336    #[serde(skip_serializing_if = "Option::is_none")]
337    path: Option<PortablePathBuf>,
338    #[serde(
339        skip_serializing_if = "Option::is_none",
340        serialize_with = "timestamp_to_toml_datetime",
341        deserialize_with = "timestamp_from_toml_datetime",
342        default
343    )]
344    upload_time: Option<Timestamp>,
345    #[serde(skip_serializing_if = "Option::is_none")]
346    size: Option<u64>,
347    hashes: Hashes,
348}
349
350#[derive(Debug, serde::Serialize, serde::Deserialize)]
351#[serde(rename_all = "kebab-case")]
352struct PylockTomlAttestationIdentity {
353    kind: String,
354}
355
356impl<'lock> PylockToml {
357    /// Construct a [`PylockToml`] from a [`ResolverOutput`].
358    ///
359    /// If `tags` is provided, only wheels compatible with the given tags will be included.
360    /// If `build_options` is provided, packages marked as `--only-binary` will not include
361    /// source distributions.
362    pub fn from_resolution(
363        resolution: &ResolverOutput,
364        omit: &[PackageName],
365        install_path: &Path,
366        tags: Option<&Tags>,
367        build_options: &BuildOptions,
368    ) -> Result<Self, PylockTomlErrorKind> {
369        // The lock version is always `1.0` at time of writing.
370        let lock_version = Version::new([1, 0]);
371
372        // The created by field is always `uv` at time of writing.
373        let created_by = "uv".to_string();
374
375        // Use the `requires-python` from the target lockfile.
376        let requires_python = resolution.requires_python.clone();
377
378        // We don't support locking for multiple extras at time of writing.
379        let extras = vec![];
380
381        // We don't support locking for multiple dependency groups at time of writing.
382        let dependency_groups = vec![];
383
384        // We don't support locking for multiple dependency groups at time of writing.
385        let default_groups = vec![];
386
387        // We don't support attestation identities at time of writing.
388        let attestation_identities = vec![];
389
390        // Convert each node to a `pylock.toml`-style package.
391        let mut packages = Vec::with_capacity(resolution.graph.node_count());
392        for (node_index, node) in resolution.base_dists() {
393            let ResolvedDist::Installable { dist, version } = &node.dist else {
394                continue;
395            };
396            if omit.contains(dist.name()) {
397                continue;
398            }
399
400            // "The version MUST NOT be included when it cannot be guaranteed to be consistent with the code used (i.e. when a source tree is used)."
401            let version = version
402                .as_ref()
403                .filter(|_| !matches!(&**dist, Dist::Source(SourceDist::Directory(..))));
404
405            // Create a `pylock.toml`-style package.
406            let mut package = PylockTomlPackage {
407                name: dist.name().clone(),
408                version: version.cloned(),
409                marker: node.marker.pep508(),
410                requires_python: None,
411                dependencies: vec![],
412                index: None,
413                vcs: None,
414                directory: None,
415                archive: None,
416                sdist: None,
417                wheels: None,
418            };
419
420            match &**dist {
421                Dist::Built(BuiltDist::DirectUrl(dist)) => {
422                    package.archive = Some(PylockTomlArchive {
423                        url: Some((*dist.location).clone()),
424                        path: None,
425                        size: dist.size(),
426                        upload_time: None,
427                        subdirectory: None,
428                        hashes: Hashes::from(node.hashes.clone()),
429                    });
430                }
431                Dist::Built(BuiltDist::Path(dist)) => {
432                    let path = try_relative_to_if(
433                        &dist.install_path,
434                        install_path,
435                        !dist.url.was_given_absolute(),
436                    )
437                    .map(Box::<Path>::from)
438                    .unwrap_or_else(|_| dist.install_path.clone());
439                    package.archive = Some(PylockTomlArchive {
440                        url: None,
441                        path: Some(PortablePathBuf::from(path)),
442                        size: dist.size(),
443                        upload_time: None,
444                        subdirectory: None,
445                        hashes: Hashes::from(node.hashes.clone()),
446                    });
447                }
448                Dist::Built(BuiltDist::GitPath(_)) => {
449                    return Err(PylockTomlErrorKind::GitArchiveUnsupported(package.name));
450                }
451                Dist::Built(BuiltDist::Registry(dist)) => {
452                    package.wheels = Self::filter_and_convert_wheels(
453                        resolution,
454                        tags,
455                        &requires_python,
456                        node_index,
457                        &dist.wheels,
458                        build_options.no_binary_package(dist.name()),
459                    )?;
460
461                    // Filter sdist based on build options (--only-binary).
462                    let no_build = build_options.no_build_package(dist.name());
463
464                    if !no_build {
465                        if let Some(sdist) = dist.sdist.as_ref() {
466                            let url = sdist
467                                .file
468                                .url
469                                .to_url()
470                                .map_err(PylockTomlErrorKind::ToUrl)?;
471                            package.sdist = Some(PylockTomlSdist {
472                                // Optional "when the last component of path/ url would be the same value".
473                                name: if url
474                                    .filename()
475                                    .is_ok_and(|filename| filename == *sdist.file.filename)
476                                {
477                                    None
478                                } else {
479                                    Some(sdist.file.filename.clone())
480                                },
481                                upload_time: sdist
482                                    .file
483                                    .upload_time_utc_ms
484                                    .map(Timestamp::from_millisecond)
485                                    .transpose()?,
486                                url: Some(url),
487                                path: None,
488                                size: sdist.file.size,
489                                hashes: Hashes::from(sdist.file.hashes.clone()),
490                            });
491                        }
492                    }
493                }
494                Dist::Source(SourceDist::DirectUrl(dist)) => {
495                    package.archive = Some(PylockTomlArchive {
496                        url: Some((*dist.location).clone()),
497                        path: None,
498                        size: dist.size(),
499                        upload_time: None,
500                        subdirectory: dist.subdirectory.clone().map(PortablePathBuf::from),
501                        hashes: Hashes::from(node.hashes.clone()),
502                    });
503                }
504                Dist::Source(SourceDist::Directory(dist)) => {
505                    let path = try_relative_to_if(
506                        &dist.install_path,
507                        install_path,
508                        !dist.url.was_given_absolute(),
509                    )
510                    .map(Box::<Path>::from)
511                    .unwrap_or_else(|_| dist.install_path.clone());
512                    package.directory = Some(PylockTomlDirectory {
513                        path: PortablePathBuf::from(path),
514                        editable: dist.editable,
515                        subdirectory: None,
516                    });
517                }
518                Dist::Source(SourceDist::GitDirectory(dist)) => {
519                    package.vcs = Some(PylockTomlVcs {
520                        r#type: VcsKind::Git,
521                        url: Some(dist.git.url().clone()),
522                        path: None,
523                        requested_revision: dist.git.reference().as_str().map(ToString::to_string),
524                        commit_id: dist.git.precise().unwrap_or_else(|| {
525                            panic!("Git distribution is missing a precise hash: {dist}")
526                        }),
527                        subdirectory: dist.subdirectory.clone().map(PortablePathBuf::from),
528                    });
529                }
530                Dist::Source(SourceDist::GitPath(_)) => {
531                    return Err(PylockTomlErrorKind::GitArchiveUnsupported(package.name));
532                }
533                Dist::Source(SourceDist::Path(dist)) => {
534                    let path = try_relative_to_if(
535                        &dist.install_path,
536                        install_path,
537                        !dist.url.was_given_absolute(),
538                    )
539                    .map(Box::<Path>::from)
540                    .unwrap_or_else(|_| dist.install_path.clone());
541                    package.archive = Some(PylockTomlArchive {
542                        url: None,
543                        path: Some(PortablePathBuf::from(path)),
544                        size: dist.size(),
545                        upload_time: None,
546                        subdirectory: None,
547                        hashes: Hashes::from(node.hashes.clone()),
548                    });
549                }
550                Dist::Source(SourceDist::Registry(dist)) => {
551                    package.wheels = Self::filter_and_convert_wheels(
552                        resolution,
553                        tags,
554                        &requires_python,
555                        node_index,
556                        &dist.wheels,
557                        build_options.no_binary_package(&dist.name),
558                    )?;
559
560                    // Filter sdist based on build options (--only-binary).
561                    let no_build = build_options.no_build_package(&dist.name);
562
563                    if !no_build {
564                        let url = dist.file.url.to_url().map_err(PylockTomlErrorKind::ToUrl)?;
565                        package.sdist = Some(PylockTomlSdist {
566                            // Optional "when the last component of path/ url would be the same value".
567                            name: if url
568                                .filename()
569                                .is_ok_and(|filename| filename == *dist.file.filename)
570                            {
571                                None
572                            } else {
573                                Some(dist.file.filename.clone())
574                            },
575                            upload_time: dist
576                                .file
577                                .upload_time_utc_ms
578                                .map(Timestamp::from_millisecond)
579                                .transpose()?,
580                            url: Some(url),
581                            path: None,
582                            size: dist.file.size,
583                            hashes: Hashes::from(dist.file.hashes.clone()),
584                        });
585                    }
586                }
587            }
588
589            // Add the package to the list of packages.
590            packages.push(package);
591        }
592
593        // Sort the packages by name, then version.
594        packages.sort_by(|a, b| a.name.cmp(&b.name).then(a.version.cmp(&b.version)));
595
596        // Return the constructed `pylock.toml`.
597        Ok(Self {
598            lock_version,
599            created_by,
600            requires_python: Some(requires_python),
601            extras,
602            dependency_groups,
603            default_groups,
604            packages,
605            attestation_identities,
606        })
607    }
608
609    /// Filter wheels based on build options (--no-binary) and incompatible tags and return the
610    /// rest.
611    ///
612    /// Returns `Ok(None)` if no wheels are compatible.
613    fn filter_and_convert_wheels(
614        resolution: &ResolverOutput,
615        tags: Option<&Tags>,
616        requires_python: &RequiresPython,
617        node_index: NodeIndex,
618        wheels: &[RegistryBuiltWheel],
619        no_binary: bool,
620    ) -> Result<Option<Vec<PylockTomlWheel>>, PylockTomlErrorKind> {
621        if no_binary {
622            return Ok(None);
623        }
624
625        // Filter wheels based on tag compatibility and requires-python.
626        let wheels: Vec<_> = wheels
627            .iter()
628            .filter(|wheel| {
629                !is_wheel_unreachable(
630                    &wheel.filename,
631                    resolution,
632                    requires_python,
633                    node_index,
634                    tags,
635                )
636            })
637            .collect();
638
639        if wheels.is_empty() {
640            return Ok(None);
641        }
642
643        let wheels = wheels
644            .into_iter()
645            .map(|wheel| {
646                let url = wheel
647                    .file
648                    .url
649                    .to_url()
650                    .map_err(PylockTomlErrorKind::ToUrl)?;
651                Ok(PylockTomlWheel {
652                    // Optional "when the last component of path/ url would be the same value".
653                    name: if url
654                        .filename()
655                        .is_ok_and(|filename| filename == *wheel.file.filename)
656                    {
657                        None
658                    } else {
659                        Some(wheel.filename.clone())
660                    },
661                    upload_time: wheel
662                        .file
663                        .upload_time_utc_ms
664                        .map(Timestamp::from_millisecond)
665                        .transpose()?,
666                    url: Some(
667                        wheel
668                            .file
669                            .url
670                            .to_url()
671                            .map_err(PylockTomlErrorKind::ToUrl)?,
672                    ),
673                    path: None,
674                    size: wheel.file.size,
675                    hashes: Hashes::from(wheel.file.hashes.clone()),
676                })
677            })
678            .collect::<Result<Vec<_>, PylockTomlErrorKind>>()?;
679        Ok(Some(wheels))
680    }
681
682    /// Construct a [`PylockToml`] from a uv lockfile.
683    pub fn from_lock(
684        target: &impl Installable<'lock>,
685        prune: &[PackageName],
686        extras: &ExtrasSpecificationWithDefaults,
687        dev: &DependencyGroupsWithDefaults,
688        annotate: bool,
689        editable: Option<&EditableMode>,
690        install_options: &'lock InstallOptions,
691    ) -> Result<Self, PylockTomlErrorKind> {
692        // Extract the packages from the lock file.
693        let ExportableRequirements(mut nodes) = ExportableRequirements::from_lock(
694            target,
695            prune,
696            extras,
697            dev,
698            annotate,
699            install_options,
700        )?;
701
702        // Sort the nodes.
703        nodes.sort_unstable_by_key(|node| &node.package.id);
704
705        // The lock version is always `1.0` at time of writing.
706        let lock_version = Version::new([1, 0]);
707
708        // The created by field is always `uv` at time of writing.
709        let created_by = "uv".to_string();
710
711        // Use the `requires-python` from the target lockfile.
712        let requires_python = target.lock().requires_python.clone();
713
714        // We don't support locking for multiple extras at time of writing.
715        let extras = vec![];
716
717        // We don't support locking for multiple dependency groups at time of writing.
718        let dependency_groups = vec![];
719
720        // We don't support locking for multiple dependency groups at time of writing.
721        let default_groups = vec![];
722
723        // We don't support attestation identities at time of writing.
724        let attestation_identities = vec![];
725
726        // Convert each node to a `pylock.toml`-style package.
727        let mut packages = Vec::with_capacity(nodes.len());
728        for node in nodes {
729            let package = node.package;
730
731            // Extract the `packages.wheels` field.
732            //
733            // This field only includes wheels from a registry. Wheels included via direct URL or
734            // direct path instead map to the `packages.archive` field.
735            let wheels = match &package.id.source {
736                Source::Registry(source) => {
737                    let wheels = package
738                        .wheels
739                        .iter()
740                        .map(|wheel| wheel.to_registry_wheel(source, target.install_path()))
741                        .collect::<Result<Vec<RegistryBuiltWheel>, LockError>>()?;
742                    Some(
743                        wheels
744                            .into_iter()
745                            .map(|wheel| {
746                                let url = wheel
747                                    .file
748                                    .url
749                                    .to_url()
750                                    .map_err(PylockTomlErrorKind::ToUrl)?;
751                                Ok(PylockTomlWheel {
752                                    // Optional "when the last component of path/ url would be the same value".
753                                    name: if url
754                                        .filename()
755                                        .is_ok_and(|filename| filename == *wheel.file.filename)
756                                    {
757                                        None
758                                    } else {
759                                        Some(wheel.filename.clone())
760                                    },
761                                    upload_time: wheel
762                                        .file
763                                        .upload_time_utc_ms
764                                        .map(Timestamp::from_millisecond)
765                                        .transpose()?,
766                                    url: Some(url),
767                                    path: None,
768                                    size: wheel.file.size,
769                                    hashes: Hashes::from(wheel.file.hashes),
770                                })
771                            })
772                            .collect::<Result<Vec<_>, PylockTomlErrorKind>>()?,
773                    )
774                }
775                Source::Path(..) => None,
776                Source::Git(..) => None,
777                Source::Direct(..) => None,
778                Source::Directory(..) => None,
779                Source::Editable(..) => None,
780                Source::Virtual(..) => {
781                    // Omit virtual packages entirely; they shouldn't be installed.
782                    continue;
783                }
784            };
785
786            // Extract the source distribution from the lockfile entry.
787            let sdist = package.to_source_dist(target.install_path())?;
788
789            // Extract some common fields from the source distribution.
790            let size = package
791                .sdist
792                .as_ref()
793                .and_then(super::super::SourceDist::size);
794            let hash = package.sdist.as_ref().and_then(|sdist| sdist.hash());
795
796            // Extract the `packages.directory` field.
797            let directory = match &sdist {
798                Some(SourceDist::Directory(sdist)) => Some(PylockTomlDirectory {
799                    path: PortablePathBuf::from(
800                        sdist
801                            .url
802                            .given()
803                            .map(PathBuf::from)
804                            .unwrap_or_else(|| sdist.install_path.to_path_buf())
805                            .into_boxed_path(),
806                    ),
807                    editable: match editable
808                        .and_then(|editable| editable.for_package(&package.id.name))
809                    {
810                        None => sdist.editable,
811                        Some(false) => None,
812                        Some(true) => Some(true),
813                    },
814                    subdirectory: None,
815                }),
816                _ => None,
817            };
818
819            // Extract the `packages.vcs` field.
820            let vcs = match &sdist {
821                Some(SourceDist::GitDirectory(sdist)) => Some(PylockTomlVcs {
822                    r#type: VcsKind::Git,
823                    url: Some(sdist.git.url().clone()),
824                    path: None,
825                    requested_revision: sdist.git.reference().as_str().map(ToString::to_string),
826                    commit_id: sdist.git.precise().unwrap_or_else(|| {
827                        panic!("Git distribution is missing a precise hash: {sdist}")
828                    }),
829                    subdirectory: sdist.subdirectory.clone().map(PortablePathBuf::from),
830                }),
831                _ => None,
832            };
833
834            // Extract the `packages.archive` field, which can either be a direct URL or a local
835            // path, pointing to either a source distribution or a wheel.
836            let archive = match &sdist {
837                Some(SourceDist::DirectUrl(sdist)) => Some(PylockTomlArchive {
838                    url: Some(sdist.url.to_url()),
839                    path: None,
840                    size,
841                    upload_time: None,
842                    subdirectory: sdist.subdirectory.clone().map(PortablePathBuf::from),
843                    hashes: hash.cloned().map(Hashes::from).unwrap_or_default(),
844                }),
845                Some(SourceDist::Path(sdist)) => Some(PylockTomlArchive {
846                    url: None,
847                    path: Some(PortablePathBuf::from(
848                        sdist
849                            .url
850                            .given()
851                            .map(PathBuf::from)
852                            .unwrap_or_else(|| sdist.install_path.to_path_buf())
853                            .into_boxed_path(),
854                    )),
855                    size,
856                    upload_time: None,
857                    subdirectory: None,
858                    hashes: hash.cloned().map(Hashes::from).unwrap_or_default(),
859                }),
860                _ => match &package.id.source {
861                    Source::Registry(..) => None,
862                    Source::Path(source) => package.wheels.first().map(|wheel| PylockTomlArchive {
863                        url: None,
864                        path: Some(PortablePathBuf::from(source.clone())),
865                        size: wheel.size,
866                        upload_time: None,
867                        subdirectory: None,
868                        hashes: wheel.hash.clone().map(Hashes::from).unwrap_or_default(),
869                    }),
870                    Source::Git(..) => None,
871                    Source::Direct(source, ..) => {
872                        if let Some(wheel) = package.wheels.first() {
873                            Some(PylockTomlArchive {
874                                url: Some(source.to_url()?),
875                                path: None,
876                                size: wheel.size,
877                                upload_time: None,
878                                subdirectory: None,
879                                hashes: wheel.hash.clone().map(Hashes::from).unwrap_or_default(),
880                            })
881                        } else {
882                            None
883                        }
884                    }
885                    Source::Directory(..) => None,
886                    Source::Editable(..) => None,
887                    Source::Virtual(..) => None,
888                },
889            };
890
891            // Extract the `packages.sdist` field.
892            let sdist = match &sdist {
893                Some(SourceDist::Registry(sdist)) => {
894                    let url = sdist
895                        .file
896                        .url
897                        .to_url()
898                        .map_err(PylockTomlErrorKind::ToUrl)?;
899                    Some(PylockTomlSdist {
900                        // Optional "when the last component of path/ url would be the same value".
901                        name: if url
902                            .filename()
903                            .is_ok_and(|filename| filename == *sdist.file.filename)
904                        {
905                            None
906                        } else {
907                            Some(sdist.file.filename.clone())
908                        },
909                        upload_time: sdist
910                            .file
911                            .upload_time_utc_ms
912                            .map(Timestamp::from_millisecond)
913                            .transpose()?,
914                        url: Some(url),
915                        path: None,
916                        size,
917                        hashes: hash.cloned().map(Hashes::from).unwrap_or_default(),
918                    })
919                }
920                _ => None,
921            };
922
923            // Extract the `packages.index` field.
924            let index = package
925                .index(target.install_path())?
926                .map(IndexUrl::into_url);
927
928            // Extract the `packages.name` field.
929            let name = package.id.name.clone();
930
931            // Extract the `packages.version` field.
932            // "The version MUST NOT be included when it cannot be guaranteed to be consistent with the code used (i.e. when a source tree is used)."
933            let version = package
934                .id
935                .version
936                .as_ref()
937                .filter(|_| directory.is_none())
938                .cloned();
939
940            let package = PylockTomlPackage {
941                name,
942                version,
943                marker: node.marker,
944                requires_python: None,
945                dependencies: vec![],
946                index,
947                vcs,
948                directory,
949                archive,
950                sdist,
951                wheels,
952            };
953
954            packages.push(package);
955        }
956
957        Ok(Self {
958            lock_version,
959            created_by,
960            requires_python: Some(requires_python),
961            extras,
962            dependency_groups,
963            default_groups,
964            packages,
965            attestation_identities,
966        })
967    }
968
969    /// Returns the TOML representation of this lockfile.
970    pub fn to_toml(&self) -> Result<String, toml_edit::ser::Error> {
971        // We construct a TOML document manually instead of going through Serde to enable
972        // the use of inline tables.
973        let mut doc = toml_edit::DocumentMut::new();
974
975        doc.insert("lock-version", value(self.lock_version.to_string()));
976        doc.insert("created-by", value(self.created_by.as_str()));
977        if let Some(ref requires_python) = self.requires_python {
978            doc.insert("requires-python", value(requires_python.to_string()));
979        }
980        if !self.extras.is_empty() {
981            doc.insert(
982                "extras",
983                value(each_element_on_its_line_array(
984                    self.extras.iter().map(ToString::to_string),
985                )),
986            );
987        }
988        if !self.dependency_groups.is_empty() {
989            doc.insert(
990                "dependency-groups",
991                value(each_element_on_its_line_array(
992                    self.dependency_groups.iter().map(ToString::to_string),
993                )),
994            );
995        }
996        if !self.default_groups.is_empty() {
997            doc.insert(
998                "default-groups",
999                value(each_element_on_its_line_array(
1000                    self.default_groups.iter().map(ToString::to_string),
1001                )),
1002            );
1003        }
1004        if !self.attestation_identities.is_empty() {
1005            let attestation_identities = self
1006                .attestation_identities
1007                .iter()
1008                .map(|attestation_identity| {
1009                    serde::Serialize::serialize(
1010                        &attestation_identity,
1011                        toml_edit::ser::ValueSerializer::new(),
1012                    )
1013                })
1014                .collect::<Result<Vec<_>, _>>()?;
1015            let attestation_identities = match attestation_identities.as_slice() {
1016                [] => Array::new(),
1017                [attestation_identity] => Array::from_iter([attestation_identity]),
1018                attestation_identities => {
1019                    each_element_on_its_line_array(attestation_identities.iter())
1020                }
1021            };
1022            doc.insert("attestation-identities", value(attestation_identities));
1023        }
1024        if !self.packages.is_empty() {
1025            let mut packages = ArrayOfTables::new();
1026            for dist in &self.packages {
1027                packages.push(dist.to_toml()?);
1028            }
1029            doc.insert("packages", Item::ArrayOfTables(packages));
1030        }
1031
1032        Ok(doc.to_string())
1033    }
1034
1035    /// Convert the [`PylockToml`] to a [`Resolution`].
1036    pub fn to_resolution(
1037        self,
1038        install_path: &Path,
1039        markers: &MarkerEnvironment,
1040        extras: &[ExtraName],
1041        groups: &[GroupName],
1042        tags: &Tags,
1043        build_options: &BuildOptions,
1044    ) -> Result<Resolution, PylockTomlError> {
1045        // Convert the extras and dependency groups specifications to a concrete environment.
1046        let mut graph =
1047            petgraph::graph::DiGraph::with_capacity(self.packages.len(), self.packages.len());
1048
1049        // Add the root node.
1050        let root = graph.add_node(Node::Root);
1051
1052        for package in self.packages {
1053            // Omit packages that aren't relevant to the current environment.
1054            if !package.marker.evaluate_pep751(markers, extras, groups) {
1055                continue;
1056            }
1057
1058            if let Some(requires_python) = package.requires_python.as_ref()
1059                && !requires_python.contains(&markers.python_full_version().version)
1060            {
1061                return Err(PylockTomlErrorKind::IncompatibleRequiresPython(
1062                    package.name.clone(),
1063                    markers.python_full_version().version.clone(),
1064                    requires_python.clone(),
1065                )
1066                .into());
1067            }
1068
1069            match (
1070                package.wheels.is_some(),
1071                package.sdist.is_some(),
1072                package.directory.is_some(),
1073                package.vcs.is_some(),
1074                package.archive.is_some(),
1075            ) {
1076                // `packages.wheels` is mutually exclusive with `packages.directory`, `packages.vcs`, and `packages.archive`.
1077                (true, _, true, _, _) => {
1078                    return Err(
1079                        PylockTomlErrorKind::WheelWithDirectory(package.name.clone()).into(),
1080                    );
1081                }
1082                (true, _, _, true, _) => {
1083                    return Err(PylockTomlErrorKind::WheelWithVcs(package.name.clone()).into());
1084                }
1085                (true, _, _, _, true) => {
1086                    return Err(PylockTomlErrorKind::WheelWithArchive(package.name.clone()).into());
1087                }
1088                // `packages.sdist` is mutually exclusive with `packages.directory`, `packages.vcs`, and `packages.archive`.
1089                (_, true, true, _, _) => {
1090                    return Err(
1091                        PylockTomlErrorKind::SdistWithDirectory(package.name.clone()).into(),
1092                    );
1093                }
1094                (_, true, _, true, _) => {
1095                    return Err(PylockTomlErrorKind::SdistWithVcs(package.name.clone()).into());
1096                }
1097                (_, true, _, _, true) => {
1098                    return Err(PylockTomlErrorKind::SdistWithArchive(package.name.clone()).into());
1099                }
1100                // `packages.directory` is mutually exclusive with `packages.vcs`, and `packages.archive`.
1101                (_, _, true, true, _) => {
1102                    return Err(PylockTomlErrorKind::DirectoryWithVcs(package.name.clone()).into());
1103                }
1104                (_, _, true, _, true) => {
1105                    return Err(
1106                        PylockTomlErrorKind::DirectoryWithArchive(package.name.clone()).into(),
1107                    );
1108                }
1109                // `packages.vcs` is mutually exclusive with `packages.archive`.
1110                (_, _, _, true, true) => {
1111                    return Err(PylockTomlErrorKind::VcsWithArchive(package.name.clone()).into());
1112                }
1113                (false, false, false, false, false) => {
1114                    return Err(PylockTomlErrorKind::MissingSource(package.name.clone()).into());
1115                }
1116                _ => {}
1117            }
1118
1119            let no_binary = build_options.no_binary_package(&package.name);
1120            let no_build = build_options.no_build_package(&package.name);
1121            let is_wheel = package
1122                .archive
1123                .as_ref()
1124                .map(|archive| archive.is_wheel(&package.name))
1125                .transpose()?
1126                .unwrap_or_default();
1127
1128            // Search for a matching wheel.
1129            let dist = if let Some(best_wheel) =
1130                package.find_best_wheel(tags).filter(|_| !no_binary)
1131            {
1132                let hashes = HashDigests::from(best_wheel.hashes.clone());
1133                let built_dist = Dist::Built(BuiltDist::Registry(RegistryBuiltDist {
1134                    wheels: vec![best_wheel.to_registry_wheel(
1135                        install_path,
1136                        &package.name,
1137                        package.index.as_ref(),
1138                    )?],
1139                    best_wheel_index: 0,
1140                    sdist: None,
1141                }));
1142                let dist = ResolvedDist::Installable {
1143                    dist: Arc::new(built_dist),
1144                    version: package.version,
1145                };
1146                Node::Dist {
1147                    dist,
1148                    hashes,
1149                    install: true,
1150                }
1151            } else if let Some(sdist) = package.sdist.as_ref().filter(|_| !no_build) {
1152                let hashes = HashDigests::from(sdist.hashes.clone());
1153                let sdist = Dist::Source(SourceDist::Registry(sdist.to_sdist(
1154                    install_path,
1155                    &package.name,
1156                    package.version.as_ref(),
1157                    package.index.as_ref(),
1158                )?));
1159                let dist = ResolvedDist::Installable {
1160                    dist: Arc::new(sdist),
1161                    version: package.version,
1162                };
1163                Node::Dist {
1164                    dist,
1165                    hashes,
1166                    install: true,
1167                }
1168            } else if let Some(sdist) = package.directory.as_ref().filter(|_| !no_build) {
1169                let hashes = HashDigests::empty();
1170                let sdist = Dist::Source(SourceDist::Directory(
1171                    sdist.to_sdist(install_path, &package.name)?,
1172                ));
1173                let dist = ResolvedDist::Installable {
1174                    dist: Arc::new(sdist),
1175                    version: package.version,
1176                };
1177                Node::Dist {
1178                    dist,
1179                    hashes,
1180                    install: true,
1181                }
1182            } else if let Some(sdist) = package.vcs.as_ref().filter(|_| !no_build) {
1183                let hashes = HashDigests::empty();
1184                let sdist = Dist::Source(SourceDist::GitDirectory(
1185                    sdist.to_sdist(install_path, &package.name)?,
1186                ));
1187                let dist = ResolvedDist::Installable {
1188                    dist: Arc::new(sdist),
1189                    version: package.version,
1190                };
1191                Node::Dist {
1192                    dist,
1193                    hashes,
1194                    install: true,
1195                }
1196            } else if let Some(dist) = package
1197                .archive
1198                .as_ref()
1199                .filter(|_| if is_wheel { !no_binary } else { !no_build })
1200            {
1201                let hashes = HashDigests::from(dist.hashes.clone());
1202                let dist = dist.to_dist(install_path, &package.name, package.version.as_ref())?;
1203                let dist = ResolvedDist::Installable {
1204                    dist: Arc::new(dist),
1205                    version: package.version,
1206                };
1207                Node::Dist {
1208                    dist,
1209                    hashes,
1210                    install: true,
1211                }
1212            } else {
1213                return match (no_binary, no_build) {
1214                    (true, true) => {
1215                        Err(PylockTomlErrorKind::NoBinaryNoBuild(package.name.clone()).into())
1216                    }
1217                    (true, false) if is_wheel => {
1218                        Err(PylockTomlErrorKind::NoBinaryWheelOnly(package.name.clone()).into())
1219                    }
1220                    (true, false) => {
1221                        Err(PylockTomlErrorKind::NoBinary(package.name.clone()).into())
1222                    }
1223                    (false, true) => Err(PylockTomlErrorKind::NoBuild(package.name.clone()).into()),
1224                    (false, false) if is_wheel => Err(PylockTomlError {
1225                        kind: Box::new(PylockTomlErrorKind::IncompatibleWheelOnly(
1226                            package.name.clone(),
1227                        )),
1228                        hint: package.tag_hint(tags, markers),
1229                    }),
1230                    (false, false) => Err(PylockTomlError {
1231                        kind: Box::new(PylockTomlErrorKind::NeitherSourceDistNorWheel(
1232                            package.name.clone(),
1233                        )),
1234                        hint: package.tag_hint(tags, markers),
1235                    }),
1236                };
1237            };
1238
1239            let index = graph.add_node(dist);
1240            graph.add_edge(root, index, Edge::Prod);
1241        }
1242
1243        Ok(Resolution::new(graph))
1244    }
1245}
1246
1247impl PylockTomlPackage {
1248    /// Convert the [`PylockTomlPackage`] to a TOML [`Table`].
1249    fn to_toml(&self) -> Result<Table, toml_edit::ser::Error> {
1250        let mut table = Table::new();
1251        table.insert("name", value(self.name.to_string()));
1252        if let Some(ref version) = self.version {
1253            table.insert("version", value(version.to_string()));
1254        }
1255        if let Some(marker) = self.marker.try_to_string() {
1256            table.insert("marker", value(marker));
1257        }
1258        if let Some(ref requires_python) = self.requires_python {
1259            table.insert("requires-python", value(requires_python.to_string()));
1260        }
1261        if !self.dependencies.is_empty() {
1262            let dependencies = self
1263                .dependencies
1264                .iter()
1265                .map(|dependency| {
1266                    serde::Serialize::serialize(&dependency, toml_edit::ser::ValueSerializer::new())
1267                })
1268                .collect::<Result<Vec<_>, _>>()?;
1269            let dependencies = match dependencies.as_slice() {
1270                [] => Array::new(),
1271                [dependency] => Array::from_iter([dependency]),
1272                dependencies => each_element_on_its_line_array(dependencies.iter()),
1273            };
1274            table.insert("dependencies", value(dependencies));
1275        }
1276        if let Some(ref index) = self.index {
1277            table.insert("index", value(index.to_string()));
1278        }
1279        if let Some(ref vcs) = self.vcs {
1280            table.insert(
1281                "vcs",
1282                value(serde::Serialize::serialize(
1283                    &vcs,
1284                    toml_edit::ser::ValueSerializer::new(),
1285                )?),
1286            );
1287        }
1288        if let Some(ref directory) = self.directory {
1289            table.insert(
1290                "directory",
1291                value(serde::Serialize::serialize(
1292                    &directory,
1293                    toml_edit::ser::ValueSerializer::new(),
1294                )?),
1295            );
1296        }
1297        if let Some(ref archive) = self.archive {
1298            table.insert(
1299                "archive",
1300                value(serde::Serialize::serialize(
1301                    &archive,
1302                    toml_edit::ser::ValueSerializer::new(),
1303                )?),
1304            );
1305        }
1306        if let Some(ref sdist) = self.sdist {
1307            table.insert(
1308                "sdist",
1309                value(serde::Serialize::serialize(
1310                    &sdist,
1311                    toml_edit::ser::ValueSerializer::new(),
1312                )?),
1313            );
1314        }
1315        if let Some(wheels) = self.wheels.as_ref().filter(|wheels| !wheels.is_empty()) {
1316            let wheels = wheels
1317                .iter()
1318                .map(|wheel| {
1319                    serde::Serialize::serialize(wheel, toml_edit::ser::ValueSerializer::new())
1320                })
1321                .collect::<Result<Vec<_>, _>>()?;
1322            let wheels = match wheels.as_slice() {
1323                [] => Array::new(),
1324                [wheel] => Array::from_iter([wheel]),
1325                wheels => each_element_on_its_line_array(wheels.iter()),
1326            };
1327            table.insert("wheels", value(wheels));
1328        }
1329
1330        Ok(table)
1331    }
1332
1333    /// Return the index of the best wheel for the given tags.
1334    fn find_best_wheel(&self, tags: &Tags) -> Option<&PylockTomlWheel> {
1335        type WheelPriority = (TagPriority, Option<BuildTag>);
1336
1337        let mut best: Option<(WheelPriority, &PylockTomlWheel)> = None;
1338        for wheel in self.wheels.iter().flatten() {
1339            let Ok(filename) = wheel.filename(&self.name) else {
1340                continue;
1341            };
1342            let TagCompatibility::Compatible(tag_priority) = filename.compatibility(tags) else {
1343                continue;
1344            };
1345            let build_tag = filename.build_tag().cloned();
1346            let wheel_priority = (tag_priority, build_tag);
1347            match &best {
1348                None => {
1349                    best = Some((wheel_priority, wheel));
1350                }
1351                Some((best_priority, _)) => {
1352                    if wheel_priority > *best_priority {
1353                        best = Some((wheel_priority, wheel));
1354                    }
1355                }
1356            }
1357        }
1358
1359        best.map(|(_, i)| i)
1360    }
1361
1362    /// Generate a [`WheelTagHint`] based on wheel-tag incompatibilities.
1363    fn tag_hint(&self, tags: &Tags, markers: &MarkerEnvironment) -> Option<WheelTagHint> {
1364        let filenames = self
1365            .wheels
1366            .iter()
1367            .flatten()
1368            .filter_map(|wheel| wheel.filename(&self.name).ok())
1369            .collect::<Vec<_>>();
1370        let filenames = filenames.iter().map(Cow::as_ref).collect::<Vec<_>>();
1371        WheelTagHint::from_wheels(&self.name, self.version.as_ref(), &filenames, tags, markers)
1372    }
1373
1374    /// Returns the [`ResolvedRepositoryReference`] for the package, if it is a Git source.
1375    pub fn as_git_ref(&self) -> Option<ResolvedRepositoryReference> {
1376        let vcs = self.vcs.as_ref()?;
1377        let url = vcs.url.as_ref()?;
1378        let reference = match vcs.requested_revision.as_ref() {
1379            Some(rev) => GitReference::from_rev(rev.clone()),
1380            None => GitReference::DefaultBranch,
1381        };
1382        Some(ResolvedRepositoryReference {
1383            reference: RepositoryReference {
1384                url: RepositoryUrl::new(url),
1385                reference,
1386            },
1387            sha: vcs.commit_id,
1388        })
1389    }
1390}
1391
1392impl PylockTomlWheel {
1393    /// Return the [`WheelFilename`] for this wheel.
1394    fn filename(&self, name: &PackageName) -> Result<Cow<'_, WheelFilename>, PylockTomlErrorKind> {
1395        if let Some(name) = self.name.as_ref() {
1396            Ok(Cow::Borrowed(name))
1397        } else if let Some(path) = self.path.as_ref() {
1398            let Some(filename) = path.as_ref().file_name().and_then(OsStr::to_str) else {
1399                return Err(PylockTomlErrorKind::PathMissingFilename(Box::<Path>::from(
1400                    path.clone(),
1401                )));
1402            };
1403            let filename = WheelFilename::from_str(filename).map(Cow::Owned)?;
1404            Ok(filename)
1405        } else if let Some(url) = self.url.as_ref() {
1406            let Some(filename) = url.filename().ok() else {
1407                return Err(PylockTomlErrorKind::UrlMissingFilename(url.clone()));
1408            };
1409            let filename = WheelFilename::from_str(&filename).map(Cow::Owned)?;
1410            Ok(filename)
1411        } else {
1412            Err(PylockTomlErrorKind::WheelMissingPathUrl(name.clone()))
1413        }
1414    }
1415
1416    /// Convert the wheel to a [`RegistryBuiltWheel`].
1417    fn to_registry_wheel(
1418        &self,
1419        install_path: &Path,
1420        name: &PackageName,
1421        index: Option<&DisplaySafeUrl>,
1422    ) -> Result<RegistryBuiltWheel, PylockTomlErrorKind> {
1423        let filename = self.filename(name)?.into_owned();
1424
1425        let file_url = if let Some(url) = self.url.as_ref() {
1426            UrlString::from(url)
1427        } else if let Some(path) = self.path.as_ref() {
1428            let path = install_path.join(path);
1429            let url = DisplaySafeUrl::from_file_path(path)
1430                .map_err(|()| PylockTomlErrorKind::PathToUrl)?;
1431            UrlString::from(url)
1432        } else {
1433            return Err(PylockTomlErrorKind::WheelMissingPathUrl(name.clone()));
1434        };
1435
1436        let index = if let Some(index) = index {
1437            IndexUrl::from(VerbatimUrl::from_url(index.clone()))
1438        } else {
1439            // Including the index is only a SHOULD in PEP 751. If it's omitted, we treat the
1440            // URL (less the filename) as the index. This isn't correct, but it's the best we can
1441            // do. In practice, the only effect here should be that we cache the wheel under a hash
1442            // of this URL (since we cache under the hash of the index).
1443            let mut index = file_url.to_url().map_err(PylockTomlErrorKind::ToUrl)?;
1444            index.path_segments_mut().unwrap().pop();
1445            IndexUrl::from(VerbatimUrl::from_url(index))
1446        };
1447
1448        let file = Box::new(uv_distribution_types::File {
1449            dist_info_metadata: false,
1450            filename: SmallString::from(filename.to_string()),
1451            hashes: HashDigests::from(self.hashes.clone()),
1452            requires_python: None,
1453            size: self.size,
1454            upload_time_utc_ms: self.upload_time.map(Timestamp::as_millisecond),
1455            url: FileLocation::AbsoluteUrl(file_url),
1456            yanked: None,
1457            zstd: None,
1458        });
1459
1460        Ok(RegistryBuiltWheel {
1461            filename,
1462            file,
1463            index,
1464        })
1465    }
1466}
1467
1468impl PylockTomlDirectory {
1469    /// Convert the sdist to a [`DirectorySourceDist`].
1470    fn to_sdist(
1471        &self,
1472        install_path: &Path,
1473        name: &PackageName,
1474    ) -> Result<DirectorySourceDist, PylockTomlErrorKind> {
1475        let path = if let Some(subdirectory) = self.subdirectory.as_ref() {
1476            install_path.join(&self.path).join(subdirectory)
1477        } else {
1478            install_path.join(&self.path)
1479        };
1480        let path = normalize_path(path);
1481        let url =
1482            VerbatimUrl::from_normalized_path(&path).map_err(|_| PylockTomlErrorKind::PathToUrl)?;
1483        Ok(DirectorySourceDist {
1484            name: name.clone(),
1485            install_path: path.into_owned().into_boxed_path(),
1486            editable: self.editable,
1487            r#virtual: Some(false),
1488            url,
1489        })
1490    }
1491}
1492
1493impl PylockTomlVcs {
1494    /// Convert the sdist to a [`GitDirectorySourceDist`].
1495    fn to_sdist(
1496        &self,
1497        install_path: &Path,
1498        name: &PackageName,
1499    ) -> Result<GitDirectorySourceDist, PylockTomlErrorKind> {
1500        let subdirectory = self.subdirectory.clone().map(Box::<Path>::from);
1501
1502        // Reconstruct the `GitUrl` from the individual fields.
1503        let git_url = {
1504            let mut url = if let Some(url) = self.url.as_ref() {
1505                url.clone()
1506            } else if let Some(path) = self.path.as_ref() {
1507                DisplaySafeUrl::from_url(
1508                    Url::from_directory_path(install_path.join(path))
1509                        .map_err(|()| PylockTomlErrorKind::PathToUrl)?,
1510                )
1511            } else {
1512                return Err(PylockTomlErrorKind::VcsMissingPathUrl(name.clone()));
1513            };
1514            url.set_fragment(None);
1515            url.set_query(None);
1516
1517            let reference = self
1518                .requested_revision
1519                .clone()
1520                .map(GitReference::from_rev)
1521                .unwrap_or_else(|| GitReference::BranchOrTagOrCommit(self.commit_id.to_string()));
1522            let precise = self.commit_id;
1523
1524            // TODO(samypr100): GitLfs::from_env() as pylock.toml spec doesn't specify how to label LFS support
1525            GitUrl::from_commit(url, reference, precise, GitLfs::from_env())?
1526        };
1527
1528        // Reconstruct the PEP 508-compatible URL from the `GitSource`.
1529        let url = DisplaySafeUrl::from(ParsedGitDirectoryUrl {
1530            url: git_url.clone(),
1531            subdirectory: subdirectory.clone(),
1532        });
1533
1534        Ok(GitDirectorySourceDist {
1535            name: name.clone(),
1536            git: Box::new(git_url),
1537            subdirectory: self.subdirectory.clone().map(Box::<Path>::from),
1538            url: VerbatimUrl::from_url(url),
1539        })
1540    }
1541}
1542
1543impl PylockTomlSdist {
1544    /// Return the filename for this sdist.
1545    fn filename(&self, name: &PackageName) -> Result<Cow<'_, SmallString>, PylockTomlErrorKind> {
1546        if let Some(name) = self.name.as_ref() {
1547            Ok(Cow::Borrowed(name))
1548        } else if let Some(path) = self.path.as_ref() {
1549            let Some(filename) = path.as_ref().file_name().and_then(OsStr::to_str) else {
1550                return Err(PylockTomlErrorKind::PathMissingFilename(Box::<Path>::from(
1551                    path.clone(),
1552                )));
1553            };
1554            Ok(Cow::Owned(SmallString::from(filename)))
1555        } else if let Some(url) = self.url.as_ref() {
1556            let Some(filename) = url.filename().ok() else {
1557                return Err(PylockTomlErrorKind::UrlMissingFilename(url.clone()));
1558            };
1559            Ok(Cow::Owned(SmallString::from(filename)))
1560        } else {
1561            Err(PylockTomlErrorKind::SdistMissingPathUrl(name.clone()))
1562        }
1563    }
1564
1565    /// Convert the sdist to a [`RegistrySourceDist`].
1566    fn to_sdist(
1567        &self,
1568        install_path: &Path,
1569        name: &PackageName,
1570        version: Option<&Version>,
1571        index: Option<&DisplaySafeUrl>,
1572    ) -> Result<RegistrySourceDist, PylockTomlErrorKind> {
1573        let filename = self.filename(name)?.into_owned();
1574        let ext = SourceDistExtension::from_path(filename.as_ref())?;
1575
1576        let version = if let Some(version) = version {
1577            Cow::Borrowed(version)
1578        } else {
1579            let filename = SourceDistFilename::parse(&filename, ext, name)?;
1580            Cow::Owned(filename.version)
1581        };
1582
1583        let file_url = if let Some(url) = self.url.as_ref() {
1584            UrlString::from(url)
1585        } else if let Some(path) = self.path.as_ref() {
1586            let path = install_path.join(path);
1587            let url = DisplaySafeUrl::from_file_path(path)
1588                .map_err(|()| PylockTomlErrorKind::PathToUrl)?;
1589            UrlString::from(url)
1590        } else {
1591            return Err(PylockTomlErrorKind::SdistMissingPathUrl(name.clone()));
1592        };
1593
1594        let index = if let Some(index) = index {
1595            IndexUrl::from(VerbatimUrl::from_url(index.clone()))
1596        } else {
1597            // Including the index is only a SHOULD in PEP 751. If it's omitted, we treat the
1598            // URL (less the filename) as the index. This isn't correct, but it's the best we can
1599            // do. In practice, the only effect here should be that we cache the sdist under a hash
1600            // of this URL (since we cache under the hash of the index).
1601            let mut index = file_url.to_url().map_err(PylockTomlErrorKind::ToUrl)?;
1602            index.path_segments_mut().unwrap().pop();
1603            IndexUrl::from(VerbatimUrl::from_url(index))
1604        };
1605
1606        let file = Box::new(uv_distribution_types::File {
1607            dist_info_metadata: false,
1608            filename,
1609            hashes: HashDigests::from(self.hashes.clone()),
1610            requires_python: None,
1611            size: self.size,
1612            upload_time_utc_ms: self.upload_time.map(Timestamp::as_millisecond),
1613            url: FileLocation::AbsoluteUrl(file_url),
1614            yanked: None,
1615            zstd: None,
1616        });
1617
1618        Ok(RegistrySourceDist {
1619            name: name.clone(),
1620            version: version.into_owned(),
1621            file,
1622            ext,
1623            index,
1624            wheels: vec![],
1625        })
1626    }
1627}
1628
1629impl PylockTomlArchive {
1630    fn to_dist(
1631        &self,
1632        install_path: &Path,
1633        name: &PackageName,
1634        version: Option<&Version>,
1635    ) -> Result<Dist, PylockTomlErrorKind> {
1636        if let Some(url) = self.url.as_ref() {
1637            let filename = url
1638                .filename()
1639                .map_err(|_| PylockTomlErrorKind::UrlMissingFilename(url.clone()))?;
1640
1641            let ext = DistExtension::from_path(filename.as_ref())?;
1642            match ext {
1643                DistExtension::Wheel => {
1644                    let filename = WheelFilename::from_str(&filename)?;
1645                    Ok(Dist::Built(BuiltDist::DirectUrl(DirectUrlBuiltDist {
1646                        filename,
1647                        location: Box::new(url.clone()),
1648                        url: VerbatimUrl::from_url(url.clone()),
1649                    })))
1650                }
1651                DistExtension::Source(ext) => {
1652                    Ok(Dist::Source(SourceDist::DirectUrl(DirectUrlSourceDist {
1653                        name: name.clone(),
1654                        location: Box::new(url.clone()),
1655                        subdirectory: self.subdirectory.clone().map(Box::<Path>::from),
1656                        ext,
1657                        url: VerbatimUrl::from_url(url.clone()),
1658                    })))
1659                }
1660            }
1661        } else if let Some(path) = self.path.as_ref() {
1662            let filename = path
1663                .as_ref()
1664                .file_name()
1665                .and_then(OsStr::to_str)
1666                .ok_or_else(|| {
1667                    PylockTomlErrorKind::PathMissingFilename(Box::<Path>::from(path.clone()))
1668                })?;
1669
1670            let ext = DistExtension::from_path(filename)?;
1671            match ext {
1672                DistExtension::Wheel => {
1673                    let filename = WheelFilename::from_str(filename)?;
1674                    let install_path = install_path.join(path);
1675                    let url = VerbatimUrl::from_absolute_path(&install_path)
1676                        .map_err(|_| PylockTomlErrorKind::PathToUrl)?;
1677                    Ok(Dist::Built(BuiltDist::Path(PathBuiltDist {
1678                        filename,
1679                        install_path: install_path.into_boxed_path(),
1680                        url,
1681                    })))
1682                }
1683                DistExtension::Source(ext) => {
1684                    let install_path = install_path.join(path);
1685                    let url = VerbatimUrl::from_absolute_path(&install_path)
1686                        .map_err(|_| PylockTomlErrorKind::PathToUrl)?;
1687                    Ok(Dist::Source(SourceDist::Path(PathSourceDist {
1688                        name: name.clone(),
1689                        version: version.cloned(),
1690                        install_path: install_path.into_boxed_path(),
1691                        ext,
1692                        url,
1693                    })))
1694                }
1695            }
1696        } else {
1697            Err(PylockTomlErrorKind::ArchiveMissingPathUrl(name.clone()))
1698        }
1699    }
1700
1701    /// Returns `true` if the [`PylockTomlArchive`] is a wheel.
1702    fn is_wheel(&self, name: &PackageName) -> Result<bool, PylockTomlErrorKind> {
1703        if let Some(url) = self.url.as_ref() {
1704            let filename = url
1705                .filename()
1706                .map_err(|_| PylockTomlErrorKind::UrlMissingFilename(url.clone()))?;
1707
1708            let ext = DistExtension::from_path(filename.as_ref())?;
1709            Ok(matches!(ext, DistExtension::Wheel))
1710        } else if let Some(path) = self.path.as_ref() {
1711            let filename = path
1712                .as_ref()
1713                .file_name()
1714                .and_then(OsStr::to_str)
1715                .ok_or_else(|| {
1716                    PylockTomlErrorKind::PathMissingFilename(Box::<Path>::from(path.clone()))
1717                })?;
1718
1719            let ext = DistExtension::from_path(filename)?;
1720            Ok(matches!(ext, DistExtension::Wheel))
1721        } else {
1722            Err(PylockTomlErrorKind::ArchiveMissingPathUrl(name.clone()))
1723        }
1724    }
1725}
1726
1727/// Convert a Jiff timestamp to a TOML datetime.
1728#[expect(clippy::ref_option)]
1729fn timestamp_to_toml_datetime<S>(
1730    timestamp: &Option<Timestamp>,
1731    serializer: S,
1732) -> Result<S::Ok, S::Error>
1733where
1734    S: serde::Serializer,
1735{
1736    let Some(timestamp) = timestamp else {
1737        return serializer.serialize_none();
1738    };
1739    let timestamp = timestamp.to_zoned(TimeZone::UTC);
1740    let timestamp = toml_edit::Datetime {
1741        date: Some(toml_edit::Date {
1742            year: u16::try_from(timestamp.year()).map_err(serde::ser::Error::custom)?,
1743            month: u8::try_from(timestamp.month()).map_err(serde::ser::Error::custom)?,
1744            day: u8::try_from(timestamp.day()).map_err(serde::ser::Error::custom)?,
1745        }),
1746        time: Some(toml_edit::Time {
1747            hour: u8::try_from(timestamp.hour()).map_err(serde::ser::Error::custom)?,
1748            minute: u8::try_from(timestamp.minute()).map_err(serde::ser::Error::custom)?,
1749            second: Some(u8::try_from(timestamp.second()).map_err(serde::ser::Error::custom)?),
1750            nanosecond: {
1751                let nanos =
1752                    u32::try_from(timestamp.nanosecond()).map_err(serde::ser::Error::custom)?;
1753                if nanos == 0 { None } else { Some(nanos) }
1754            },
1755        }),
1756        offset: Some(toml_edit::Offset::Z),
1757    };
1758    serializer.serialize_some(&timestamp)
1759}
1760
1761/// Convert a TOML datetime to a Jiff timestamp.
1762fn timestamp_from_toml_datetime<'de, D>(deserializer: D) -> Result<Option<Timestamp>, D::Error>
1763where
1764    D: serde::Deserializer<'de>,
1765{
1766    let Some(datetime) = Option::<toml_edit::Datetime>::deserialize(deserializer)? else {
1767        return Ok(None);
1768    };
1769
1770    // If the date is omitted, we can't parse the datetime.
1771    let Some(date) = datetime.date else {
1772        return Err(serde::de::Error::custom("missing date"));
1773    };
1774
1775    let year = i16::try_from(date.year).map_err(serde::de::Error::custom)?;
1776    let month = i8::try_from(date.month).map_err(serde::de::Error::custom)?;
1777    let day = i8::try_from(date.day).map_err(serde::de::Error::custom)?;
1778    let date = Date::new(year, month, day).map_err(serde::de::Error::custom)?;
1779
1780    // If the timezone is omitted, assume UTC.
1781    let tz = if let Some(offset) = datetime.offset {
1782        match offset {
1783            toml_edit::Offset::Z => TimeZone::UTC,
1784            toml_edit::Offset::Custom { minutes } => {
1785                let hours = i8::try_from(minutes / 60).map_err(serde::de::Error::custom)?;
1786                TimeZone::fixed(Offset::constant(hours))
1787            }
1788        }
1789    } else {
1790        TimeZone::UTC
1791    };
1792
1793    // If the time is omitted, assume midnight.
1794    let time = if let Some(time) = datetime.time {
1795        let hour = i8::try_from(time.hour).map_err(serde::de::Error::custom)?;
1796        let minute = i8::try_from(time.minute).map_err(serde::de::Error::custom)?;
1797        let second = time
1798            .second
1799            .map(i8::try_from)
1800            .transpose()
1801            .map_err(serde::de::Error::custom)?
1802            .unwrap_or_default();
1803        let nanosecond = time
1804            .nanosecond
1805            .map(i32::try_from)
1806            .transpose()
1807            .map_err(serde::de::Error::custom)?
1808            .unwrap_or_default();
1809        Time::new(hour, minute, second, nanosecond).map_err(serde::de::Error::custom)?
1810    } else {
1811        Time::midnight()
1812    };
1813
1814    let timestamp = tz
1815        .to_timestamp(DateTime::from_parts(date, time))
1816        .map_err(serde::de::Error::custom)?;
1817    Ok(Some(timestamp))
1818}