mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-21 04:58:42 +00:00
Optimize: Create new fn helpers::cargo_home
that caches return value of `home::cargo_home` Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
305a4e4c30
commit
21eac33e1f
3 changed files with 14 additions and 2 deletions
|
@ -1,12 +1,14 @@
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::io;
|
use std::io;
|
||||||
|
use std::ops;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use cargo_toml::Manifest;
|
use cargo_toml::Manifest;
|
||||||
use futures_util::stream::Stream;
|
use futures_util::stream::Stream;
|
||||||
use log::debug;
|
use log::debug;
|
||||||
|
use once_cell::sync::OnceCell;
|
||||||
use reqwest::{tls, Client, ClientBuilder, Method, Response};
|
use reqwest::{tls, Client, ClientBuilder, Method, Response};
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use tempfile::NamedTempFile;
|
use tempfile::NamedTempFile;
|
||||||
|
@ -40,6 +42,14 @@ pub use tls_version::TLSVersion;
|
||||||
mod crate_name;
|
mod crate_name;
|
||||||
pub use crate_name::CrateName;
|
pub use crate_name::CrateName;
|
||||||
|
|
||||||
|
pub fn cargo_home() -> Result<&'static Path, io::Error> {
|
||||||
|
static CARGO_HOME: OnceCell<PathBuf> = OnceCell::new();
|
||||||
|
|
||||||
|
CARGO_HOME
|
||||||
|
.get_or_try_init(home::cargo_home)
|
||||||
|
.map(ops::Deref::deref)
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn await_task<T>(task: tokio::task::JoinHandle<miette::Result<T>>) -> miette::Result<T> {
|
pub async fn await_task<T>(task: tokio::task::JoinHandle<miette::Result<T>>) -> miette::Result<T> {
|
||||||
match task.await {
|
match task.await {
|
||||||
Ok(res) => res,
|
Ok(res) => res,
|
||||||
|
|
|
@ -10,6 +10,7 @@ use serde::{Deserialize, Serialize};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
use super::CrateVersionSource;
|
use super::CrateVersionSource;
|
||||||
|
use crate::cargo_home;
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
|
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
|
||||||
pub struct CratesToml {
|
pub struct CratesToml {
|
||||||
|
@ -18,7 +19,7 @@ pub struct CratesToml {
|
||||||
|
|
||||||
impl CratesToml {
|
impl CratesToml {
|
||||||
pub fn default_path() -> Result<PathBuf, CratesTomlParseError> {
|
pub fn default_path() -> Result<PathBuf, CratesTomlParseError> {
|
||||||
Ok(home::cargo_home()?.join(".crates.toml"))
|
Ok(cargo_home()?.join(".crates.toml"))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load() -> Result<Self, CratesTomlParseError> {
|
pub fn load() -> Result<Self, CratesTomlParseError> {
|
||||||
|
|
|
@ -9,6 +9,7 @@ use serde::{Deserialize, Serialize};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
use super::CrateVersionSource;
|
use super::CrateVersionSource;
|
||||||
|
use crate::cargo_home;
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
|
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
|
||||||
pub struct Crates2Json {
|
pub struct Crates2Json {
|
||||||
|
@ -39,7 +40,7 @@ pub struct CrateInfo {
|
||||||
|
|
||||||
impl Crates2Json {
|
impl Crates2Json {
|
||||||
pub fn default_path() -> Result<PathBuf, Crates2JsonParseError> {
|
pub fn default_path() -> Result<PathBuf, Crates2JsonParseError> {
|
||||||
Ok(home::cargo_home()?.join(".crates2.json"))
|
Ok(cargo_home()?.join(".crates2.json"))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load() -> Result<Self, Crates2JsonParseError> {
|
pub fn load() -> Result<Self, Crates2JsonParseError> {
|
||||||
|
|
Loading…
Add table
Reference in a new issue