uv_distribution_types/
pip_index.rs1use serde::{Deserialize, Deserializer, Serialize};
6#[cfg(feature = "schemars")]
7use std::borrow::Cow;
8use std::path::Path;
9
10use crate::{Index, IndexUrl, Origin};
11
12macro_rules! impl_index {
13 ($name:ident, $from:expr) => {
14 #[derive(Debug, Clone, Eq, PartialEq)]
15 pub struct $name(Index);
16
17 impl $name {
18 pub fn relative_to(self, root_dir: &Path) -> Result<Self, crate::IndexUrlError> {
19 Ok(Self(self.0.relative_to(root_dir)?))
20 }
21
22 pub fn try_set_origin(&mut self, origin: Origin) {
24 self.0.origin.get_or_insert(origin);
25 }
26 }
27
28 impl From<$name> for Index {
29 fn from(value: $name) -> Self {
30 value.0
31 }
32 }
33
34 impl From<Index> for $name {
35 fn from(value: Index) -> Self {
36 Self(value)
37 }
38 }
39
40 impl Serialize for $name {
41 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
42 where
43 S: serde::Serializer,
44 {
45 self.0.url().serialize(serializer)
46 }
47 }
48
49 impl<'de> Deserialize<'de> for $name {
50 fn deserialize<D>(deserializer: D) -> Result<$name, D::Error>
51 where
52 D: Deserializer<'de>,
53 {
54 IndexUrl::deserialize(deserializer).map($from).map(Self)
55 }
56 }
57
58 #[cfg(feature = "schemars")]
59 impl schemars::JsonSchema for $name {
60 fn schema_name() -> Cow<'static, str> {
61 IndexUrl::schema_name()
62 }
63
64 fn json_schema(
65 generator: &mut schemars::generate::SchemaGenerator,
66 ) -> schemars::Schema {
67 IndexUrl::json_schema(generator)
68 }
69 }
70 };
71}
72
73impl_index!(PipIndex, Index::from_index_url);
74impl_index!(PipExtraIndex, Index::from_extra_index_url);
75impl_index!(PipFindLinks, Index::from_find_links);