Skip to content

Commit

Permalink
Removed lazy_static in favour of OnceLock
Browse files Browse the repository at this point in the history
  • Loading branch information
lmammino committed Jul 9, 2023
1 parent 427e1a3 commit 37409c1
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 21 deletions.
9 changes: 1 addition & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ categories = ["command-line-interface", "command-line-utilities", "cryptography"
repository = "https://github.com/lmammino/jwtinfo"
documentation = "https://github.com/lmammino/jwtinfo"
readme = "README.md"
version = "0.3.0"
version = "0.4.0"
authors = ["Luciano Mammino", "Stefano Abalsamo"]
edition = "2018"
license = "MIT"
Expand All @@ -16,7 +16,6 @@ clap = "4.3"
base64 = "0.21"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
lazy_static = "1.4"

[lib]
name = "jwtinfo"
Expand Down
3 changes: 0 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ use std::process;

mod jwt;

#[macro_use]
extern crate lazy_static;

#[doc(hidden)]
fn main() -> io::Result<()> {
let matches = Command::new("jwtinfo")
Expand Down
14 changes: 9 additions & 5 deletions src/jwt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,20 @@
use std::error::Error;
use std::fmt;
use std::str;
use std::sync::OnceLock;

use base64::{
alphabet,
engine::{self, general_purpose},
Engine as _,
};

lazy_static! {
static ref BASE64_URLSAFE: engine::GeneralPurpose =
engine::GeneralPurpose::new(&alphabet::URL_SAFE, general_purpose::NO_PAD);
static BASE64_ENGINE: OnceLock<engine::GeneralPurpose> = OnceLock::new();

#[inline]
fn get_base64() -> &'static engine::GeneralPurpose {
BASE64_ENGINE
.get_or_init(|| engine::GeneralPurpose::new(&alphabet::URL_SAFE, general_purpose::NO_PAD))
}

/// Represents a JWT, composed by a header, a body and a signature
Expand Down Expand Up @@ -149,7 +153,7 @@ impl Error for JWTParsePartError {}

#[doc(hidden)]
fn parse_base64_string(s: &str) -> Result<String, JWTParseError> {
let s = BASE64_URLSAFE.decode(s)?;
let s = get_base64().decode(s)?;
let s = str::from_utf8(&s)?;
Ok(s.to_string())
}
Expand Down Expand Up @@ -180,7 +184,7 @@ fn parse_body(raw_body: Option<&str>) -> Result<serde_json::Value, JWTParseError
fn parse_signature(raw_signature: Option<&str>) -> Result<Vec<u8>, JWTParseError> {
match raw_signature {
None => Err(JWTParseError::MissingSection()),
Some(s) => Ok(BASE64_URLSAFE.decode(s)?),
Some(s) => Ok(get_base64().decode(s)?),
}
}

Expand Down
3 changes: 0 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,3 @@
//! ```

pub mod jwt;

#[macro_use]
extern crate lazy_static;

0 comments on commit 37409c1

Please sign in to comment.