Remove regex/lazy_static dependencies

This commit is contained in:
Gijs Burghoorn 2022-11-22 21:20:55 +01:00
parent 1d7690b707
commit 3d1dba4cc3
3 changed files with 25 additions and 53 deletions

40
Cargo.lock generated
View file

@ -2,15 +2,6 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "aho-corasick"
version = "0.7.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f"
dependencies = [
"memchr",
]
[[package]]
name = "autocfg"
version = "1.0.1"
@ -156,12 +147,6 @@ dependencies = [
"cfg-if 1.0.0",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "lemurs"
version = "0.3.0"
@ -171,14 +156,12 @@ dependencies = [
"crossterm 0.22.1",
"fern",
"hex",
"lazy_static",
"libc 0.2.112",
"log",
"nix 0.23.1",
"pam",
"pgs-files",
"rand",
"regex",
"serde",
"toml",
"tui",
@ -216,12 +199,6 @@ dependencies = [
"cfg-if 1.0.0",
]
[[package]]
name = "memchr"
version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a"
[[package]]
name = "memoffset"
version = "0.6.5"
@ -449,23 +426,6 @@ dependencies = [
"redox_syscall",
]
[[package]]
name = "regex"
version = "1.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d83f127d94bdbcda4c8cc2e50f6f84f4b611f69c902699ca385a39c3a75f9ff1"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.6.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49b3de9ec5dc0a3417da371aab17d729997c15010e7fd24ff707773a33bddb64"
[[package]]
name = "scopeguard"
version = "1.1.0"

View file

@ -21,10 +21,6 @@ tui = { version = "0.16.0", features = [ "crossterm" ] }
unicode-width = "0.1"
crossterm = { version = "0.22" }
# Parsing cached information
lazy_static = "1.4.0"
regex = "1.5"
# Interacting with the kernel interfaces
rand = "0.8.4"
nix = "0.23.1"

View file

@ -1,10 +1,7 @@
use lazy_static::lazy_static;
use log::{info, warn};
use regex::Regex;
use std::fs::{read_to_string, write};
pub const CACHE_PATH: &str = "/var/cache/lemurs";
const USERNAME_REGEX_STR: &str = r"^[a-z][-a-z0-9]*$";
const USERNAME_LENGTH_LIMIT: usize = 32;
// Saved in the /var/cache/lemurs file as
@ -18,6 +15,29 @@ pub struct CachedInfo {
username: Option<String>,
}
fn verify_username(username: &str) -> bool {
// REGEX: "^[a-zA-Z][-a-zA-Z0-9]*$"
if username.len() > USERNAME_LENGTH_LIMIT {
return false;
}
let mut bytes = username.bytes();
match bytes.next() {
Some(b'a'..=b'z' | b'A'..=b'Z') => {}
_ => return false,
};
for b in bytes {
if !matches!(b, b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' | b'-') {
return false;
}
}
true
}
impl CachedInfo {
pub fn environment(&self) -> Option<&str> {
self.environment.as_deref()
@ -28,10 +48,6 @@ impl CachedInfo {
}
}
lazy_static! {
static ref USERNAME_REGEX: Regex = Regex::new(USERNAME_REGEX_STR).unwrap();
}
pub fn get_cached_information() -> CachedInfo {
info!(
"Attempting to get a cached information from '{}'",
@ -61,7 +77,7 @@ pub fn get_cached_information() -> CachedInfo {
None
// Username validity check (through regex)
} else if !USERNAME_REGEX.is_match(cached_username) {
} else if !verify_username(cached_username) {
warn!("Cached username is not a valid username and is therefore not loaded.");
None
} else {
@ -97,7 +113,7 @@ pub fn set_cache(environment: Option<&str>, username: Option<&str>) {
}
// Username validity check (through regex)
if !USERNAME_REGEX.is_match(username) {
if !verify_username(username) {
warn!("Username is not a valid username and is therefore not cached.");
None
} else {