Refactor: Impl Source::cratesio_registry

Makes initializing `metafiles::CrateVersionSource` more readable and
improves performance since the parsing is now cached.

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-07-20 16:48:34 +10:00
parent 3961dbb84a
commit 5bdffd9178
No known key found for this signature in database
GPG key ID: 591C0B03040416D6
2 changed files with 11 additions and 3 deletions

View file

@ -650,9 +650,7 @@ async fn install_from_package(
let cvs = metafiles::CrateVersionSource {
name: name.clone(),
version: package.version.parse().into_diagnostic()?,
source: metafiles::Source::Registry(
url::Url::parse("https://github.com/rust-lang/crates.io-index").unwrap(),
),
source: metafiles::Source::cratesio_registry(),
};
info!("Installing binaries...");

View file

@ -1,6 +1,7 @@
use std::{fmt, str::FromStr};
use miette::Diagnostic;
use once_cell::sync::Lazy;
use semver::Version;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use thiserror::Error;
@ -20,6 +21,15 @@ pub enum Source {
Registry(Url),
}
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())
}
}
impl FromStr for CrateVersionSource {
type Err = CvsParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {