Skip to content

Commit f3999d5

Browse files
committed
refactor(schema): Pull SourceKind out into schemas
1 parent d7bcc0c commit f3999d5

File tree

5 files changed

+210
-202
lines changed

5 files changed

+210
-202
lines changed

src/cargo/core/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ pub use self::package_id_spec::{PackageIdSpec, PackageIdSpecQuery};
88
pub use self::registry::Registry;
99
pub use self::resolver::{Resolve, ResolveVersion};
1010
pub use self::shell::{Shell, Verbosity};
11-
pub use self::source_id::{GitReference, SourceId, SourceKind};
11+
pub use self::source_id::SourceId;
1212
pub use self::summary::{FeatureMap, FeatureValue, Summary};
1313
pub use self::workspace::{
1414
find_workspace_root, resolve_relative_path, MaybePackage, Workspace, WorkspaceConfig,
1515
WorkspaceRootConfig,
1616
};
17+
pub use crate::util_schemas::core::{GitReference, SourceKind};
1718

1819
pub mod compiler;
1920
pub mod dependency;

src/cargo/core/source_id.rs

Lines changed: 2 additions & 201 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
use crate::core::GitReference;
12
use crate::core::PackageId;
3+
use crate::core::SourceKind;
24
use crate::sources::registry::CRATES_IO_HTTP_INDEX;
35
use crate::sources::source::Source;
46
use crate::sources::{DirectorySource, CRATES_IO_DOMAIN, CRATES_IO_INDEX, CRATES_IO_REGISTRY};
@@ -82,38 +84,6 @@ impl fmt::Display for Precise {
8284
}
8385
}
8486

85-
/// The possible kinds of code source.
86-
/// Along with [`SourceIdInner`], this fully defines the source.
87-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
88-
pub enum SourceKind {
89-
/// A git repository.
90-
Git(GitReference),
91-
/// A local path.
92-
Path,
93-
/// A remote registry.
94-
Registry,
95-
/// A sparse registry.
96-
SparseRegistry,
97-
/// A local filesystem-based registry.
98-
LocalRegistry,
99-
/// A directory-based registry.
100-
Directory,
101-
}
102-
103-
/// Information to find a specific commit in a Git repository.
104-
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
105-
pub enum GitReference {
106-
/// From a tag.
107-
Tag(String),
108-
/// From a branch.
109-
Branch(String),
110-
/// From a specific revision. Can be a commit hash (either short or full),
111-
/// or a named reference like `refs/pull/493/head`.
112-
Rev(String),
113-
/// The default branch of the repository, the reference named `HEAD`.
114-
DefaultBranch,
115-
}
116-
11787
/// Where the remote source key is defined.
11888
///
11989
/// The purpose of this is to provide better diagnostics for different sources of keys.
@@ -746,108 +716,6 @@ impl PartialEq for SourceIdInner {
746716
}
747717
}
748718

