From 70324b1f9a1bec8db2fdc62647a9615ae38148a3 Mon Sep 17 00:00:00 2001 From: L-ING Date: Fri, 19 Jan 2024 20:28:22 +0800 Subject: [PATCH] perf: use static variable for cmd arguments --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/env.rs | 25 +++++++++++++++++++++++++ src/main.rs | 1 + src/utils.rs | 5 ++--- 5 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 src/env.rs diff --git a/Cargo.lock b/Cargo.lock index 31a4df8..1b3d4a7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -489,6 +489,7 @@ dependencies = [ "actix-web", "anyhow", "env_logger", + "lazy_static", "pico-args", "reqwest", "serde", @@ -646,6 +647,12 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d4345964bb142484797b161f473a503a434de77149dd8c7427788c6e13379388" +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "libc" version = "0.2.150" diff --git a/Cargo.toml b/Cargo.toml index ca99b04..af62656 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ opt-level = 'z' actix-web = { version = "4.4.1", default-features = false, features = ["macros"] } anyhow = { version = "1.0.79", default-features = false, features = ["std"] } env_logger = { version = "0.10.1",default-features = false, features = ["humantime"] } +lazy_static = { version = "1.4.0", default-features = false } pico-args = { version = "0.5.0", default-features = false, features = ["short-space-opt"] } reqwest = { version = "0.11.23", default-features = false, features = ["rustls-tls"] } serde = { version = "1.0.193", default-features = false, features = ["derive"] } diff --git a/src/env.rs b/src/env.rs new file mode 100644 index 0000000..c7aeda4 --- /dev/null +++ b/src/env.rs @@ -0,0 +1,25 @@ +/* +:project: gpts-code-analyst +:author: L-ING +:copyright: (C) 2024 L-ING +:license: MIT, see LICENSE for more details. +*/ + +use lazy_static::lazy_static; +use pico_args::{Arguments, Error}; +use std::fmt::Display; +use std::str::FromStr; + +fn get_arg_value(arg_name: &'static str) -> Result +where + T: FromStr, + T::Err: Display, +{ + let mut args = Arguments::from_env(); + let value: T = args.value_from_str(arg_name)?; + return Ok(value); +} + +lazy_static! { + pub static ref GITHUB_TOKEN: Result = get_arg_value("--token"); +} diff --git a/src/main.rs b/src/main.rs index 2d075a9..3cf2112 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,6 +13,7 @@ use env_logger::Env; mod handlers; mod structures; mod utils; +mod env; #[actix_web::main] async fn main() -> std::io::Result<()> { diff --git a/src/utils.rs b/src/utils.rs index 310cd3a..c83ca9a 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -6,14 +6,13 @@ */ use anyhow::Context; -use pico_args::Arguments; use std::error::Error; +use crate::env::GITHUB_TOKEN; use crate::structures::{FileInfo, PathTree, RepoInfo}; pub fn create_client() -> Result> { - let mut args = Arguments::from_env(); - let github_token = args.value_from_str::<&str, String>("--token")?; + let github_token = GITHUB_TOKEN.as_ref()?; let mut headers = reqwest::header::HeaderMap::new(); headers.insert( reqwest::header::AUTHORIZATION,