Skip to content

Commit

Permalink
Use a single type
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Aug 24, 2024
1 parent 5021f3f commit bfd7b53
Show file tree
Hide file tree
Showing 5 changed files with 393 additions and 143 deletions.
20 changes: 18 additions & 2 deletions crates/distribution-types/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,6 @@ impl Display for FileLocation {
PartialOrd,
Ord,
Hash,
Serialize,
Deserialize,
rkyv::Archive,
rkyv::Deserialize,
rkyv::Serialize,
Expand All @@ -153,6 +151,24 @@ impl Display for FileLocation {
#[archive_attr(derive(Debug))]
pub struct UrlString(String);

impl serde::Serialize for UrlString {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::ser::Serializer,
{
String::serialize(&self.0, serializer)
}
}

impl<'de> serde::de::Deserialize<'de> for UrlString {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::de::Deserializer<'de>,
{
String::deserialize(deserializer).map(UrlString)
}
}

impl UrlString {
/// Converts a [`UrlString`] to a [`Url`].
pub fn to_url(&self) -> Url {
Expand Down
7 changes: 3 additions & 4 deletions crates/distribution-types/src/index_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl FromStr for IndexUrl {

fn from_str(s: &str) -> Result<Self, Self::Err> {
let path = Path::new(s);
let url = if path.exists() {
let url = if Path::new(s).exists() {
VerbatimUrl::from_path(path)?
} else {
VerbatimUrl::parse_url(s)?
Expand Down Expand Up @@ -248,9 +248,8 @@ impl FromStr for FlatIndexLocation {
type Err = IndexUrlError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let path = Path::new(s);
let url = if path.exists() {
VerbatimUrl::from_path(path)?
let url = if Path::new(s).exists() {
VerbatimUrl::from_path(s)?
} else {
VerbatimUrl::parse_url(s)?
};
Expand Down
15 changes: 14 additions & 1 deletion crates/pep508-rs/src/verbatim_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ impl VerbatimUrl {
let mut url = Url::from_file_path(path.clone())
.map_err(|()| VerbatimUrlError::UrlConversion(path.to_path_buf()))?;

// Drop trailing slashes.
url.path_segments_mut().unwrap().pop_if_empty();

// Set the fragment, if it exists.
if let Some(fragment) = fragment {
url.set_fragment(Some(fragment));
Expand All @@ -67,7 +70,11 @@ impl VerbatimUrl {

/// Parse a URL from a string, expanding any environment variables.
pub fn parse_url(given: impl AsRef<str>) -> Result<Self, ParseError> {
let url = Url::parse(given.as_ref())?;
let mut url = Url::parse(given.as_ref())?;

// Drop trailing slashes.
url.path_segments_mut().unwrap().pop_if_empty();

Ok(Self { url, given: None })
}

Expand Down Expand Up @@ -97,6 +104,9 @@ impl VerbatimUrl {
let mut url = Url::from_file_path(path.clone())
.map_err(|()| VerbatimUrlError::UrlConversion(path.to_path_buf()))?;

// Drop trailing slashes.
url.path_segments_mut().unwrap().pop_if_empty();

// Set the fragment, if it exists.
if let Some(fragment) = fragment {
url.set_fragment(Some(fragment));
Expand Down Expand Up @@ -127,6 +137,9 @@ impl VerbatimUrl {
let mut url = Url::from_file_path(path.clone())
.unwrap_or_else(|()| panic!("path is absolute: {}", path.display()));

// Drop trailing slashes.
url.path_segments_mut().unwrap().pop_if_empty();

// Set the fragment, if it exists.
if let Some(fragment) = fragment {
url.set_fragment(Some(fragment));
Expand Down
Loading

0 comments on commit bfd7b53

Please sign in to comment.