Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(core): Clean up deserialization of PackageId #12785

Merged
merged 1 commit into from
Oct 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions src/cargo/core/package_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,27 +82,31 @@ impl<'de> de::Deserialize<'de> for PackageId {
D: de::Deserializer<'de>,
{
let string = String::deserialize(d)?;
let mut s = string.splitn(3, ' ');
let name = s.next().unwrap();
let name = InternedString::new(name);
let Some(version) = s.next() else {
return Err(de::Error::custom("invalid serialized PackageId"));
};
let version = version.to_semver().map_err(de::Error::custom)?;
let Some(url) = s.next() else {
return Err(de::Error::custom("invalid serialized PackageId"));
};
let url = if url.starts_with('(') && url.ends_with(')') {
&url[1..url.len() - 1]
} else {
return Err(de::Error::custom("invalid serialized PackageId"));
};

let (field, rest) = string
.split_once(' ')
.ok_or_else(|| de::Error::custom("invalid serialized PackageId"))?;
epage marked this conversation as resolved.
Show resolved Hide resolved
let name = InternedString::new(field);

let (field, rest) = rest
.split_once(' ')
.ok_or_else(|| de::Error::custom("invalid serialized PackageId"))?;
let version = field.to_semver().map_err(de::Error::custom)?;

let url =
strip_parens(rest).ok_or_else(|| de::Error::custom("invalid serialized PackageId"))?;
let source_id = SourceId::from_url(url).map_err(de::Error::custom)?;

Ok(PackageId::pure(name, version, source_id))
}
}

fn strip_parens(value: &str) -> Option<&str> {
let value = value.strip_prefix('(')?;
let value = value.strip_suffix(')')?;
Some(value)
}

impl PartialEq for PackageId {
fn eq(&self, other: &PackageId) -> bool {
if ptr::eq(self.inner, other.inner) {
Expand Down