mirror of https://github.com/astral-sh/uv
Use small string for index name type (#12355)
## Summary I'm considering passing this around more, so nice to reduce the size in easy ways.
This commit is contained in:
parent
82fe8662ab
commit
cf20d9a1ca
|
|
@ -1,31 +1,34 @@
|
||||||
|
use std::borrow::Cow;
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
|
use uv_small_str::SmallString;
|
||||||
|
|
||||||
/// The normalized name of an index.
|
/// The normalized name of an index.
|
||||||
///
|
///
|
||||||
/// Index names may contain letters, digits, hyphens, underscores, and periods, and must be ASCII.
|
/// Index names may contain letters, digits, hyphens, underscores, and periods, and must be ASCII.
|
||||||
#[derive(Debug, Clone, Hash, Eq, PartialEq, serde::Serialize)]
|
#[derive(Debug, Clone, Hash, Eq, PartialEq, serde::Serialize)]
|
||||||
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
pub struct IndexName(String);
|
pub struct IndexName(SmallString);
|
||||||
|
|
||||||
impl IndexName {
|
impl IndexName {
|
||||||
/// Validates the given index name and returns [`IndexName`] if it's valid, or an error
|
/// Validates the given index name and returns [`IndexName`] if it's valid, or an error
|
||||||
/// otherwise.
|
/// otherwise.
|
||||||
pub fn new(name: String) -> Result<Self, IndexNameError> {
|
pub fn new(name: &str) -> Result<Self, IndexNameError> {
|
||||||
for c in name.chars() {
|
for c in name.chars() {
|
||||||
match c {
|
match c {
|
||||||
'a'..='z' | 'A'..='Z' | '0'..='9' | '-' | '_' | '.' => {}
|
'a'..='z' | 'A'..='Z' | '0'..='9' | '-' | '_' | '.' => {}
|
||||||
c if c.is_ascii() => {
|
c if c.is_ascii() => {
|
||||||
return Err(IndexNameError::UnsupportedCharacter(c, name));
|
return Err(IndexNameError::UnsupportedCharacter(c, name.to_string()));
|
||||||
}
|
}
|
||||||
c => {
|
c => {
|
||||||
return Err(IndexNameError::NonAsciiName(c, name));
|
return Err(IndexNameError::NonAsciiName(c, name.to_string()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(Self(name))
|
Ok(Self(SmallString::from(name)))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Converts the index name to an environment variable name.
|
/// Converts the index name to an environment variable name.
|
||||||
|
|
@ -49,7 +52,7 @@ impl FromStr for IndexName {
|
||||||
type Err = IndexNameError;
|
type Err = IndexNameError;
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
Self::new(s.to_string())
|
Self::new(s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -58,8 +61,8 @@ impl<'de> serde::de::Deserialize<'de> for IndexName {
|
||||||
where
|
where
|
||||||
D: serde::de::Deserializer<'de>,
|
D: serde::de::Deserializer<'de>,
|
||||||
{
|
{
|
||||||
let s = String::deserialize(deserializer)?;
|
let s = Cow::<'_, str>::deserialize(deserializer)?;
|
||||||
IndexName::new(s).map_err(serde::de::Error::custom)
|
IndexName::new(&s).map_err(serde::de::Error::custom)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue