uv_distribution_types/
index.rs

1use std::path::Path;
2use std::str::FromStr;
3
4use serde::{Deserialize, Serialize};
5use thiserror::Error;
6use url::Url;
7
8use uv_auth::{AuthPolicy, Credentials};
9use uv_redacted::DisplaySafeUrl;
10use uv_small_str::SmallString;
11
12use crate::index_name::{IndexName, IndexNameError};
13use crate::origin::Origin;
14use crate::{IndexStatusCodeStrategy, IndexUrl, IndexUrlError, SerializableStatusCode};
15
16/// Cache control configuration for an index.
17#[derive(Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize, Default)]
18#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
19#[serde(rename_all = "kebab-case")]
20pub struct IndexCacheControl {
21    /// Cache control header for Simple API requests.
22    pub api: Option<SmallString>,
23    /// Cache control header for file downloads.
24    pub files: Option<SmallString>,
25}
26
27impl IndexCacheControl {
28    /// Return the default Simple API cache control headers for the given index URL, if applicable.
29    pub fn simple_api_cache_control(_url: &Url) -> Option<&'static str> {
30        None
31    }
32
33    /// Return the default files cache control headers for the given index URL, if applicable.
34    pub fn artifact_cache_control(url: &Url) -> Option<&'static str> {
35        let dominated_by_pytorch_or_nvidia = url.host_str().is_some_and(|host| {
36            host.eq_ignore_ascii_case("download.pytorch.org")
37                || host.eq_ignore_ascii_case("pypi.nvidia.com")
38        });
39        if dominated_by_pytorch_or_nvidia {
40            // Some wheels in the PyTorch registry were accidentally uploaded with `no-cache,no-store,must-revalidate`.
41            // The PyTorch team plans to correct this in the future, but in the meantime we override
42            // the cache control headers to allow caching of static files.
43            //
44            // See: https://github.com/pytorch/pytorch/pull/149218
45            //
46            // The same issue applies to files hosted on `pypi.nvidia.com`.
47            Some("max-age=365000000, immutable, public")
48        } else {
49            None
50        }
51    }
52}
53
54#[derive(Debug, Clone, Serialize, Deserialize)]
55#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
56#[serde(rename_all = "kebab-case")]
57pub struct Index {
58    /// The name of the index.
59    ///
60    /// Index names can be used to reference indexes elsewhere in the configuration. For example,
61    /// you can pin a package to a specific index by name:
62    ///
63    /// ```toml
64    /// [[tool.uv.index]]
65    /// name = "pytorch"
66    /// url = "https://download.pytorch.org/whl/cu121"
67    ///
68    /// [tool.uv.sources]
69    /// torch = { index = "pytorch" }
70    /// ```
71    pub name: Option<IndexName>,
72    /// The URL of the index.
73    ///
74    /// Expects to receive a URL (e.g., `https://pypi.org/simple`) or a local path.
75    pub url: IndexUrl,
76    /// Mark the index as explicit.
77    ///
78    /// Explicit indexes will _only_ be used when explicitly requested via a `[tool.uv.sources]`
79    /// definition, as in:
80    ///
81    /// ```toml
82    /// [[tool.uv.index]]
83    /// name = "pytorch"
84    /// url = "https://download.pytorch.org/whl/cu121"
85    /// explicit = true
86    ///
87    /// [tool.uv.sources]
88    /// torch = { index = "pytorch" }
89    /// ```
90    #[serde(default)]
91    pub explicit: bool,
92    /// Mark the index as the default index.
93    ///
94    /// By default, uv uses PyPI as the default index, such that even if additional indexes are
95    /// defined via `[[tool.uv.index]]`, PyPI will still be used as a fallback for packages that
96    /// aren't found elsewhere. To disable the PyPI default, set `default = true` on at least one
97    /// other index.
98    ///
99    /// Marking an index as default will move it to the front of the list of indexes, such that it
100    /// is given the highest priority when resolving packages.
101    #[serde(default)]
102    pub default: bool,
103    /// The origin of the index (e.g., a CLI flag, a user-level configuration file, etc.).
104    #[serde(skip)]
105    pub origin: Option<Origin>,
106    /// The format used by the index.
107    ///
108    /// Indexes can either be PEP 503-compliant (i.e., a PyPI-style registry implementing the Simple
109    /// API) or structured as a flat list of distributions (e.g., `--find-links`). In both cases,
110    /// indexes can point to either local or remote resources.
111    #[serde(default)]
112    pub format: IndexFormat,
113    /// The URL of the upload endpoint.
114    ///
115    /// When using `uv publish --index <name>`, this URL is used for publishing.
116    ///
117    /// A configuration for the default index PyPI would look as follows:
118    ///
119    /// ```toml
120    /// [[tool.uv.index]]
121    /// name = "pypi"
122    /// url = "https://pypi.org/simple"
123    /// publish-url = "https://upload.pypi.org/legacy/"
124    /// ```
125    pub publish_url: Option<DisplaySafeUrl>,
126    /// When uv should use authentication for requests to the index.
127    ///
128    /// ```toml
129    /// [[tool.uv.index]]
130    /// name = "my-index"
131    /// url = "https://<omitted>/simple"
132    /// authenticate = "always"
133    /// ```
134    #[serde(default)]
135    pub authenticate: AuthPolicy,
136    /// Status codes that uv should ignore when deciding whether
137    /// to continue searching in the next index after a failure.
138    ///
139    /// ```toml
140    /// [[tool.uv.index]]
141    /// name = "my-index"
142    /// url = "https://<omitted>/simple"
143    /// ignore-error-codes = [401, 403]
144    /// ```
145    #[serde(default)]
146    pub ignore_error_codes: Option<Vec<SerializableStatusCode>>,
147    /// Cache control configuration for this index.
148    ///
149    /// When set, these headers will override the server's cache control headers
150    /// for both package metadata requests and artifact downloads.
151    ///
152    /// ```toml
153    /// [[tool.uv.index]]
154    /// name = "my-index"
155    /// url = "https://<omitted>/simple"
156    /// cache-control = { api = "max-age=600", files = "max-age=3600" }
157    /// ```
158    #[serde(default)]
159    pub cache_control: Option<IndexCacheControl>,
160}
161
162impl PartialEq for Index {
163    fn eq(&self, other: &Self) -> bool {
164        let Self {
165            name,
166            url,
167            explicit,
168            default,
169            origin: _,
170            format,
171            publish_url,
172            authenticate,
173            ignore_error_codes,
174            cache_control,
175        } = self;
176        *url == other.url
177            && *name == other.name
178            && *explicit == other.explicit
179            && *default == other.default
180            && *format == other.format
181            && *publish_url == other.publish_url
182            && *authenticate == other.authenticate
183            && *ignore_error_codes == other.ignore_error_codes
184            && *cache_control == other.cache_control
185    }
186}
187
188impl Eq for Index {}
189
190impl PartialOrd for Index {
191    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
192        Some(self.cmp(other))
193    }
194}
195
196impl Ord for Index {
197    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
198        let Self {
199            name,
200            url,
201            explicit,
202            default,
203            origin: _,
204            format,
205            publish_url,
206            authenticate,
207            ignore_error_codes,
208            cache_control,
209        } = self;
210        url.cmp(&other.url)
211            .then_with(|| name.cmp(&other.name))
212            .then_with(|| explicit.cmp(&other.explicit))
213            .then_with(|| default.cmp(&other.default))
214            .then_with(|| format.cmp(&other.format))
215            .then_with(|| publish_url.cmp(&other.publish_url))
216            .then_with(|| authenticate.cmp(&other.authenticate))
217            .then_with(|| ignore_error_codes.cmp(&other.ignore_error_codes))
218            .then_with(|| cache_control.cmp(&other.cache_control))
219    }
220}
221
222impl std::hash::Hash for Index {
223    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
224        let Self {
225            name,
226            url,
227            explicit,
228            default,
229            origin: _,
230            format,
231            publish_url,
232            authenticate,
233            ignore_error_codes,
234            cache_control,
235        } = self;
236        url.hash(state);
237        name.hash(state);
238        explicit.hash(state);
239        default.hash(state);
240        format.hash(state);
241        publish_url.hash(state);
242        authenticate.hash(state);
243        ignore_error_codes.hash(state);
244        cache_control.hash(state);
245    }
246}
247
248#[derive(
249    Default,
250    Debug,
251    Copy,
252    Clone,
253    Hash,
254    Eq,
255    PartialEq,
256    Ord,
257    PartialOrd,
258    serde::Serialize,
259    serde::Deserialize,
260)]
261#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
262#[serde(rename_all = "kebab-case")]
263pub enum IndexFormat {
264    /// A PyPI-style index implementing the Simple Repository API.
265    #[default]
266    Simple,
267    /// A `--find-links`-style index containing a flat list of wheels and source distributions.
268    Flat,
269}
270
271impl Index {
272    /// Initialize an [`Index`] from a pip-style `--index-url`.
273    pub fn from_index_url(url: IndexUrl) -> Self {
274        Self {
275            url,
276            name: None,
277            explicit: false,
278            default: true,
279            origin: None,
280            format: IndexFormat::Simple,
281            publish_url: None,
282            authenticate: AuthPolicy::default(),
283            ignore_error_codes: None,
284            cache_control: None,
285        }
286    }
287
288    /// Initialize an [`Index`] from a pip-style `--extra-index-url`.
289    pub fn from_extra_index_url(url: IndexUrl) -> Self {
290        Self {
291            url,
292            name: None,
293            explicit: false,
294            default: false,
295            origin: None,
296            format: IndexFormat::Simple,
297            publish_url: None,
298            authenticate: AuthPolicy::default(),
299            ignore_error_codes: None,
300            cache_control: None,
301        }
302    }
303
304    /// Initialize an [`Index`] from a pip-style `--find-links`.
305    pub fn from_find_links(url: IndexUrl) -> Self {
306        Self {
307            url,
308            name: None,
309            explicit: false,
310            default: false,
311            origin: None,
312            format: IndexFormat::Flat,
313            publish_url: None,
314            authenticate: AuthPolicy::default(),
315            ignore_error_codes: None,
316            cache_control: None,
317        }
318    }
319
320    /// Set the [`Origin`] of the index.
321    #[must_use]
322    pub fn with_origin(mut self, origin: Origin) -> Self {
323        self.origin = Some(origin);
324        self
325    }
326
327    /// Return the [`IndexUrl`] of the index.
328    pub fn url(&self) -> &IndexUrl {
329        &self.url
330    }
331
332    /// Consume the [`Index`] and return the [`IndexUrl`].
333    pub fn into_url(self) -> IndexUrl {
334        self.url
335    }
336
337    /// Return the raw [`Url`] of the index.
338    pub fn raw_url(&self) -> &DisplaySafeUrl {
339        self.url.url()
340    }
341
342    /// Return the root [`Url`] of the index, if applicable.
343    ///
344    /// For indexes with a `/simple` endpoint, this is simply the URL with the final segment
345    /// removed. This is useful, e.g., for credential propagation to other endpoints on the index.
346    pub fn root_url(&self) -> Option<DisplaySafeUrl> {
347        self.url.root()
348    }
349
350    /// Retrieve the credentials for the index, either from the environment, or from the URL itself.
351    pub fn credentials(&self) -> Option<Credentials> {
352        // If the index is named, and credentials are provided via the environment, prefer those.
353        if let Some(name) = self.name.as_ref() {
354            if let Some(credentials) = Credentials::from_env(name.to_env_var()) {
355                return Some(credentials);
356            }
357        }
358
359        // Otherwise, extract the credentials from the URL.
360        Credentials::from_url(self.url.url())
361    }
362
363    /// Resolve the index relative to the given root directory.
364    pub fn relative_to(mut self, root_dir: &Path) -> Result<Self, IndexUrlError> {
365        if let IndexUrl::Path(ref url) = self.url {
366            if let Some(given) = url.given() {
367                self.url = IndexUrl::parse(given, Some(root_dir))?;
368            }
369        }
370        Ok(self)
371    }
372
373    /// Return the [`IndexStatusCodeStrategy`] for this index.
374    pub fn status_code_strategy(&self) -> IndexStatusCodeStrategy {
375        if let Some(ignore_error_codes) = &self.ignore_error_codes {
376            IndexStatusCodeStrategy::from_ignored_error_codes(ignore_error_codes)
377        } else {
378            IndexStatusCodeStrategy::from_index_url(self.url.url())
379        }
380    }
381
382    /// Return the cache control header for file requests to this index, if any.
383    pub fn artifact_cache_control(&self) -> Option<&str> {
384        if let Some(artifact_cache_control) = self
385            .cache_control
386            .as_ref()
387            .and_then(|cache_control| cache_control.files.as_deref())
388        {
389            Some(artifact_cache_control)
390        } else {
391            IndexCacheControl::artifact_cache_control(self.url.url())
392        }
393    }
394
395    /// Return the cache control header for API requests to this index, if any.
396    pub fn simple_api_cache_control(&self) -> Option<&str> {
397        if let Some(api_cache_control) = self
398            .cache_control
399            .as_ref()
400            .and_then(|cache_control| cache_control.api.as_deref())
401        {
402            Some(api_cache_control)
403        } else {
404            IndexCacheControl::simple_api_cache_control(self.url.url())
405        }
406    }
407}
408
409impl From<IndexUrl> for Index {
410    fn from(value: IndexUrl) -> Self {
411        Self {
412            name: None,
413            url: value,
414            explicit: false,
415            default: false,
416            origin: None,
417            format: IndexFormat::Simple,
418            publish_url: None,
419            authenticate: AuthPolicy::default(),
420            ignore_error_codes: None,
421            cache_control: None,
422        }
423    }
424}
425
426impl FromStr for Index {
427    type Err = IndexSourceError;
428
429    fn from_str(s: &str) -> Result<Self, Self::Err> {
430        // Determine whether the source is prefixed with a name, as in `name=https://pypi.org/simple`.
431        if let Some((name, url)) = s.split_once('=') {
432            if !name.chars().any(|c| c == ':') {
433                let name = IndexName::from_str(name)?;
434                let url = IndexUrl::from_str(url)?;
435                return Ok(Self {
436                    name: Some(name),
437                    url,
438                    explicit: false,
439                    default: false,
440                    origin: None,
441                    format: IndexFormat::Simple,
442                    publish_url: None,
443                    authenticate: AuthPolicy::default(),
444                    ignore_error_codes: None,
445                    cache_control: None,
446                });
447            }
448        }
449
450        // Otherwise, assume the source is a URL.
451        let url = IndexUrl::from_str(s)?;
452        Ok(Self {
453            name: None,
454            url,
455            explicit: false,
456            default: false,
457            origin: None,
458            format: IndexFormat::Simple,
459            publish_url: None,
460            authenticate: AuthPolicy::default(),
461            ignore_error_codes: None,
462            cache_control: None,
463        })
464    }
465}
466
467/// An [`IndexUrl`] along with the metadata necessary to query the index.
468#[derive(Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
469pub struct IndexMetadata {
470    /// The URL of the index.
471    pub url: IndexUrl,
472    /// The format used by the index.
473    pub format: IndexFormat,
474}
475
476impl IndexMetadata {
477    /// Return a reference to the [`IndexMetadata`].
478    pub fn as_ref(&self) -> IndexMetadataRef<'_> {
479        let Self { url, format: kind } = self;
480        IndexMetadataRef { url, format: *kind }
481    }
482
483    /// Consume the [`IndexMetadata`] and return the [`IndexUrl`].
484    pub fn into_url(self) -> IndexUrl {
485        self.url
486    }
487}
488
489/// A reference to an [`IndexMetadata`].
490#[derive(Debug, Copy, Clone)]
491pub struct IndexMetadataRef<'a> {
492    /// The URL of the index.
493    pub url: &'a IndexUrl,
494    /// The format used by the index.
495    pub format: IndexFormat,
496}
497
498impl IndexMetadata {
499    /// Return the [`IndexUrl`] of the index.
500    pub fn url(&self) -> &IndexUrl {
501        &self.url
502    }
503}
504
505impl IndexMetadataRef<'_> {
506    /// Return the [`IndexUrl`] of the index.
507    pub fn url(&self) -> &IndexUrl {
508        self.url
509    }
510}
511
512impl<'a> From<&'a Index> for IndexMetadataRef<'a> {
513    fn from(value: &'a Index) -> Self {
514        Self {
515            url: &value.url,
516            format: value.format,
517        }
518    }
519}
520
521impl<'a> From<&'a IndexMetadata> for IndexMetadataRef<'a> {
522    fn from(value: &'a IndexMetadata) -> Self {
523        Self {
524            url: &value.url,
525            format: value.format,
526        }
527    }
528}
529
530impl From<IndexUrl> for IndexMetadata {
531    fn from(value: IndexUrl) -> Self {
532        Self {
533            url: value,
534            format: IndexFormat::Simple,
535        }
536    }
537}
538
539impl<'a> From<&'a IndexUrl> for IndexMetadataRef<'a> {
540    fn from(value: &'a IndexUrl) -> Self {
541        Self {
542            url: value,
543            format: IndexFormat::Simple,
544        }
545    }
546}
547
548/// An error that can occur when parsing an [`Index`].
549#[derive(Error, Debug)]
550pub enum IndexSourceError {
551    #[error(transparent)]
552    Url(#[from] IndexUrlError),
553    #[error(transparent)]
554    IndexName(#[from] IndexNameError),
555    #[error("Index included a name, but the name was empty")]
556    EmptyName,
557}
558
559#[cfg(test)]
560mod tests {
561    use super::*;
562
563    #[test]
564    fn test_index_cache_control_headers() {
565        // Test that cache control headers are properly parsed from TOML
566        let toml_str = r#"
567            name = "test-index"
568            url = "https://test.example.com/simple"
569            cache-control = { api = "max-age=600", files = "max-age=3600" }
570        "#;
571
572        let index: Index = toml::from_str(toml_str).unwrap();
573        assert_eq!(index.name.as_ref().unwrap().as_ref(), "test-index");
574        assert!(index.cache_control.is_some());
575        let cache_control = index.cache_control.as_ref().unwrap();
576        assert_eq!(cache_control.api.as_deref(), Some("max-age=600"));
577        assert_eq!(cache_control.files.as_deref(), Some("max-age=3600"));
578    }
579
580    #[test]
581    fn test_index_without_cache_control() {
582        // Test that indexes work without cache control headers
583        let toml_str = r#"
584            name = "test-index"
585            url = "https://test.example.com/simple"
586        "#;
587
588        let index: Index = toml::from_str(toml_str).unwrap();
589        assert_eq!(index.name.as_ref().unwrap().as_ref(), "test-index");
590        assert_eq!(index.cache_control, None);
591    }
592
593    #[test]
594    fn test_index_partial_cache_control() {
595        // Test that cache control can have just one field
596        let toml_str = r#"
597            name = "test-index"
598            url = "https://test.example.com/simple"
599            cache-control = { api = "max-age=300" }
600        "#;
601
602        let index: Index = toml::from_str(toml_str).unwrap();
603        assert_eq!(index.name.as_ref().unwrap().as_ref(), "test-index");
604        assert!(index.cache_control.is_some());
605        let cache_control = index.cache_control.as_ref().unwrap();
606        assert_eq!(cache_control.api.as_deref(), Some("max-age=300"));
607        assert_eq!(cache_control.files, None);
608    }
609}