Refactor: Extract helpers::cratesio_url

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-07-27 19:22:45 +10:00
parent 5ca1278c22
commit cc13aa911f
No known key found for this signature in database
GPG key ID: 591C0B03040416D6
2 changed files with 11 additions and 6 deletions

View file

@ -10,7 +10,7 @@ use bytes::Bytes;
use cargo_toml::Manifest;
use futures_util::stream::Stream;
use log::debug;
use once_cell::sync::OnceCell;
use once_cell::sync::{Lazy, OnceCell};
use reqwest::{tls, Client, ClientBuilder, Method, Response};
use serde::Serialize;
use tempfile::NamedTempFile;
@ -55,6 +55,13 @@ pub fn cargo_home() -> Result<&'static Path, io::Error> {
.map(ops::Deref::deref)
}
pub fn cratesio_url() -> &'static Url {
static CRATESIO: Lazy<Url, fn() -> Url> =
Lazy::new(|| url::Url::parse("https://github.com/rust-lang/crates.io-index").unwrap());
&*CRATESIO
}
/// Returned file is readable and writable.
pub fn create_if_not_exist(path: impl AsRef<Path>) -> io::Result<fs::File> {
let path = path.as_ref();

View file

@ -1,12 +1,13 @@
use std::{borrow::Cow, fmt, str::FromStr};
use miette::Diagnostic;
use once_cell::sync::Lazy;
use semver::Version;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use thiserror::Error;
use url::Url;
use crate::cratesio_url;
#[derive(Clone, Debug, Ord, PartialOrd, Eq, PartialEq)]
pub struct CrateVersionSource {
pub name: String,
@ -23,10 +24,7 @@ pub enum Source {
impl Source {
pub fn cratesio_registry() -> Source {
static CRATESIO: Lazy<Url, fn() -> Url> =
Lazy::new(|| url::Url::parse("https://github.com/rust-lang/crates.io-index").unwrap());
Self::Registry(CRATESIO.clone())
Self::Registry(cratesio_url().clone())
}
}