749-
impl SourceKind {
750-
pub(crate) fn protocol(&self) -> Option<&str> {
751-
match self {
752-
SourceKind::Path => Some("path"),
753-
SourceKind::Git(_) => Some("git"),
754-
SourceKind::Registry => Some("registry"),
755-
// Sparse registry URL already includes the `sparse+` prefix, see `SourceId::new`
756-
SourceKind::SparseRegistry => None,
757-
SourceKind::LocalRegistry => Some("local-registry"),
758-
SourceKind::Directory => Some("directory"),
759-
}
760-
}
761-
}
762-
763-
/// Forwards to `Ord`
764-
impl PartialOrd for SourceKind {
765-
fn partial_cmp(&self, other: &SourceKind) -> Option<Ordering> {
766-
Some(self.cmp(other))
767-
}
768-
}
769-
770-
/// Note that this is specifically not derived on `SourceKind` although the
771-
/// implementation here is very similar to what it might look like if it were
772-
/// otherwise derived.
773-
///
774-
/// The reason for this is somewhat obtuse. First of all the hash value of
775-
/// `SourceKind` makes its way into `~/.cargo/registry/index/github.com-XXXX`
776-
/// which means that changes to the hash means that all Rust users need to
777-
/// redownload the crates.io index and all their crates. If possible we strive
778-
/// to not change this to make this redownloading behavior happen as little as
779-
/// possible. How is this connected to `Ord` you might ask? That's a good
780-
/// question!
781-
///
782-
/// Since the beginning of time `SourceKind` has had `#[derive(Hash)]`. It for
783-
/// the longest time *also* derived the `Ord` and `PartialOrd` traits. In #8522,
784-
/// however, the implementation of `Ord` changed. This handwritten implementation
785-
/// forgot to sync itself with the originally derived implementation, namely
786-
/// placing git dependencies as sorted after all other dependencies instead of
787-
/// first as before.
788-
///
789-
/// This regression in #8522 (Rust 1.47) went unnoticed. When we switched back
790-
/// to a derived implementation in #9133 (Rust 1.52 beta) we only then ironically
791-
/// saw an issue (#9334). In #9334 it was observed that stable Rust at the time
792-
/// (1.51) was sorting git dependencies last, whereas Rust 1.52 beta would sort
793-
/// git dependencies first. This is because the `PartialOrd` implementation in
794-
/// 1.51 used #8522, the buggy implementation, which put git deps last. In 1.52
795-
/// it was (unknowingly) restored to the pre-1.47 behavior with git dependencies
796-
/// first.
797-
///
798-
/// Because the breakage was only witnessed after the original breakage, this
799-
/// trait implementation is preserving the "broken" behavior. Put a different way:
800-
///
801-
/// * Rust pre-1.47 sorted git deps first.
802-
/// * Rust 1.47 to Rust 1.51 sorted git deps last, a breaking change (#8522) that
803-
/// was never noticed.
804-
/// * Rust 1.52 restored the pre-1.47 behavior (#9133, without knowing it did
805-
/// so), and breakage was witnessed by actual users due to difference with
806-
/// 1.51.
807-
/// * Rust 1.52 (the source as it lives now) was fixed to match the 1.47-1.51
808-
/// behavior (#9383), which is now considered intentionally breaking from the
809-
/// pre-1.47 behavior.
810-
///
811-
/// Note that this was all discovered when Rust 1.53 was in nightly and 1.52 was
812-
/// in beta. #9133 was in both beta and nightly at the time of discovery. For
813-
/// 1.52 #9383 reverted #9133, meaning 1.52 is the same as 1.51. On nightly
814-
/// (1.53) #9397 was created to fix the regression introduced by #9133 relative
815-
/// to the current stable (1.51).
816-
///
817-
/// That's all a long winded way of saying "it's weird that git deps hash first
818-
/// and are sorted last, but it's the way it is right now". The author of this
819-
/// comment chose to handwrite the `Ord` implementation instead of the `Hash`
820-
/// implementation, but it's only required that at most one of them is
821-
/// hand-written because the other can be derived. Perhaps one day in
822-
/// the future someone can figure out how to remove this behavior.
823-
impl Ord for SourceKind {
824-
fn cmp(&self, other: &SourceKind) -> Ordering {
825-
match (self, other) {
826-
(SourceKind::Path, SourceKind::Path) => Ordering::Equal,
827-
(SourceKind::Path, _) => Ordering::Less,
828-
(_, SourceKind::Path) => Ordering::Greater,
829-
830-
(SourceKind::Registry, SourceKind::Registry) => Ordering::Equal,
831-
(SourceKind::Registry, _) => Ordering::Less,
832-
(_, SourceKind::Registry) => Ordering::Greater,
833-
834-
(SourceKind::SparseRegistry, SourceKind::SparseRegistry) => Ordering::Equal,
835-
(SourceKind::SparseRegistry, _) => Ordering::Less,
836-
(_, SourceKind::SparseRegistry) => Ordering::Greater,
837-
838-
(SourceKind::LocalRegistry, SourceKind::LocalRegistry) => Ordering::Equal,
839-
(SourceKind::LocalRegistry, _) => Ordering::Less,
840-
(_, SourceKind::LocalRegistry) => Ordering::Greater,
841-
842-
(SourceKind::Directory, SourceKind::Directory) => Ordering::Equal,
843-
(SourceKind::Directory, _) => Ordering::Less,
844-
(_, SourceKind::Directory) => Ordering::Greater,
845-
846-
(SourceKind::Git(a), SourceKind::Git(b)) => a.cmp(b),
847-
}
848-
}
849-
}
850-
851719
/// A `Display`able view into a `SourceId` that will write it as a url
852720
pub struct SourceIdAsUrl<'a> {
853721
inner: &'a SourceIdInner,
@@ -877,73 +745,6 @@ impl<'a> fmt::Display for SourceIdAsUrl<'a> {
877745
}
878746
}
879747

880-
impl GitReference {
881-
pub fn from_query(
882-
query_pairs: impl Iterator<Item = (impl AsRef<str>, impl AsRef<str>)>,
883-
) -> Self {
884-
let mut reference = GitReference::DefaultBranch;
885-
for (k, v) in query_pairs {
886-
let v = v.as_ref();
887-
match k.as_ref() {
888-
// Map older 'ref' to branch.
889-
"branch" | "ref" => reference = GitReference::Branch(v.to_owned()),
890-
891-
"rev" => reference = GitReference::Rev(v.to_owned()),
892-
"tag" => reference = GitReference::Tag(v.to_owned()),
893-
_ => {}
894-
}
895-
}
896-
reference
897-
}
898-
899-
/// Returns a `Display`able view of this git reference, or None if using
900-
/// the head of the default branch
901-
pub fn pretty_ref(&self, url_encoded: bool) -> Option<PrettyRef<'_>> {
902-
match self {
903-
GitReference::DefaultBranch => None,
904-
_ => Some(PrettyRef {
905-
inner: self,
906-
url_encoded,
907-
}),
908-
}
909-
}
910-
}
911-
912-
/// A git reference that can be `Display`ed
913-
pub struct PrettyRef<'a> {
914-
inner: &'a GitReference,
915-
url_encoded: bool,
916-
}
917-
918-
impl<'a> fmt::Display for PrettyRef<'a> {
919-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
920-
let value: &str;
921-
match self.inner {
922-
GitReference::Branch(s) => {
923-
write!(f, "branch=")?;
924-
value = s;
925-
}
926-
GitReference::Tag(s) => {
927-
write!(f, "tag=")?;
928-
value = s;
929-
}
930-
GitReference::Rev(s) => {
931-
write!(f, "rev=")?;
932-
value = s;
933-
}
934-
GitReference::DefaultBranch => unreachable!(),
935-
}
936-
if self.url_encoded {
937-
for value in url::form_urlencoded::byte_serialize(value.as_bytes()) {
938-
write!(f, "{value}")?;
939-
}
940-
} else {
941-
write!(f, "{value}")?;
942-
}
943-
Ok(())
944-
}
945-
}
946-
947748
impl KeyOf {
948749
/// Gets the underlying key.
949750
fn key(&self) -> &str {

src/cargo/util_schemas/core/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
mod source_kind;
2+
3+
pub use source_kind::GitReference;
4+
pub use source_kind::SourceKind;

0 commit comments

Comments
 (0)