binstalk_types/
crate_info.rs1use std::{borrow, cmp, hash};
4
5use compact_str::CompactString;
6use maybe_owned::MaybeOwned;
7use once_cell::sync::Lazy;
8use semver::Version;
9use serde::{Deserialize, Serialize};
10use url::Url;
11
12pub fn cratesio_url() -> &'static Url {
13 static CRATESIO: Lazy<Url, fn() -> Url> =
14 Lazy::new(|| Url::parse("https://github.com/rust-lang/crates.io-index").unwrap());
15
16 &CRATESIO
17}
18
19#[derive(Clone, Debug, Serialize, Deserialize)]
20pub struct CrateInfo {
21 pub name: CompactString,
22 pub version_req: CompactString,
23 pub current_version: Version,
24 pub source: CrateSource,
25 pub target: CompactString,
26 pub bins: Vec<CompactString>,
27}
28
29impl borrow::Borrow<str> for CrateInfo {
30 fn borrow(&self) -> &str {
31 &self.name
32 }
33}
34
35impl PartialEq for CrateInfo {
36 fn eq(&self, other: &Self) -> bool {
37 self.name == other.name
38 }
39}
40impl Eq for CrateInfo {}
41
42impl PartialOrd for CrateInfo {
43 fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
44 Some(self.cmp(other))
45 }
46}
47
48impl Ord for CrateInfo {
49 fn cmp(&self, other: &Self) -> cmp::Ordering {
50 self.name.cmp(&other.name)
51 }
52}
53
54impl hash::Hash for CrateInfo {
55 fn hash<H>(&self, state: &mut H)
56 where
57 H: hash::Hasher,
58 {
59 self.name.hash(state)
60 }
61}
62
63#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
64pub enum SourceType {
65 Git,
66 Path,
67 Registry,
68 Sparse,
69}
70
71#[derive(Clone, Debug, Serialize, Deserialize)]
72pub struct CrateSource {
73 pub source_type: SourceType,
74 pub url: MaybeOwned<'static, Url>,
75}
76
77impl CrateSource {
78 pub fn cratesio_registry() -> CrateSource {
79 Self {
80 source_type: SourceType::Registry,
81 url: MaybeOwned::Borrowed(cratesio_url()),
82 }
83 }
84}