uv_normalize/
extra_name.rs

1use std::fmt;
2use std::fmt::{Display, Formatter};
3use std::str::FromStr;
4
5use serde::ser::SerializeSeq;
6use serde::{Deserialize, Deserializer, Serialize};
7
8use uv_small_str::SmallString;
9
10use crate::{InvalidNameError, validate_and_normalize_ref};
11
12/// Either the literal "all" or a list of extras
13#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
14#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
15pub enum DefaultExtras {
16    /// All extras are defaulted
17    All,
18    /// A list of extras
19    List(Vec<ExtraName>),
20}
21
22/// Serialize a [`DefaultExtras`] struct into a list of marker strings.
23impl serde::Serialize for DefaultExtras {
24    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
25    where
26        S: serde::Serializer,
27    {
28        match self {
29            Self::All => serializer.serialize_str("all"),
30            Self::List(extras) => {
31                let mut seq = serializer.serialize_seq(Some(extras.len()))?;
32                for extra in extras {
33                    seq.serialize_element(&extra)?;
34                }
35                seq.end()
36            }
37        }
38    }
39}
40
41/// Deserialize a "all" or list of [`ExtraName`] into a [`DefaultExtras`] enum.
42impl<'de> serde::Deserialize<'de> for DefaultExtras {
43    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
44    where
45        D: serde::Deserializer<'de>,
46    {
47        struct StringOrVecVisitor;
48
49        impl<'de> serde::de::Visitor<'de> for StringOrVecVisitor {
50            type Value = DefaultExtras;
51
52            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
53                formatter.write_str(r#"the string "all" or a list of strings"#)
54            }
55
56            fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
57            where
58                E: serde::de::Error,
59            {
60                if value != "all" {
61                    return Err(serde::de::Error::custom(
62                        r#"default-extras must be "all" or a ["list", "of", "extras"]"#,
63                    ));
64                }
65                Ok(DefaultExtras::All)
66            }
67
68            fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
69            where
70                A: serde::de::SeqAccess<'de>,
71            {
72                let mut extras = Vec::new();
73
74                while let Some(elem) = seq.next_element::<ExtraName>()? {
75                    extras.push(elem);
76                }
77
78                Ok(DefaultExtras::List(extras))
79            }
80        }
81
82        deserializer.deserialize_any(StringOrVecVisitor)
83    }
84}
85
86impl Default for DefaultExtras {
87    fn default() -> Self {
88        Self::List(Vec::new())
89    }
90}
91
92/// The normalized name of an extra dependency.
93///
94/// Converts the name to lowercase and collapses runs of `-`, `_`, and `.` down to a single `-`.
95/// For example, `---`, `.`, and `__` are all converted to a single `-`.
96///
97/// See:
98/// - <https://peps.python.org/pep-0685/#specification/>
99/// - <https://packaging.python.org/en/latest/specifications/name-normalization/>
100#[derive(
101    Debug,
102    Clone,
103    PartialEq,
104    Eq,
105    PartialOrd,
106    Ord,
107    Hash,
108    Serialize,
109    rkyv::Archive,
110    rkyv::Deserialize,
111    rkyv::Serialize,
112)]
113#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
114#[rkyv(derive(Debug))]
115pub struct ExtraName(SmallString);
116
117impl ExtraName {
118    /// Create a validated, normalized extra name.
119    ///
120    /// At present, this is no more efficient than calling [`ExtraName::from_str`].
121    #[allow(clippy::needless_pass_by_value)]
122    pub fn from_owned(name: String) -> Result<Self, InvalidNameError> {
123        validate_and_normalize_ref(&name).map(Self)
124    }
125
126    /// Return the underlying extra name as a string.
127    pub fn as_str(&self) -> &str {
128        &self.0
129    }
130}
131
132impl FromStr for ExtraName {
133    type Err = InvalidNameError;
134
135    fn from_str(name: &str) -> Result<Self, Self::Err> {
136        validate_and_normalize_ref(name).map(Self)
137    }
138}
139
140impl<'de> Deserialize<'de> for ExtraName {
141    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
142    where
143        D: Deserializer<'de>,
144    {
145        struct Visitor;
146
147        impl serde::de::Visitor<'_> for Visitor {
148            type Value = ExtraName;
149
150            fn expecting(&self, f: &mut Formatter) -> fmt::Result {
151                f.write_str("a string")
152            }
153
154            fn visit_str<E: serde::de::Error>(self, v: &str) -> Result<Self::Value, E> {
155                ExtraName::from_str(v).map_err(serde::de::Error::custom)
156            }
157
158            fn visit_string<E: serde::de::Error>(self, v: String) -> Result<Self::Value, E> {
159                ExtraName::from_owned(v).map_err(serde::de::Error::custom)
160            }
161        }
162
163        deserializer.deserialize_str(Visitor)
164    }
165}
166
167impl Display for ExtraName {
168    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
169        self.0.fmt(f)
170    }
171}
172
173impl AsRef<str> for ExtraName {
174    fn as_ref(&self) -> &str {
175        self.as_str()
176    }
177}