binstalk_types/
crate_info.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//! Common structure for crate information for post-install manifests.

use std::{borrow, cmp, hash};

use compact_str::CompactString;
use maybe_owned::MaybeOwned;
use once_cell::sync::Lazy;
use semver::Version;
use serde::{Deserialize, Serialize};
use url::Url;

pub fn cratesio_url() -> &'static Url {
    static CRATESIO: Lazy<Url, fn() -> Url> =
        Lazy::new(|| Url::parse("https://github.com/rust-lang/crates.io-index").unwrap());

    &CRATESIO
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct CrateInfo {
    pub name: CompactString,
    pub version_req: CompactString,
    pub current_version: Version,
    pub source: CrateSource,
    pub target: CompactString,
    pub bins: Vec<CompactString>,
}

impl borrow::Borrow<str> for CrateInfo {
    fn borrow(&self) -> &str {
        &self.name
    }
}

impl PartialEq for CrateInfo {
    fn eq(&self, other: &Self) -> bool {
        self.name == other.name
    }
}
impl Eq for CrateInfo {}

impl PartialOrd for CrateInfo {
    fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for CrateInfo {
    fn cmp(&self, other: &Self) -> cmp::Ordering {
        self.name.cmp(&other.name)
    }
}

impl hash::Hash for CrateInfo {
    fn hash<H>(&self, state: &mut H)
    where
        H: hash::Hasher,
    {
        self.name.hash(state)
    }
}

#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
pub enum SourceType {
    Git,
    Path,
    Registry,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct CrateSource {
    pub source_type: SourceType,
    pub url: MaybeOwned<'static, Url>,
}

impl CrateSource {
    pub fn cratesio_registry() -> CrateSource {
        Self {
            source_type: SourceType::Registry,
            url: MaybeOwned::Borrowed(cratesio_url()),
        }
    }
}