rustup_available_packages/
source.rs

1use chrono::NaiveDate;
2use std::borrow::Cow;
3
4/// A set of methods that we need to retrieve manifest from a source.
5pub trait SourceInfo {
6    /// A type of URLs returned by this trait.
7    type Url: AsRef<str>;
8
9    /// Makes a URL for a manifest for a specified date.
10    fn make_manifest_url(&self, _: NaiveDate) -> Self::Url;
11
12    /// Makes a URL for the latest manifest.
13    fn make_latest_manifest_url(&self) -> Self::Url;
14}
15
16/// Default source, i.e. `https://static.rust-lang.org/...`.
17pub struct DefaultSource<'a> {
18    channel: &'a str,
19    base_url: Cow<'a, str>,
20}
21
22impl<'a> DefaultSource<'a> {
23    /// Default base url.
24    pub const DEFAULT_BASE_URL: &'static str = "https://static.rust-lang.org/dist";
25
26    /// Initializes a new default source instance for a channel.
27    pub fn new(channel: &'a str) -> Self {
28        DefaultSource {
29            channel,
30            base_url: Cow::Borrowed(Self::DEFAULT_BASE_URL),
31        }
32    }
33
34    /// Overrides the base URL.
35    pub fn override_base(&mut self, base_url: Cow<'a, str>) {
36        self.base_url = base_url
37    }
38}
39
40impl<'a> SourceInfo for DefaultSource<'a> {
41    type Url = String;
42
43    fn make_manifest_url(&self, date: NaiveDate) -> Self::Url {
44        format!(
45            "{}/{}/channel-rust-{}.toml",
46            self.base_url, date, self.channel
47        )
48    }
49
50    fn make_latest_manifest_url(&self) -> Self::Url {
51        format!("{}/channel-rust-{}.toml", self.base_url, self.channel)
52    }
53}