From 5bdffd9178805899c8d06a6d060778f32d8abec7 Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Wed, 20 Jul 2022 16:48:34 +1000 Subject: [PATCH] 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 --- src/main.rs | 4 +--- src/metafiles/cvs.rs | 10 ++++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index cea320ce..9ed8f67f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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..."); diff --git a/src/metafiles/cvs.rs b/src/metafiles/cvs.rs index a111d02f..2b8f2214 100644 --- a/src/metafiles/cvs.rs +++ b/src/metafiles/cvs.rs @@ -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> = + 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 {