Skip to main content

uv_resolver/lock/export/
pylock_toml.rs

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